Nexus 手机
原生Android系统
Android 8.0
问题
测试发现在Nexus手机,android8.0手机上发现,扫码的时候显示的拍照预览方向不正,有180度的旋转并且变形。 但是在其他手机及系统上测试是没有问题的。
解决
还是依旧Stack Overflow啊。 亲测完美解决此问题。
private void surfaceIsChanged() {
if (mHolder.getSurface() == null) {
System.out.println("getSurface,nullnull");
return;
}
try {
mCamera.stopPreview();
} catch (Exception e) {
}
try {
Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
int dataBufferSize = (int) (previewSize.height * previewSize.width * (ImageFormat.getBitsPerPixel(mCamera.getParameters()
.getPreviewFormat()) / 8.0));
mCamera.addCallbackBuffer(new byte[dataBufferSize]);
mCamera.setPreviewDisplay(mHolder);
mCamera.setPreviewCallbackWithBuffer(previewCallback);
mCamera.startPreview();
mCamera.autoFocus(autoFocusCallback);
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int camIdx = 0; // DO your logic to get front or back camera...or loop through all avaialable.
Camera.getCameraInfo(camIdx, cameraInfo);
try {
// If using back camera then simply rotate what CameraInfo tells you.
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK)
mCamera.setDisplayOrientation(cameraInfo.orientation);
else
// If using front camera note that image might be flipped to give users the impresion the are looking at a mirror.
mCamera.setDisplayOrientation( (360 - cameraInfo.orientation) % 360);
} catch (Exception e) {
e.printStackTrace();
}
// Toast.makeText(QRZbarActivity.this, "开始扫描",
// Toast.LENGTH_SHORT).show();
autoOpenLight();
} catch (Exception e) {
Toast.makeText(BaseScanActivity.this, R.string.account_toast_not_open_camera, Toast.LENGTH_SHORT).show();
// showTip("您未允许" + getResources().getString(R.string.app_name) + "访问您的相册\n请在“安全中心 -授权管理”中更改设置");
Log.d("DBG", "Error starting camera preview: " + e.getMessage());
}
}
其中主要生效的代码是:
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int camIdx = 0; // DO your logic to get front or back camera...or loop through all avaialable.
Camera.getCameraInfo(camIdx, cameraInfo);
try {
// If using back camera then simply rotate what CameraInfo tells you.
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK)
mCamera.setDisplayOrientation(cameraInfo.orientation);
else
// If using front camera note that image might be flipped to give users the impresion the are looking at a mirror.
mCamera.setDisplayOrientation( (360 - cameraInfo.orientation) % 360);
} catch (Exception e) {
e.printStackTrace();
}
根据照相的内容进行设置显示方向。
参考:解决方法
// END 2018年4月19日