gpt4 book ai didi

android - 从相机捕获图像,导致方向爆炸

转载 作者:太空宇宙 更新时间:2023-11-03 12:54:57 25 4
gpt4 key购买 nike

我正在尝试从相机捕捉图像,它在风景模式下工作正常,当我拍摄肖像照片时它会旋转。下面是我正在使用的代码:

            BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(largeImagePath, bounds);

Bitmap bm = BitmapFactory.decodeFile(largeImagePath, opts);
ExifInterface exif = new ExifInterface(largeImagePath);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
System.out.println("Orientation camera:"+orientString);
int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90){
rotationAngle = 90;




System.out.println("In 90 degrees rotate");
}
if (orientation == ExifInterface.ORIENTATION_ROTATE_180){
rotationAngle = 180;
System.out.println("In 180 degrees rotate");
}
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
rotationAngle = 270;
System.out.println("In 270 degrees rotate");
}

Matrix matrix = new Matrix();
System.out.println("Rotation Angle"+rotationAngle);
matrix.setRotate(rotationAngle, (float) thumbnail.getWidth() / 2, (float) thumbnail.getHeight() / 2);
bitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);

在调试时,控件输入:

if (orientation == ExifInterface.ORIENTATION_ROTATE_90){
rotationAngle = 90;}

block ,但是没有旋转来将图片设置回正确的位置。

这是我的 XML:

<ImageView
android:id="@+id/imageView1"
android:layout_width="140dp"
android:layout_height="160dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:src="@drawable/btn_upload" />

更新:我收到以下异常:

02-27 13:51:55.126: I/System.out(16549): Exceptionjava.lang.IllegalArgumentException: x + width must be <= bitmap.width()

最佳答案

您可以按如下方式安排您的代码...

    Bitmap bm = BitmapFactory.decodeFile(largeImagePath, opts);
ExifInterface ei;
try {
ei = new ExifInterface(largeImagePath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
bitmap = rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
bitmap = rotateImage(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
bitmap = rotateImage(bitmap, 270);
break;
}
} catch (IOException e) {
e.printStackTrace();
}

编写rotateImage()方法如下....

private Bitmap rotateImage(Bitmap source, float angle) {

Bitmap bitmap = null;
Matrix matrix = new Matrix();
matrix.postRotate(angle);
try {
bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
} catch (OutOfMemoryError err) {
err.printStackTrace();
}
return bitmap;
}

关于android - 从相机捕获图像,导致方向爆炸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22062555/

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