在Android中镜像前置摄像头 [英] Mirror the front facing camera in Android
问题描述
在Android中使用前置摄像头拍照时,预览会沿Y轴反射,使所看到的图像看起来就像用户在照镜子一样。我想撤消此效果(应用第二次反射)或只是停止自动完成的操作。
When you take a picture with the front facing camera in Android the preview is reflected along the Y axis to make the image seen appear as if the user was looking in the mirror. I want to undo this effect (apply a second reflection) or just stop the one thats done automatically.
我虽然要使用此功能:
Camera mCamera;
....
mCamera.setPreviewCallback(...);
但是我真的不知道该如何处理
But I dont really know what to do with the overriding of
onPreviewFrame(byte[] data, Camera camera){...}
实现我所描述的最佳方法是什么?
注意尝试将这种效果应用于实时预览,而不是已拍摄的图像。
推荐答案
打开时首先使用Camera.open()您的相机实例,应该使用Camera.open(getSpecialFacingCamera())
First when you open your camera instance with Camera.open() you should open front camera with Camera.open(getSpecialFacingCamera())
private int getSpecialFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
cameraId = i;
break;
}
}
return cameraId;
}
然后在您的回调方法中,将相机数据转换为图像
您可以使用此代码使它保持正常
Then in your callback method where camera data is converting in to image you can use this code to keep it normal
public void onPictureTaken(byte[] data, Camera camera){
Bitmap newImage = null;
Bitmap cameraBitmap;
if (data != null) {
cameraBitmap = BitmapFactory.decodeByteArray(data, 0, (data != null) ? data.length : 0);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// use matrix to reverse image data and keep it normal
Matrix mtx = new Matrix();
//this will prevent mirror effect
mtx.preScale(-1.0f, 1.0f);
// Setting post rotate to 90 because image will be possibly in landscape
mtx.postRotate(90.f);
// Rotating Bitmap , create real image that we want
newImage = Bitmap.createBitmap(cameraBitmap, 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), mtx, true);
}else{// LANDSCAPE MODE
//No need to reverse width and height
newImage = Bitmap.createScaledBitmap(cameraBitmap, screenWidth, screenHeight, true);
cameraBitmap = newImage;
}
}
}
您可以在画布中传递newImage并创建jpeg图像并将其保存在设备上。
不要忘了Api 21级不建议使用相机...
you can pass newImage in canvas and create jpeg image and save it on device. Do not forgot Camera is deprecated in Api level 21...
这篇关于在Android中镜像前置摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!