2017-04-18 93 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

相關問題