2017-07-14 86 views
1

我正在使用下面的代码保存在我的应用程序的图片:垂直图像旋转

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

    } 

我的问题是,如果拍摄的图像是垂直的,图像会翻转水平。我不知道究竟发生了什么,我很感激任何帮助。谢谢。

+0

或许事做 '的Exif' 如果你的设备的相机操作这样。而你并没有考虑它。 [https://www.howtogeek.com/254830/why-your-photos-dont-always-appear-correctly-rotated/] – Doomsknight

回答

0

某些设备摄像头使用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; 
} 

helper方法:

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

谢谢!我是一个很大的小白菜,但是谢谢你教我一些东西。 –

+0

@Mateuszplaza没问题。当我第一次遇到它时,也让我感到困惑。 :) – Doomsknight