我正在創建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);
}
};
我使用的代碼以這種方式,但這不是工作仍在同一個問題的圖像始終處於橫向模式。
請參閱下面我的回答 http://stackoverflow.com/questions/43447987/android-camera-provide-upside-down-data-on-back-camera-in-some-devices/43449660#43449660 – Pehlaj
爲什麼update2代碼會改變嗎?您仍在保存數據數組。並且不要在相機信息或方向上做任何事情。 – greenapps