2015-06-18 49 views
2

我在使用手机相机拍摄的照片时遇到了一些问题。当我拍照并保存时,我的手机会以原始​​尺寸显示它,但是当我尝试在我的应用程序上打开相同的照片时,它会输出旋转后的照片,如下所示:自动旋转智能手机拍摄的照片

原始照片:

http://i.imgur.com/P7nlfnC.png

旋转的照片:

enter image description here

,当我带着我的手机照片只出现此问题。

我用下面的代码来选择空间:

private void selectImage() { 

     final CharSequence[] options = {"My gallery","Cancel" }; 

     AlertDialog.Builder builder = new AlertDialog.Builder(PhotosActivity.this); 
     builder.setTitle("Add Photo!"); 
     builder.setItems(options, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int item) { 
       if (options[item].equals("My gallery")) 
       { 
        Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        startActivityForResult(intent, 2); 

       } 
       else if (options[item].equals("Cancel")) { 
        dialog.dismiss(); 
       } 
      } 
     }); 
     builder.show(); 
    } 

要选择照片:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     if (requestCode == 2) { 

      Uri selectedImage = data.getData(); 
      String[] filePath = { MediaStore.Images.Media.DATA }; 
      Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null); 
      c.moveToFirst(); 
      int columnIndex = c.getColumnIndex(filePath[0]); 
      String picturePath = c.getString(columnIndex); 
      c.close(); 
      Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath)); 

      if (thumbnail.getWidth() > thumbnail.getHeight()) { 
       Matrix matrix = new Matrix(); 
       matrix.postRotate(90); 
       thumbnail = Bitmap.createBitmap(thumbnail , 0, 0, thumbnail.getWidth(), thumbnail.getHeight(), matrix, true); 
      } 

      int nh = (int) (thumbnail.getHeight() * (612.0/thumbnail.getWidth())); 
      Bitmap scaled = Bitmap.createScaledBitmap(thumbnail, 612, nh, true); 

      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      scaled.compress(Bitmap.CompressFormat.JPEG, 90, stream); 

      byte [] byte_arr = stream.toByteArray(); 
      String image_str = Base64.encodeBytes(byte_arr); 

      newtask = new saveImage(); 
      newtask.execute(image_str,sessionid); 

      Toast.makeText(getBaseContext(), "Nice!", 
      Toast.LENGTH_LONG).show(); 

     } 
    } 
} 

我尝试使用下面的代码来修复它,但它不在风景照片上工作,我想获得一个代码来输出原始大小。

if (thumbnail.getWidth() > thumbnail.getHeight()) { 
        Matrix matrix = new Matrix(); 
        matrix.postRotate(90); 
        thumbnail = Bitmap.createBitmap(thumbnail , 0, 0, thumbnail.getWidth(), thumbnail.getHeight(), matrix, true); 
       } 

谢谢。

回答

0

您需要从mediastore查询方向(它是ImageColumns.ORIENTATION)。
尝试改变这一行:matrix.postRotate(90);matrix.postRotate(270);

+0

我搜索如何让我找到了解决的方向,我在这里使用此功能:http://stackoverflow.com/questions/21085105/get-orientation-of-image -from-mediastore-images-media-data和改变的matrix.postRotate(90);到matrix.postRotate(getExifOrientation(picturePath));现在工作正常。 –