繁体   English   中英

如何检查是否使用Android相机以纵向或横向拍摄图像?

[英]How to check whether an image is captured in portrait mode or landscape mode using camera in android?

我正在创建一个打开照片库的应用,通过从图库中选择该照片,该照片将显示在另一个活动中。 我的问题是,以人像模式拍摄的照片在显示后会被旋转。 但是我以横向模式拍摄的照片将正确显示。

因此,我必须检查是否使用Android相机以纵向模式或横向模式捕获图像,以便旋转纵向捕获的照片。 谁能帮我怎么做?

注意:肖像拍摄的图像和风景拍摄的图像的宽度和高度均相同。

您始终可以使用Matrix检查图像的旋转并相应地旋转它。

这段代码放在onActivityResult->

    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inPurgeable = true;

        Bitmap cameraBitmap = BitmapFactory.decodeFile(filePath);//get file path from intent when you take iamge.
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);


        ExifInterface exif = new ExifInterface(filePath);
        float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);  
        System.out.println(rotation);

        float rotationInDegrees = exifToDegrees(rotation);
        System.out.println(rotationInDegrees);

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

        Bitmap scaledBitmap = Bitmap.createBitmap(cameraBitmap);
        Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
        FileOutputStream fos=new FileOutputStream(filePath);
        rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();

OnActivityResult代码在这里结束。

下面的此函数用于旋转:-

    private static float exifToDegrees(float exifOrientation) {        
    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;    
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM