gpt4 book ai didi

java - Android 自行旋转相机拍摄的照片

转载 作者:行者123 更新时间:2023-12-02 12:42:33 24 4
gpt4 key购买 nike

我想启动原生 Android 相机并将捕获的图像保存在指定位置,但是当我这样做并稍后尝试到达照片时,保存的图像会自行旋转 90 度。我正在使用 LG 手机来运行该程序。我已经尝试过在这里找到的解决方案,但它们不起作用。我把我的代码放在这里...请纠正我或给我一个新的解决方案。非常感谢

 private void cameraIntent() {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("photo", true);
intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
startActivityForResult(intent, REQUEST_CAMERA);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}



private void onCaptureImageResult(Intent data) {


ByteArrayOutputStream bytes = null;
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

File destination = new File(getExternalStorageDirectory(),
File.separator + "profile.jpg");

FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


String address = getExternalStorageDirectory().getAbsolutePath();
ExifInterface exif = null;
try {
if(destination.exists()) {
exif = new ExifInterface(address+File.separator+"profile.jpg");

Log.d("EXIF value", exif.getAttribute(ExifInterface.TAG_ORIENTATION));
}
} catch (IOException e) {
e.printStackTrace();
}



if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")){
thumbnail= rotate(thumbnail, 90);
}else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")){
thumbnail= rotate(thumbnail, 270);
}else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")){
thumbnail= rotate(thumbnail, 270);

}



resized = Bitmap.createScaledBitmap(thumbnail, navUserImage.getWidth(), navUserImage.getHeight(), true);
navUserImage.setImageBitmap(resized);
}

最佳答案

它不会在所有设备中旋转。您可以尝试this解决方案。我曾经做过类似的事情,它很适合我。

关于java - Android 自行旋转相机拍摄的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44881540/

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