2012-10-08 147 views
4

在我的应用程序,我需要从我的手机画廊页面上传一些图像。android拍摄的图像是在肖像

我正在使用三星Galaxy ace,并且我使用手机的默认相机在肖像模式下拍摄了一些图像。捕获后,我在我的应用程序中打开这些图像,并试图在图像视图中显示它。以纵向模式拍摄的图像似乎在图像视图中处于横向。

使用exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION)我检查图像取向值使其6.

使用以下代码我显示在图像肖像模式图像,

Matrix matrix = new Matrix(); 
         matrix.postRotate(90); 
         bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(HomePage._uri)); 
         bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
         i.setImageBitmap(bitmap); 

但之后将图像上载并在我的应用程序的另一个活动中检索它似乎再次处于横向模式。如何上传图片本身的图像?

I have captured in Portrait , i have showed it in portrait by myself, while uploading it i need it to be in portrait itself, so that when i am retrieving it i can view it in portrait mode

如何完成这件事,(捕捉我力用相机在我的应用程序,我捕捉使用手机默认的摄像头,应用外)

回答

1

我已经找到了解决方案从图库中获取图像并上传它。从图库中选择一些图像可能看起来旋转,在这种情况下,下面的解决方案工作良好

从图库中选择图片

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     intent.setType("image/*"); 
     startActivityForResult(intent, 2); 

下一页在onActivityResult

public void onActivityResult(int requestCode, int resultCode, final Intent data) 
    { 
     super.onActivityResult(requestCode, resultCode, data); 
     if(resultCode == Activity.RESULT_OK) 
     { 
      f(requestCode == 2) 
      { 
       try 
       { 
       String [] proj = { MediaStore.Images.Media.DATA }; 
       Cursor cursor = managedQuery(data.getData(), proj, null, null, null); 
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
       cursor.moveToFirst(); 
       pathInput = cursor.getString(column_index); 

       Appconstants.f = Environment.getExternalStorageDirectory() + "/tmp_siva.jpg"; 
       ImageUtils.resampleImageAndSaveToNewLocation(pathInput, Appconstants.f); 
       } 
       catch (Exception ex) 
       { 
       Log.e("Exception ex @ try catch",""+ex); 
       } 
      } 
      }    
    } 

这里是ImageUtils类

public class ImageUtils 
{ 
    private ImageUtils() 
    { 
    } 

    public static void resampleImageAndSaveToNewLocation(String pathInput, String pathOutput) throws Exception 
    { 
     Bitmap bmp = resampleImage(pathInput, 800); 

     OutputStream out = new FileOutputStream(pathOutput); 
     bmp.compress(Bitmap.CompressFormat.JPEG, 100, out); 
    } 

    public static Bitmap resampleImage(String path, int maxDim) throws Exception 
    {   
     BitmapFactory.Options bfo = new BitmapFactory.Options(); 
     bfo.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, bfo); 

     BitmapFactory.Options optsDownSample = new BitmapFactory.Options(); 
     optsDownSample.inSampleSize = getClosestResampleSize(bfo.outWidth, bfo.outHeight, maxDim); 

     Bitmap bmpt = BitmapFactory.decodeFile(path, optsDownSample); 

     Matrix m = new Matrix(); 

     if (bmpt.getWidth() > maxDim || bmpt.getHeight() > maxDim) 
     {   
      BitmapFactory.Options optsScale = getResampling(bmpt.getWidth(), bmpt.getHeight(), maxDim); 
      m.postScale((float)optsScale.outWidth/(float)bmpt.getWidth(), (float)optsScale.outHeight/(float)bmpt.getHeight()); 
      } 

     int sdk = new Integer(Build.VERSION.SDK).intValue(); 
     if (sdk > 4) 
     { 
      int rotation = ExifUtils.getExifRotation(path); 
      if (rotation != 0) 
      { 
       m.postRotate(rotation); 
      } 
     } 

     return Bitmap.createBitmap(bmpt, 0, 0, bmpt.getWidth(), bmpt.getHeight(), m, true); 
    } 

    private static BitmapFactory.Options getResampling(int cx, int cy, int max) 
    { 
     float scaleVal = 1.0f; 
     BitmapFactory.Options bfo = new BitmapFactory.Options(); 
     if (cx > cy) 
     { 
      scaleVal = (float)max/(float)cx; 
     } 
     else if (cy > cx) 
     { 
      scaleVal = (float)max/(float)cy; 
     } 
     else 
     { 
      scaleVal = (float)max/(float)cx; 
     } 
     bfo.outWidth = (int)(cx * scaleVal + 0.5f); 
     bfo.outHeight = (int)(cy * scaleVal + 0.5f); 
     return bfo; 
    } 

    private static int getClosestResampleSize(int cx, int cy, int maxDim) 
    { 
     /*Log.e("cx",""+cx); 
     Log.e("cy",""+cy);*/ 
     int max = Math.max(cx, cy); 

     int resample = 1; 
     for (resample = 1; resample < Integer.MAX_VALUE; resample++) 
     { 
      if (resample * maxDim > max) 
      { 
       resample--; 
       break; 
      } 
     } 

     if (resample > 0) 
     { 
      return resample; 
     } 
     return 1; 
    } 

    public static BitmapFactory.Options getBitmapDims(String path) throws Exception 
    { 
     BitmapFactory.Options bfo = new BitmapFactory.Options(); 
     bfo.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, bfo); 
     return bfo; 
    } 
} 

这里是Exif类

public class ExifUtils 
{ 
    private ExifUtils() 
    { 
    } 

    public static int getExifRotation(String imgPath) 
    { 
     try 
     { 
      ExifInterface exif = new ExifInterface(imgPath); 
      String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
      if (!TextUtils.isEmpty(rotationAmount)) 
      { 
       int rotationParam = Integer.parseInt(rotationAmount); 
       switch (rotationParam) 
       { 
        case ExifInterface.ORIENTATION_NORMAL: 
         return 0; 
        case ExifInterface.ORIENTATION_ROTATE_90: 
         return 90; 
        case ExifInterface.ORIENTATION_ROTATE_180: 
         return 180; 
        case ExifInterface.ORIENTATION_ROTATE_270: 
         return 270; 
        default: 
         return 0; 
       } 
      } 
      else 
      { 
       return 0; 
      } 
     } 
     catch (Exception ex) 
     { 
      return 0; 
     } 
    } 
} 

在图库中选择的图像被检查是否为纵向或横向类型,并被旋转并保存在SD卡中的新路径中。为了避免OOM问题被重新调整。

+0

但是这不适用于从相机捕捉和使用图像,如果有人来知道请将其张贴在这里... –