1

我正在开发一个使用android原生相机的应用程序。我可以成功地拍照并存储并显示在ImageView上。我正在测试HTC欲望Z,Nexus S和摩托罗拉Zoom。它适用于除Nexus S上的一个问题以外的所有三种设备。使用Nexus S拍摄照片时,预览图像已旋转90度Android SDK Nexus S原生相机问题

您可以让我知道如何解决这个问题吗?

我的代码:从Problems saving a photo to a file

非常感谢

请人

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.camera_preview); 
     imgTakenPhoto = (ImageView) findViewById(R.id.imageView); 
     getPhotoClick(); 
    } 
    private File getTempFile() 
    { 
     return new File(Environment.getExternalStorageDirectory(), "image.tmp"); 
    } 

    private void getPhotoClick() 
    { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile())); 
     startActivityForResult(intent, REQUEST_FROM_CAMERA); 
    } 

    InputStream is=null; 

    @Override 
    protected void onActivityResult(int requestcode, int resultCode, Intent data){ 
     if (requestcode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK) { 

      File file=getTempFile(); 

      try { 
       is=new FileInputStream(file); 

      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 
      if(is==null){ 

       try { 
        Uri u = data.getData(); 
        is=getContentResolver().openInputStream(u); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 
      } 

      Bitmap thumbnail = BitmapFactory.decodeStream(is); 
      imgTakenPhoto.setImageBitmap(thumbnail); 

      } 
    } 

我已经采取了帮助?

回答

1

在某些电话上发生预览时出现此问题。唯一的简单的解决方法,我知道是设置风景模式中的onCreate():

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

另一个(可能是矫枉过正)可能是覆盖在预览表面的顶部另一面,创建自定义PreviewCallback和旋转并绘制每一帧手动到这个表面,但是在性能和​​屏幕尺寸方面存在问题。

+0

如果您创建您自己的相机界面,这将起作用。如果您使用本机相机,则不起作用。我找到了一个解决方案。我会在稍后发布。 干杯 – Chinthaka

+0

@Chinthaka有没有可以在这里发布的解决方案? – Swaroop

0

以下功能将检测图像的方向。

public static int getExifOrientation(String filepath) { 
     int degree = 0; 
     ExifInterface exif = null; 
     try { 
      exif = new ExifInterface(filepath); 
     } catch (IOException ex) { 
      Log.e(TAG, "cannot read exif", ex); 
     } 
     if (exif != null) { 
      int orientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, -1); 
      if (orientation != -1) {      
       switch(orientation) { 
        case ExifInterface.ORIENTATION_ROTATE_90: 
         degree = 90; 
         break; 
        case ExifInterface.ORIENTATION_ROTATE_180: 
         degree = 180; 
         break; 
        case ExifInterface.ORIENTATION_ROTATE_270: 
         degree = 270; 
         break; 
      } 
    } 
} 

在您的代码上,使用此函数旋转位图图像。的Util类可以https://android.googlesource.com/platform/packages/apps/Camera.git

if(degree != 0){ 
    bmp = Util.rotate(bmp, degree); 
} 

找到记住这只会校正图像的预览的方向。 我希望这会帮助你。