2017-04-18 94 views
2

我正在创建Android相机应用程序。这是一个使用camera API的自定义相机。
每当我从相机拍摄照片,它总是保存在景观模式。
我正在使用此方法设置相机方向。在Android中定位从相机拍摄的图像

public static void setCameraDisplayOrientation(Activity activity, 
               int cameraId, android.hardware.Camera camera) { 
    android.hardware.Camera.CameraInfo info = 
      new android.hardware.Camera.CameraInfo(); 
    android.hardware.Camera.getCameraInfo(cameraId, info); 
    int rotation = activity.getWindowManager().getDefaultDisplay() 
      .getRotation(); 
    int degrees = 0; 
    switch (rotation) { 
     case Surface.ROTATION_0: degrees = 0; break; 
     case Surface.ROTATION_90: degrees = 90; break; 
     case Surface.ROTATION_180: degrees = 180; break; 
     case Surface.ROTATION_270: degrees = 270; break; 
    } 

    int result; 
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
     result = (info.orientation + degrees) % 360; 
     result = (360 - result) % 360; // compensate the mirror 
    } else { // back-facing 
     result = (info.orientation - degrees + 360) % 360; 
    } 
    camera.setDisplayOrientation(result); 
} 

这里是负责存储图像的代码。

PictureCallback jpegCallback = new PictureCallback() { 
    public void onPictureTaken(byte[] data, Camera camera) { 
     File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); 
     if (pictureFile == null){ 
      Log.d(TAG, "Error creating media file, check storage permissions: "); 
      return; 
     } 

     try { 
      FileOutputStream fos = new FileOutputStream(pictureFile); 
      fos.write(data); 
      fos.close(); 
     } catch (FileNotFoundException e) { 
      Log.d(TAG, "File not found: " + e.getMessage()); 
     } catch (IOException e) { 
      Log.d(TAG, "Error accessing file: " + e.getMessage()); 
     } 
    } 
    }; 

此代码成功保存图像,但始终处于横向模式。我在想什么我是Android新手。我想要像预览中显示的那样保存图像。

更新2(根据答案)

 PictureCallback jpegCallback = new PictureCallback() { 
    public void onPictureTaken(byte[] data, Camera camera) { 

     Camera.CameraInfo info = new Camera.CameraInfo(); 

     int rotationAngle = getCorrectCameraOrientation (MainActivity.this, info); 
    Toast.makeText(MainActivity.this, "Angle "+ rotationAngle, Toast.LENGTH_SHORT).show(); 
     File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); 

     try { 
      writeFile (data, pictureFile); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     processImage (pictureFile, rotationAngle, 1); 
    } 
    }; 

我使用的代码以这种方式,但这不是工作仍在同一个问题的图像始终处于横向模式。

+0

请参阅下面我的回答 http://stackoverflow.com/questions/43447987/android-camera-provide-upside-down-data-on-back-camera-in-some-devices/43449660#43449660 – Pehlaj

+0

为什么update2代码会改变吗?您仍在保存数据数组。并且不要在相机信息或方向上做任何事情。 – greenapps

回答

1

尝试

1.首先,添加以下定义的方法(WriteFile的,processImage来和getCorrectCameraOrientation)捕获照片和更新后onPictureTaken

2.计算照相机取向(步骤3)之后**

@Override 
public void onPictureTaken (final byte[] data, final Camera camera) { 

    int rotationAngle = getCorrectCameraOrientation (this, info); 

创建文件和更新照片角度ü唱rotationAngle

File file = new File (folder, fileName); 

try { 
    file.createNewFile(); 
} 
catch (IOException e) { 
    e.printStackTrace(); 
} 

writeFile (data, file); 
processImage (file, rotationAngle, compressRatio); 

WriteFile的

public static void writeFile (byte[] data, File file) throws IOException { 

    BufferedOutputStream bos = null; 

    try { 
     FileOutputStream fos = new FileOutputStream (file); 
     bos = new BufferedOutputStream (fos); 
     bos.write (data); 
    } 
    finally { 
     if (bos != null) { 
      try { 
       bos.flush(); 
       bos.close(); 
      } 
      catch (Exception e) { 
      } 
     } 
    } 
} 

processImage来

public static void processImage (File file, int rotationAngle, int compressionRatio) { 

    BufferedOutputStream bos = null; 

    try { 

     Bitmap bmp = BitmapFactory.decodeFile (file.getPath()); 

     Matrix matrix = new Matrix(); 
     matrix.postRotate (rotationAngle); 

     bmp = Bitmap.createBitmap (bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); 

     FileOutputStream fos = new FileOutputStream (file); 
     bmp.compress (Bitmap.CompressFormat.PNG, compressionRatio, fos); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
    catch (OutOfMemoryError t) { 
     t.printStackTrace(); 
    } 
    catch (Throwable t) { 
     t.printStackTrace(); 
    } 
    finally { 
     if (bos != null) { 
      try { 
       bos.flush(); 
       bos.close(); 
      } 
      catch (Exception e) { 
      } 
     } 
    } 
} 

getCorrectCameraOrientation

public static int getCorrectCameraOrientation (Activity activity, Camera.CameraInfo info) { 

    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); 
    int degrees = 0; 

    if (hasValidRotation (rotation)) { 
     degrees = rotation * 90; 
    } 

    int result; 
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
     result = (info.orientation + degrees) % 360; 
     result = (360 - result) % 360; 
    } 
    else { 
     result = (info.orientation - degrees + 360) % 360; 
    } 

    return result; 
} 
+0

根据你的回答更新我的问题,但这不起作用仍然是同样的问题。我不认识'processImage'是如何工作的。您是先保存文件然后处理图像? –

+0

是的,我将字节数据保存到本地文件,然后在保存的文件上应用旋转角度。使用本地保存的图像文件。 – Pehlaj

+0

此代码已在多个设备上完全测试,我们的应用程序在Play商店中生效。您将在不同的设备上面对不同的方向问题,但这会解决您的问题 – Pehlaj

相关问题