gpt4 book ai didi

android - 如何在保存之前在 android 相机预览应用程序中旋转图像?

转载 作者:行者123 更新时间:2023-11-29 20:55:51 25 4
gpt4 key购买 nike

我有一个以纵向模式拍照的 Android 相机应用程序,相机预览很好......但是当我拍照时它以横向模式保存......它旋转 90 度:我的代码 onPictureTaken():

PictureCallback jpegCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";

String filename = pictureFileDir.getPath() + File.separator
+ photoFile;

Log.e("path", filename.toString());

File pictureFile = new File(filename);

try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(arg0);
fos.close();

Bitmap bm = BitmapFactory.decodeFile(filename);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
// sba1 = Base64.encodeBytes(ba);
int flag = 0; // you can pass the default 0 = Base64.DEFAULT
String ba1 = Base64.encodeToString(ba, flag);

// Log.e("base64", "-----" + ba1);

Toast.makeText(AndroidCamera.this,
"New Image saved 22222:" + photoFile, Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(AndroidCamera.this,
UploadActivity.class);

fileUri = Uri.fromFile(pictureFile);

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

// start the image capture Intent
startActivityForResult(intent,
CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
launchUploadActivity(true);

} catch (Exception error) {
// Log.d(MakePhotoActivity.DEBUG_TAG, "File" + filename +
// "not saved: "
// + error.getMessage());
Toast.makeText(AndroidCamera.this, "Image could not be saved.",
Toast.LENGTH_LONG).show();
}
}
};

最佳答案

您可以像这样将位图旋转 90 度:

Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);

在你的具体例子中,你需要将它保存回原来的路径:

Bitmap bm = BitmapFactory.decodeFile(filename);

Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);

FileOutputStream fos = new FileOutputStream(pictureFile);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();

您还可以使用 Camera.Parameters.setRotation()

关于android - 如何在保存之前在 android 相机预览应用程序中旋转图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27771999/

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