简体   繁体   中英

Java Android - JPEG Image rotation

I'm working on an application to capture images but I'd like to rotate a JPEG image before saving it, I already saw this link : Android Rotate Picture before saving

This is what I'm doing right now.

ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);

FileOutputStream fileOutputStream = null;

try {
    fileOutputStream = new FileOutputStream(mImageFileName);
    fileOutputStream.write(bytes);
} catch (IOException e) {
    e.printStackTrace();
}


I tried this to rotate the image like this :

// Bytes array to bitmap and matrix rotation
Bitmap sourceBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Matrix m = new Matrix();
m.setRotate((float)90, sourceBitmap.getWidth(), sourceBitmap.getHeight());
Bitmap targetBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), m, true);

// Bitmap to bytes array
int size = targetBitmap.getRowBytes() * targetBitmap.getHeight();
ByteBuffer targetByteBuffer = ByteBuffer.allocate(size);
targetBitmap.copyPixelsToBuffer(targetByteBuffer);
bytes = targetByteBuffer.array();

But when I look into the file into my gallery, I cannot read it, the image seems broken.

EDIT: Doesn't work on Android 7.1.1 :/ Any idea ? Can I do something similar for a video record?

You are Coverting Your Bitmap to bytes array , Now You stop That way save Bitmap directly to File

Bitmap sourceBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Matrix m = new Matrix();
m.setRotate((float)90, sourceBitmap.getWidth(), sourceBitmap.getHeight());
Bitmap rotatedBitmap= Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), m, true);

// Save Bitmap directly to the file

String filename = "hello.jpg";
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, filename);

try {
     FileOutputStream out = new FileOutputStream(dest);
     bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
     out.flush();
     out.close();
    } catch (Exception e) {
     e.printStackTrace();
    }

This little change apparently did the trick ! Thanks Nikunj !

ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);

Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Matrix matrix = new Matrix();
matrix.setRotate((float)90, bitmap.getWidth(), bitmap.getHeight());

FileOutputStream fileOutputStream = null;
try {
    fileOutputStream = new FileOutputStream(mImageFileName);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
    fileOutputStream.flush();
    fileOutputStream.close();
} catch (IOException e) {
    e.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM