gpt4 book ai didi

Android 前置摄像头拍摄倒置照片

转载 作者:IT王子 更新时间:2023-10-28 23:35:41 29 4
gpt4 key购买 nike

我有这个以纵向模式运行的应用程序,作为一个 Activity 的一部分,我有一个相机对象作为 fragment 运行。

我可以选择从前置摄像头切换到后置摄像头,使用后置摄像头拍照时一切正常。

不过,当我使用前置摄像头拍照时,它们会反转 180 度。现在我知道这可能与处于纵向模式的方向有关,但是将其置于横向模式只会扼杀我的应用程序的想法。

是否可以解决此问题,使拍摄的照片与您在预览中看到的相同?

        listener = new OrientationEventListener(this.getActivity(),SensorManager.SENSOR_DELAY_NORMAL){

@Override
public void onOrientationChanged(int orientation) {
// TODO Auto-generated method stub
if (orientation == ORIENTATION_UNKNOWN) return;
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(mCurrentCamera, info);
orientation = (orientation + 45) / 90 * 90;
int rotation = 0;
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
rotation = (info.orientation - orientation + 360) % 360;
} else { // back-facing camera
rotation = (info.orientation + orientation) % 360;
}
if (mCamera.getParameters() != null){
Camera.Parameters mParameters = mCamera.getParameters();

mParameters.setRotation(rotation);
mCamera.setParameters(mParameters);
}
}

};
listener.enable();
if (listener.canDetectOrientation())
Log.d("Orientation Possible", "Orientation Possible");

问题是,在我尝试拍照后,这个东西崩溃了。此外,如果它仅在预览模式下运行一段时间,它会再次崩溃。我还应该补充一点,我正在用另一种方法这样做。

   public void switchCamera(Camera camera) {
setCamera(camera);
try {
camera.setPreviewDisplay(mHolder);
} catch (IOException exception) {
Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
}
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);

requestLayout();


try{
camera.setParameters(parameters);
}
catch (RuntimeException ex){
Log.d("Preview", "Failure to set proper parameters");
//need to improve this method
if (mSupportedPreviewSizes != null) {
Camera.Size cs = mSupportedPreviewSizes.get(0);
parameters.setPreviewSize(cs.width, cs.height);
camera.setParameters(parameters);
requestLayout();
//int tempheight = mPreviewSize.height;
//mPreviewSize.height = mPreviewSize.width;
//mPreviewSize.width = tempheight;

}
}

当您在相机之间切换时调用此方法(反之亦然)。这会干扰方向事件监听器吗?

另外,当保存拍摄的照片时,这就是我所说的。在它只是将数据作为参数之前,但我试图让它也采用屏幕方向。问题是屏幕方向始终是 90,无论如何。所以位图将始终旋转 90 度(这对于使用后置摄像头拍照非常有用)但在使用前置摄像头拍摄时会反转它。可以在这个函数中实现修复吗?

  public static Bitmap MakeSquare(byte[] data, int orientation) {
int width;
int height;
// Rotate photo
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
// Convert ByteArray to Bitmap
Bitmap bitPic = BitmapFactory.decodeByteArray(data, 0, data.length);
width = bitPic.getWidth();
height = bitPic.getHeight();


// Create new Bitmap out of the old one
Bitmap bitPicFinal = Bitmap.createBitmap(bitPic, 0, 0, width, height,matrix, true);
bitPic.recycle();
int desWidth;
int desHeight;
desWidth = bitPicFinal.getWidth();
desHeight = desWidth;
Bitmap croppedBitmap = Bitmap.createBitmap(bitPicFinal, 0,bitPicFinal.getHeight() / 2 - bitPicFinal.getWidth() / 2,desWidth, desHeight);
croppedBitmap = Bitmap.createScaledBitmap(croppedBitmap, 528, 528, true);
return croppedBitmap;
}

最佳答案

好吧好吧。看来我有点照顾它了。我使用了以下建议:Android, front and back camera Orientation , Landscape

并将我的 MakeSquare 函数更改为:

public static Bitmap MakeSquare(byte[] data, int cameraID) {
int width;
int height;
Matrix matrix = new Matrix();
Camera.CameraInfo info = new Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraID, info);
// Convert ByteArray to Bitmap
Bitmap bitPic = BitmapFactory.decodeByteArray(data, 0, data.length);
width = bitPic.getWidth();
height = bitPic.getHeight();

// Perform matrix rotations/mirrors depending on camera that took the photo
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
{
float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1};
Matrix matrixMirrorY = new Matrix();
matrixMirrorY.setValues(mirrorY);

matrix.postConcat(matrixMirrorY);
}

matrix.postRotate(90);


// Create new Bitmap out of the old one
Bitmap bitPicFinal = Bitmap.createBitmap(bitPic, 0, 0, width, height,matrix, true);
bitPic.recycle();
int desWidth;
int desHeight;
desWidth = bitPicFinal.getWidth();
desHeight = desWidth;
Bitmap croppedBitmap = Bitmap.createBitmap(bitPicFinal, 0,bitPicFinal.getHeight() / 2 - bitPicFinal.getWidth() / 2,desWidth, desHeight);
croppedBitmap = Bitmap.createScaledBitmap(croppedBitmap, 528, 528, true);
return croppedBitmap;
}

这似乎奏效了。虽然我不确定这是最好的方法,但我很满意。现在我要做的就是弄清楚为什么它在使用前置摄像头时没有采用正确的纵横比。

关于Android 前置摄像头拍摄倒置照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10283467/

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