【发布时间】:2018-08-24 14:13:40
【问题描述】:
我正在开发一个捕获图像的应用程序,但我想在保存之前旋转 JPEG 图像,我已经看到了这个链接: Android Rotate Picture before saving
这就是我现在正在做的事情。
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();
}
我试过这样旋转图像:
// 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();
但是当我在我的图库中查看文件时,我无法读取它,图像似乎损坏了。
编辑:在 Android 7.1.1 上不起作用:/ 知道吗?我可以为视频记录做类似的事情吗?
【问题讨论】:
-
当您保存而不旋转时,是否会将您的图像显示到图库中。?
-
你分享的链接 - 看看有 17 个赞的答案
-
是的,@NikunjParadva
标签: java android matrix android-camera2