gpt4 book ai didi

android - Camera Source (Google Mobile Vision) 在某些设备上返回旋转图像

转载 作者:太空狗 更新时间:2023-10-29 13:05:31 27 4
gpt4 key购买 nike

我有 Google Mobile Vision 的开源代码 - CameraSource,这是我点击照片时调用的方法:cameraSource.takePicture();

在CameraSource.java的开源版本中,确定屏幕方向的方法是stock one:

private void setRotation(Camera camera, Camera.Parameters parameters, int cameraId) {
WindowManager windowManager =
(WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
int degrees = 0;
int rotation = windowManager.getDefaultDisplay().getRotation();
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;
default:
Log.e(TAG, "Bad rotation value: " + rotation);
}

CameraInfo cameraInfo = new CameraInfo();
Camera.getCameraInfo(cameraId, cameraInfo);

int angle;
int displayAngle;
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
angle = (cameraInfo.orientation + degrees) % 360;
displayAngle = (360 - angle) % 360; // compensate for it being mirrored
} else { // back-facing
angle = (cameraInfo.orientation - degrees + 360) % 360;
displayAngle = angle;
}

// This corresponds to the rotation constants in {@link Frame}.
mRotation = angle / 90;

camera.setDisplayOrientation(displayAngle);
parameters.setRotation(angle);
}

这里的displayAngle和angle对于三星、联想和Yuntab H8都是一样的。但是为 backCamera 返回的位图在每个设备中的旋转方式不同。我必须为每个设备手动旋转位图(三星:90,联想:0 和 Yuntab:180)

我的要求是 onPictureTaken 应该返回一个与当前显示方向匹配的位图。我很长时间以来一直在研究这个问题,但仍然必须想办法解决这个问题。下面是我的 onPicturetaken() (拍照后调用):

  @Override
public void onPictureTaken(byte[] bytes) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, currentCameraId == 0 ? options : null);
}catch (Exception ex){
ex.printStackTrace();
Log.e("PictureTaken",ex.toString());
}
};

最佳答案

当图像已保存在设备中时,您应该旋转图像。

然后您可以旋转它以匹配拍摄照片时的位置。

示例代码(它可能需要一些清理和改进,但它可以工作...):

计算图像旋转的方法:

private static int rotationNeeded(String path) {
try {
File file = new File(path);

if (!file.getName().contains(".jpg")) {
return 0;
}

ExifInterface exif = new ExifInterface(file.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270;
}

if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
}

if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}

对图像应用所需的旋转:

public static void rotateImage(String filePath) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);

//check image rotation
int rotate = rotationNeeded(filePath);
if (rotate != 0) {
//rotate image if needed
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
Bitmap rotatedImage = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);

bitmap.recycle();
bitmap = rotatedImage;

//save image
byte[] dataPicture = bao.toByteArray();
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(dataPicture);
fos.flush();
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

关于android - Camera Source (Google Mobile Vision) 在某些设备上返回旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48502055/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com