Android-拍照时图像旋转

时间:2018-11-09 03:00:11

标签: android android-studio inputstream android-bitmap

当我拍照并保存图像时,图像会自动旋转。这仅发生在某些Android手机上。有人可以帮我吗?谢谢。这是将图像保存到位图时的代码

/*
            imageUri will parse the path that was saved to mCurrentPhoto
            */
            if(mCurrentPhotoPath1 != null) {
                Uri imageUri = Uri.parse(mCurrentPhotoPath1);
                File file = new File(imageUri.getPath());
                try {
                /*
                The image will be save to bmp1.
                 */
                    InputStream ims = new FileInputStream(file);
                    //bmp1 = BitmapFactory.decodeStream(ims);

                    ExifInterface exif = null;
                    try {
                        exif = new ExifInterface(mCurrentPhotoPath1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_UNDEFINED);
                    bmp1 = rotateBitmap(BitmapFactory.decodeStream(ims), orientation);

                /*
                From bmp1 a thumbnail image will be create.
                 */
                    Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(bmp1, 200, 100);
                /*
                The thumbnail image will be set to the imgbutton1 so the user can see the thumnail of the image.
                 */

                    imgbutton1.setImageBitmap(ThumbImage);
                    wfDatabase.addCapturedName(temp_id + "_1" + ".jpg", mCurrentPhotoPath1, jobno);
                } catch (FileNotFoundException e) {
                    Log.e("Error:", e.getMessage());


                }
                }

对于rotateBitmap,这是静态方法:

公共静态位图rotateBitmap(位图位图,int方向){

    Matrix matrix = new Matrix();
    switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            return bitmap;
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.setScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.setRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.setRotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.setRotate(-90);
            break;
        default:
            return bitmap;
    }
    try {
        Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        bitmap.recycle();
        return bmRotated;
    }
    catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}

0 个答案:

没有答案