从相机捕获图像,导致炸毁的方向 [英] Capturing image from camera, results in blowing up the orientation

查看:165
本文介绍了从相机捕获图像,导致炸毁的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从摄像机捕捉的图像,它工作正常与横向模式,当我参加画像它旋转图片。下面是code我使用的:

  BitmapFactory.Options边界=新BitmapFactory.Options();
            bounds.inJustDe codeBounds = TRUE;
            BitmapFactory.de codeFILE(largeImagePath,边界);            位图BM = BitmapFactory.de codeFILE(largeImagePath,选择采用);
            ExifInterface EXIF​​ =新ExifInterface(largeImagePath);
            字符串orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
            的System.out.println(方向拍照:+ orientString);
            INT方向= orientString!= NULL?的Integer.parseInt(orientString):ExifInterface.ORIENTATION_NORMAL;
            INT rotationAngle = 0;
            如果(方向== ExifInterface.ORIENTATION_ROTATE_90){
                rotationAngle = 90;
                的System.out.println(在旋转90度);
            }
            如果(方向== ExifInterface.ORIENTATION_ROTATE_180){
                rotationAngle = 180;
                的System.out.println(在180度旋转);
            }
            如果(方向== ExifInterface.ORIENTATION_ROTATE_270){
                rotationAngle = 270;
                的System.out.println(在270度旋转);
            }            字模=新的Matrix();
            的System.out.println(旋转角度+ rotationAngle);
            matrix.setRotate(rotationAngle,(浮点)thumbnail.getWidth()/ 2,(浮点)thumbnail.getHeight()/ 2);
            位图= Bitmap.createBitmap(BM,0,0,bounds.outWidth,bounds.outHeight,矩阵,真);

在调试,控制进入:

 如果(方向== ExifInterface.ORIENTATION_ROTATE_90){
                rotationAngle = 90;}

块,但没有旋转设置图像回纠正情况。

这是我的XML:

 < ImageView的
        机器人:ID =@ + ID / imageView1
        机器人:layout_width =140dp
        机器人:layout_height =160dp
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:layout_marginLeft =10dp
        机器人:layout_marginTop =30dp
        机器人:SRC =@绘制/ btn_upload/>

更新:我收到以下异常:

  02-27 13:51:55.126:我/的System.out(16549):Exceptionjava.lang.IllegalArgumentException:X +宽度必须< = bitmap.width()


解决方案

您可以安排您的code如下...

 位图BM = BitmapFactory.de codeFILE(largeImagePath,选择采用);
    ExifInterface EI;
    尝试{
        EI =新ExifInterface(largeImagePath);
        INT方向= ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        开关(方向){
        案例ExifInterface.ORIENTATION_ROTATE_90:
            位图= rotateImage(位图,90);
            打破;
        案例ExifInterface.ORIENTATION_ROTATE_180:
            位= rotateImage(位图,180);
            打破;
        案例ExifInterface.ORIENTATION_ROTATE_270:
            位= rotateImage(位图,270);
            打破;
        }
    }赶上(IOException异常五){
        e.printStackTrace();
    }

rotateImage()方法如下....

 私人位图rotateImage(位图源,飘角){    位图位图= NULL;
    字模=新的Matrix();
    matrix.postRotate(角度);
    尝试{
        位= Bitmap.createBitmap(源,0,0,source.getWidth(),source.getHeight()
                矩阵,真);
    }赶上(ERR的OutOfMemoryError){
        err.printStackTrace();
    }
    返回位图;
}

I am trying to capture image from the camera, it works fine with landscape mode, when I take the picture in portrait it is rotated. Below is the code I am using:

            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); 

On Debug, the control enters the :

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

block, however no rotation to set the picture back to correct happens.

This is my 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" />

UPDATE: I am getting the following exception:

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

解决方案

you can arrange your code as follow...

    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();
    }

write rotateImage() method as follows....

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;
}

这篇关于从相机捕获图像,导致炸毁的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆