我渲染camera preview
上GLSurfaceView
一切工作正常,但camera preview
显示倒挂一些设备专门在Nexus 5X上。我已经检查这个解决方案Android - Camera preview is sideways。这是我处理问题的方向代码
private void setCameraDisplayOrientation() {
Constants.debugLog(TAG_DEBUG, "setCameraDisplayOrientation");
Constants.debugLog(TAG, "setCameraDisplayOrientation ");
if (camera == null) {
Constants.debugLog(TAG + " Owncamera", "setCameraDisplayOrientation - camera null");
return;
}
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(currentCameraId, info);
WindowManager winManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int rotation = winManager.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;
}
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
this.cameraOrientation = 2;
} else if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
this.cameraOrientation = 1;
}
Constants.debugLog(TAG_DEBUG, "result: " + result + " this.cameraOrientation == " + this.cameraOrientation);
Constants.debugLog(TAG, "result: " + result + " this.cameraOrientation == " + this.cameraOrientation);
//Log.e("camera_orient", "res: "+ result);
camera.setDisplayOrientation(result);
}
从Camera.CameraInfo info
类,我可以得到orientation
。为了获得最大的设备我得到portrait mode
的价值是“for front camera = 270, back camera = 90
‘’但Nexus 5X
它提供camera orientation
270270两个正面和背面摄像头。除了在其他设备上我的相机在人像模式,但对于result
的正面和背面摄像头提供Nexus 5x
值90前置摄像头90后置摄像头270我也通过设置camera.setDisplayOrientation(90);
Nexus 5X保护装置固定90值尝试,但不工作如何处理这个问题,使所有设备摄像头的方向将是相同的,我不会得到任何旋转的图像? ?
尝试
1.首先,添加方法(WriteFile的,processImage来和getCorrectCameraOrientation)如下所定义
拍摄照片和更新onPictureTaken之后2.计算相机方向**
@Override
public void onPictureTaken (final byte[] data, final Camera camera) {
int rotationAngle = getCorrectCameraOrientation (this, info);
3.创建文件,并使用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;
}
问题原因:默认情况下,最大的设备在使用相机API 1.返回与90度的默认方向的图像我在用照相机API 2的情况下,我不知道这不过在Nexus 5X保护和一些罕见的设备的情况下(利用摄像头API 1),它返回图像具有270度默认的旋转这是一些特定的模型装置上的特殊情况。所以你要添加的180度的Nexus 5X的另外旋转。请检查印刷的Nexus 5X的形象定位的日志。