Android - при съемке изображения вращаются - PullRequest
0 голосов
/ 09 ноября 2018

Когда я снимаю и сохраняю изображение, изображение автоматически поворачивается. Это происходит только с некоторыми телефонами 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;
    }
}
...