java - 垂直图像旋转

标签 java android

我正在使用以下代码在我的应用程序中拍摄并保存照片:

    public void addPhoto(){
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                // Create the File where the photo should go
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {}

                // Continue only if the File was successfully created
                if (photoFile != null) {
                    Uri photoURI = FileProvider.getUriForFile(this,
                            BuildConfig.APPLICATION_ID,
                            photoFile);
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
             }

        }


        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(requestCode==REQUEST_IMAGE_CAPTURE&&resultCode==RESULT_OK){

                Intent i = new Intent(Assignments.this, AddAssignment.class);
                i.putExtra("fileName", mCurrentPhotoPath);
                startActivity(i);
            }
        }

然后,我使用以下代码缩放图像以创建较小尺寸的预览:

public Bitmap getImage(){
        int targetW;
        int targetH;

        // Get the dimensions of the bitmap
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(fileName, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        if(photoW>photoH){
            targetW=400;
            targetH=300;
        }else{
            targetW=300;
            targetH=400;

        }



        // Determine how much to scale down the image
        int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

        // Decode the image file into a Bitmap sized to fill the View
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

        Bitmap bitmap = BitmapFactory.decodeFile(fileName, bmOptions);
        photo=bitmap;

        return(bitmap);

    }

我的问题是,如果拍摄的图像是垂直的,则图像会水平翻转。我不知道这到底发生在哪里,如果有任何帮助,我将不胜感激。谢谢。

最佳答案

某些设备相机使用 Exif 标志。这是他们以自然轮换方式存储它的地方,并用一个标志来表示它。

下面是我编写的一些用于在内存中旋转位图的代码。请酌情使用/应用。

有关 exif 标志的信息:

https://www.howtogeek.com/254830/why-your-photos-dont-always-appear-correctly-rotated/

http://sylvana.net/jpegcrop/exif_orientation.html

     public static Bitmap decodeSampledBitmapFromFile(String photoDir,
                                                 int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    BitmapFactory.decodeFile(photoDir, options);

    // Calculate inSampleSize
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    int inSampleSize = 1;

    if (height > reqHeight) {
        inSampleSize = Math.round((float) height / (float) reqHeight);
    }

    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth) {// If bigger SampSize..
        inSampleSize = Math.round((float) width / (float) reqWidth);
    }


    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;


    Bitmap photo = BitmapFactory.decodeFile(photoDir, options);

    // WILL CHECK ROTATION FLAGS AND ROTATE AS NECESSARY
    try {
        ExifInterface exif = new ExifInterface(photoDir);
        float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        float rotationInDegrees = exifToDegrees(rotation);

        Matrix matrix = new Matrix();
        matrix.postRotate(rotationInDegrees);

        // if needs rotating, rotate in memory.
        if(rotationInDegrees != 0)
            photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);

    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return photo;
}

辅助方法:

private static float exifToDegrees(float exifOrientation) {
        // MAY NEED TO HANDLE THE OTHER FLAGS TOO, though I don't think android flips photos.
        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
            return 90;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
            return 180;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
            return 270;
        }
        return 0;
    }

关于java - 垂直图像旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45107077/

相关文章:

java - 我应该将在类级别定义的 Executor 服务实例设为静态吗?

java - 从 PJSIP 示例 (PJSUA2) 中调用 LogWriter 的位置打印

java.io.IOException : parseAlgParameters failed: PBE AlgorithmParameters not available, 当 docker 容器尝试访问 rabbitmq TLS 端口时导致

java - 如何将容器内的内部框架图标化到特定位置

java - 使用 Java 8 在文本文件中搜索字符串

java - JXBrowser类NotFoundException

android - 如何使用 Volley Library 从缓存中获取图像位图

android - 对齐父底部在 Android 设备上无法正常工作

android - android studio中的NoClassDefFoundError异常

android - 多个设备/模拟器 react native react native 启动错误