How can i rotate nv21 image format in android

I tried the following code to rotate an image. 90 degrees by default is 180 degrees.

public static void rotateNV21(byte[] input, byte[] output, int width, int height, int rotation) {
    boolean swap = (rotation == 90 || rotation == 270);
    boolean yflip = (rotation == 90 || rotation == 180);
    boolean xflip = (rotation == 270 || rotation == 180);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int xo = x, yo = y;
            int w = width, h = height;
            int xi = xo, yi = yo;
            if (swap) {
                xi = w * yo / h;
                yi = h * xo / w;
            }
            if (yflip) {
                yi = h - yi - 1;
            }
            if (xflip) {
                xi = w - xi - 1;
            }
            output[w * yo + xo] = input[w * yi + xi];
            int fs = w * h;
            int qs = (fs >> 2);
            xi = (xi >> 1);
            yi = (yi >> 1);
            xo = (xo >> 1);
            yo = (yo >> 1);
            w = (w >> 1);
            h = (h >> 1);
            // adjust for interleave here
            int ui = fs + (w * yi + xi) * 2;
            int uo = fs + (w * yo + xo) * 2;
            // and here
            int vi = ui + 1;
            int vo = uo + 1;
            output[uo] = input[ui]; 
            output[vo] = input[vi]; 
        }
    }
}   

      

I can rotate the image, but the image is compressed. Here's an example.enter image description here

Any idea what's going on, I've been stuck on this for a week. Any help would be much appreciated.

Here's the code to get the image

    public Bitmap getPic(int x, int y, int width, int height) {
        System.gc();
        Bitmap b;
        Size s = mParameters.getPreviewSize();
        byte[] mmbuffer = Rotate.rotateNV21(mBuffer, s.width, s.height, 90);
        YuvImage yuvimage = new YuvImage(mmbuffer, ImageFormat.NV21, s.width, s.height, null);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        yuvimage.compressToJpeg(new Rect(x, y, width, height), 100, outStream); // make JPG
        b = BitmapFactory.decodeByteArray(outStream.toByteArray(), 0, outStream.size()); // decode JPG
        if (b != null) {
//            Matrix matrix = new Matrix();
//            matrix.preRotate(90);
//            b = Bitmap.createBitmap(b, x, y, width, height, matrix, true);
        } else {
            //Log.i(TAG, "getPic(): Bitmap is null..");
        }
        yuvimage = null;
        outStream = null;
        System.gc();
        return b;
    }

      

here is the code i am trying to modify to use the camera in portait and crop the image at runtime. I need to rotate the image in order to crop the image with success and accuracy.

+3
java android


source to share


No one has answered this question yet

Check out similar questions:

3295
Why is the Android emulator so slow? How can we speed up the development of an Android emulator?
2240
How to create an executable JAR with dependencies using Maven?
1415
How do I convert the stack trace to a string?
1296
Reloading activity on android rotation
960
How to rotate the screen of Android emulator?
753
Code formatting shortcut in Android Studio
4
how to rotate bitmap in android by center point using matrix
1
Invalid format for the YuvImage Android class?
1
Moving an image after rotating does not behave correctly in android
-2
Rotate image bitmap from gallery



All Articles
Loading...
X
Show
Funny
Dev
Pics