2012-11-22 49 views
4

照片旋转90度,同时从三星移动照相机捕获其他手机的其余手机其工作正常。请帮助我。照片旋转90度,同时在一些手机中捕获

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, IMAGE_CAPTURE); 

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

       Uri contentUri = data.getData(); 
       if(contentUri!=null) 
       { 
        String[] proj = { MediaStore.Images.Media.DATA };   
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);   
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);   
        cursor.moveToFirst();   
        imageUri = Uri.parse(cursor.getString(column_index)); 
       } 

       tempBitmap = (Bitmap) data.getExtras().get("data"); 
       mainImageView.setImageBitmap(tempBitmap); 
       isCaptureFromCamera = true; 
      } 
     } 

回答

11

这恰好是早期版本的Android中的一个错误。

我解决了这个问题,只是获取方向角并相应地旋转位图。

public Bitmap decodeFile(String path) {//you can provide file path here 
    int orientation; 
    try { 
     if (path == null) { 
      return null; 
     } 
     // decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     // Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE = 70; 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 0; 
     while (true) { 
      if (width_tmp/2 < REQUIRED_SIZE 
        || height_tmp/2 < REQUIRED_SIZE) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
     scale++; 
     } 
     // decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     Bitmap bm = BitmapFactory.decodeFile(path, o2); 
     Bitmap bitmap = bm; 

     ExifInterface exif = new ExifInterface(path); 

     orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 

     Log.e("ExifInteface .........", "rotation ="+orientation); 

     //exif.setAttribute(ExifInterface.ORIENTATION_ROTATE_90, 90); 

     Log.e("orientation", "" + orientation); 
     Matrix m = new Matrix(); 

     if ((orientation == ExifInterface.ORIENTATION_ROTATE_180)) { 
      m.postRotate(180); 
      //m.postScale((float) bm.getWidth(), (float) bm.getHeight()); 
      // if(m.preRotate(90)){ 
      Log.e("in orientation", "" + orientation); 
      bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), m, true); 
      return bitmap; 
     } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { 
      m.postRotate(90); 
      Log.e("in orientation", "" + orientation); 
      bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), m, true); 
      return bitmap; 
     } 
     else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { 
      m.postRotate(270); 
      Log.e("in orientation", "" + orientation); 
      bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), m, true); 
      return bitmap; 
     } 
     return bitmap; 
    } catch (Exception e) { 
     return null; 
    } 
} 
+0

如果有人会用你的解决方案,我只是想提一提,我认为你以错误的方式来计算比例。正如你在你的代码评论中所说的那样,如果这个规模是2的幂,那么它会更好。像你一样增加一个规模不会向我们承诺它将是两个幂。如果你想得到接近正确的所需宽度,你应该在每次迭代中将比例乘以2。 – MikeL

0

还查询MediaStore.Images.Media.ORIENTATION值以获取旋转角度。然后你可以自己旋转图像或任何东西。

+0

它的工作良好的一些手机,但它不工作在一些供应商的手机我怎么能认识到这一点,我应该在哪里旋转图像 –

+0

如果供应商没有正确实施他们的代码,我猜你没有什么可以做... – Neil

0

我在做什么: 首先检查通过使用它的元数据信息的相机所拍摄图像的方向,如果我们发现这个人像那么就需要通过旋转90°的图像,并显示否则只能显示。

为了获得关于图像方向的信息,我们可以使用ExifInterface。 就是这样!

+0

你在这里重复的同样的答案,没有更多的意义,即使是在我的答案更好的解释 –