Android 前置摄像头图像被颠倒保存
Android front camera images are being saved upside down
我正在构建一个使用相机功能的 android 应用程序。我遇到的问题是我从前置摄像头获得的图像数据 (byte[]) 在我的 samsung s7 和 nexus phone 上倒过来了。它在预览中显示正确,但在我按原样保存数据然后在图库中显示图像后,它们都颠倒了。我知道我可以在保存之前翻转图像数据,但我已经在 blu C 5.0 HD 运行 4.4 (kitkat) 上测试了代码,phone 上的图像数据以正确的方式定向。所以总是翻转图像会导致其他设备上的错误。有人告诉我这个问题是因为在构建新的三星和 nexus phones 时,前置摄像头是倒置的以节省空间。我不确定这是否正确,但如果是这样,如果我翻转所有图像,它会弄乱具有正确相机方向的 phones。那么有没有办法在保存图像之前检测图像数据的方向?
这是我使用的代码:
mCamera.takePicture(null, null, mPicture);
回调:
private final Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
processCameraCallback(data);
}
};
正在处理数据:
public void processCameraCallback(byte[] data) {
confirmPhoto(true);
//Make a new empty picture file
try {
pictureFile = Utils.createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(TAG, "Failed to create photo file: " + ex.toString());
confirmPhoto(false);
return;
}
//Write the file to the storage
try {
FileOutputStream fos;
if (pictureFile != null) {
fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
}
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
confirmPhoto(false);
return;
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
confirmPhoto(false);
return;
}
}
使用此代码调整相机方向:
private int detectCameraDisplayOrientation(Activity activity,
Camera.CameraInfo 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;
}
return result;
}
这是来自摄像头 api 的示例,在 Nexus 5x 上可以正常工作。(使用倒置摄像头)
在那之后打电话
camera.setDisplayOrientation(displayOrientation);
并且它会正确保存图片。
我正在构建一个使用相机功能的 android 应用程序。我遇到的问题是我从前置摄像头获得的图像数据 (byte[]) 在我的 samsung s7 和 nexus phone 上倒过来了。它在预览中显示正确,但在我按原样保存数据然后在图库中显示图像后,它们都颠倒了。我知道我可以在保存之前翻转图像数据,但我已经在 blu C 5.0 HD 运行 4.4 (kitkat) 上测试了代码,phone 上的图像数据以正确的方式定向。所以总是翻转图像会导致其他设备上的错误。有人告诉我这个问题是因为在构建新的三星和 nexus phones 时,前置摄像头是倒置的以节省空间。我不确定这是否正确,但如果是这样,如果我翻转所有图像,它会弄乱具有正确相机方向的 phones。那么有没有办法在保存图像之前检测图像数据的方向?
这是我使用的代码:
mCamera.takePicture(null, null, mPicture);
回调:
private final Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
processCameraCallback(data);
}
};
正在处理数据:
public void processCameraCallback(byte[] data) {
confirmPhoto(true);
//Make a new empty picture file
try {
pictureFile = Utils.createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(TAG, "Failed to create photo file: " + ex.toString());
confirmPhoto(false);
return;
}
//Write the file to the storage
try {
FileOutputStream fos;
if (pictureFile != null) {
fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
}
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
confirmPhoto(false);
return;
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
confirmPhoto(false);
return;
}
}
使用此代码调整相机方向:
private int detectCameraDisplayOrientation(Activity activity,
Camera.CameraInfo 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;
}
return result;
}
这是来自摄像头 api 的示例,在 Nexus 5x 上可以正常工作。(使用倒置摄像头)
在那之后打电话
camera.setDisplayOrientation(displayOrientation);
并且它会正确保存图片。