2012-07-26 58 views
13

我写了一个自定义相机活动来处理一些问题,我一直在使用某些安卓设备时调用意图图像捕获。用户可以选择保存图像,也可以使用从OnPictureTakenCallback返回的数据。Android定位问题与自定义相机活动

我遇到的问题是正确显示图像的方向。我通过致电SetRequestedOrientation强制活动以纵向显示。

如何知道用户拍摄照片时相机的正确方向? 即,用户可以以90(人像)的旋转拍摄照片。

我试图在窗口管理器的默认显示屏上使用getRotation(),但是将请求的方向设置为纵向,只返回Surface.ROTATION_0

更新: 为了澄清我的其他问题,我怎么能确定从刚刚byte[]数据,如果用户不保存图像画面回调的方向​​是什么?

更新:使用此代码尝试下面的答案后,我得到的是ExifInterface.ORIENTATION_NORMAL。我也改变了我的代码,以保存从相机返回的文件,因为我不确定是否有简单的方法来确定只有byte[]数据的方向。

private PictureCallback mPicture = new PictureCallback() 
    { 
     @Override 
     public void onPictureTaken(byte[] data, Camera camera) 
     { 
      File directory = new File(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES), 
        "MyApp"); 
      if(!directory.exists()) 
      { 
       if(!directory.mkdirs()) 
       { 
        Log.d("CAMERA", "Unable to create directory to save photos."); 
        return; 
       } 
      } 
      File file = new File(directory.getPath() + file.separator + "IMG_" + SimpleDateFormat.getDateTimeInstance().toString() + ".jpg"); 
      FileOutputStream fos = new FileOutputStream(file); 
      fos.write(data); 
      fos.close(); 
      ExifInterface exif = new ExifInterface(file.getCanonicalPath()); 
      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
      int rotate = 0; 

      switch (orientation) 
      { 
       case ExifInterface.ORIENTATION_ROTATE_90: 
        rotate = 90; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_180: 
        rotate = 180; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_270: 
        rotate = 270; 
        break; 
       case default: 
        break; 
      } 
     } 
    }; 
+0

任何一个可以帮助http://stackoverflow.com/questions/28379130/how设置相机图像方向 – 2015-02-07 13:28:37

回答

15

所以,你所面临的一些问题,与相机的方向。

此链接显示一个简单的摄像头采集活动的一个示例应用程序: http://labs.makemachine.net/2010/03/simple-android-photo-capture/

也许你应该尝试做这样的事情固定方向:

  ExifInterface exif = new ExifInterface(_path); 
      int exifOrientation = exif.getAttributeInt(
      ExifInterface.TAG_ORIENTATION, 
      ExifInterface.ORIENTATION_NORMAL); 

      int rotate = 0; 

      switch (exifOrientation) { 
      case ExifInterface.ORIENTATION_ROTATE_90: 
      rotate = 90; 
      break; 

     case ExifInterface.ORIENTATION_ROTATE_180: 
     rotate = 180; 
     break; 

     case ExifInterface.ORIENTATION_ROTATE_270: 
     rotate = 270; 
     break; 
     } 

      if (rotate != 0) { 
      int w = bitmap.getWidth(); 
      int h = bitmap.getHeight(); 

// Setting pre rotate 
      Matrix mtx = new Matrix(); 
      mtx.preRotate(rotate); 

     // Rotating Bitmap & convert to ARGB_8888, required by tess 
     bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false); 
     bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 
     } 
+0

似乎为我工作..再次申请: http://labs.makemachine.net/2010/03/simple-android-photo-capture/ – Android2390 2012-07-26 18:59:39

+0

我已更新我的问题回答你的回答。 – Bryan 2012-07-26 20:08:20

+1

我已经使用这个代码在picturecalback方法中旋转图片,但一些它不工作,你可以帮我,我会投票给你的帮助 – Nitin 2013-08-05 13:05:56

4

您需要从原始JPEG中读取元数据以验证照片的拍摄方向。

ExifInterface exif = new ExifInterface(SourceFileName); 
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION); 

来源:How to determine orientation of picture without ExifInterface?

编辑:在回答你的编辑,你尝试过使用getCameraInfo()方法,可在回调传递的Camera对象?它有你需要的信息吗?

来源:http://developer.android.com/reference/android/hardware/Camera.html

+0

您应该可以在CameraInfo上调用“方向”。 http://developer.android.com/reference/android/hardware/Camera.CameraInfo。html – bsempe 2012-07-26 18:15:56

+0

根据文档,这是从API级别9开始可用的,即Android 2.3。我的应用程序是最低API级别8. – Bryan 2012-07-26 18:53:10

3

卸下setRequestedOrientation()允许getWindowManager().getDefaultDisplay().getRotation()给正确的旋转。我想设置请求的方向可防止活动在配置发生变化时重新绘制,因此设备不知道任何类型的轮换已更改。我唯一的问题是现在从横向模式在0度方向切换到横向模式180度的旋转不火的:

@Override 
public void onConfigurationChanged(Configuration newconfig) 
{ 
    super.onConfigurationChanged(newconfig); 

} 
3
public void surfaceCreated(SurfaceHolder holder) { 
    try { 
     mCamera.setPreviewDisplay(holder); 
     Camera.Parameters parameters = mCamera.getParameters(); 
     if (this.getResources().getConfiguration().orientation != 
       Configuration.ORIENTATION_LANDSCAPE) 
     { 
      parameters.set("orientation", "portrait"); <----THis gets the job done!!! 
      // For Android Version 2.2 and above 
      mCamera.setDisplayOrientation(90); 
      // For Android Version 2.0 and above 
      parameters.setRotation(90); 
     } 


     // End Effects for Android Version 2.0 and higher 
     mCamera.setParameters(parameters); 
    } 
    catch (IOException exception) 
    { 
     mCamera.release(); 
    } 

}