gpt4 book ai didi

Java Android - JPEG 图像旋转

转载 作者:行者123 更新时间:2023-12-02 02:15:52 25 4
gpt4 key购买 nike

我正在开发一个捕获图像的应用程序,但我想在保存之前旋转 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 上不起作用:/有什么想法吗?我可以对视频录制做类似的事情吗?

最佳答案

您正在将位图转换为字节数组,现在你停止这样将Bitmap直接保存到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();
}

关于Java Android - JPEG 图像旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49305022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com