200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > android前置摄像头拍摄 Android前置摄像头拍摄倒置照片

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

时间:2019-10-30 08:43:06

相关推荐

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

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

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

当我用前置摄像头拍照时,它们会被倒置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度(这对于使用后置摄像头拍摄照片非常有用),但在使用前置摄像头拍摄时会将其反转。 可以在此function中实现修复吗?

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,前后摄像头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; }

这似乎有效并且可以解决问题。 虽然我不确定这是最好的方式,但我很满意。 现在,我所要做的就是弄清楚为什么在使用前置摄像头时它没有采用适当的宽高比。

试试这些链接:

具体来说,这是来自setRotation链接的一大块措辞。

“如果应用程序想要旋转图片以匹配用户看到的方向,则应该使用OrientationEventListener和Camera.CameraInfo。来自OrientationEventListener的值相对于设备的自然方向.CameraInfo.orientation是相机方向和自然设备方向。两者的总和是后置摄像头的旋转角度。 两者的差异是前置摄像头的旋转角度 。请注意, 前置摄像头的JPEG图像不像预览那样镜像显示。”

我按原样使用了这段代码,根本没有修改它。 我的相机现在更好,但仍然需要一些TLC。 我还没有前面的function。

祝你好运!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。