Android 用前置摄像头拍摄的照片上下颠倒
我在我的项目中有自定义相机实现。 当我使用设备的后置摄像头时,它工作正常。 在纵向模式或反向纵向模式下使用前摄像头拍摄图片时,图像会上下颠倒。在横向模式下,两个摄像头都可以正常工作 我已经实现了Android 用前置摄像头拍摄的照片上下颠倒,android,android-camera,Android,Android Camera,我在我的项目中有自定义相机实现。 当我使用设备的后置摄像头时,它工作正常。 在纵向模式或反向纵向模式下使用前摄像头拍摄图片时,图像会上下颠倒。在横向模式下,两个摄像头都可以正常工作 我已经实现了SensorEventListener并在SensorChanged回调中计算orientation值,以便在捕获的文件中设置ExifInterface.TAG\u orientation public void onSensorChanged(SensorEvent event) { s
SensorEventListener
并在SensorChanged
回调中计算orientation
值,以便在捕获的文件中设置ExifInterface.TAG\u orientation
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
if (event.values[0] < 4 && event.values[0] > -4) {
if (event.values[1] > 0 && orientation != ExifInterface.ORIENTATION_ROTATE_90) {
// UP
orientation = ExifInterface.ORIENTATION_ROTATE_90;
} else if (event.values[1] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_270) {
// UP SIDE DOWN
orientation = ExifInterface.ORIENTATION_ROTATE_270;
}
} else if (event.values[1] < 4 && event.values[1] > -4) {
if (event.values[0] > 0 && orientation != ExifInterface.ORIENTATION_NORMAL) {
// LEFT
orientation = ExifInterface.ORIENTATION_NORMAL;
} else if (event.values[0] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_180) {
// RIGHT
orientation = ExifInterface.ORIENTATION_ROTATE_180;
}
}
}
}
}
对于调试,当Iexif.setAttribute()
将orientation
值设置为ExifInterface.orientation\u ROTATE\u 90
时,如果是ExifInterface.orientation\u ROTATE\u 270
,反之亦然,则工作正常。
我的问题是什么是解决这个问题的最好和可靠的方法,以便它最适合所有设备
注意:
我没有在整个过程中使用位图。我正在将数据
写入一个文件,并将其发送回另一个活动以显示捕获的图像
非常感谢您的帮助。我不久前遇到了这个问题。我所做的是旋转图像,在使用前透镜相机时,注意设备的负旋转。因此,要转换旋转,必须执行以下操作:
//check first in which camera we are
public boolean isFrontCamera(int cameraIndex) {
try {
if (mDeviceManager != null) {
CameraCharacteristics characteristics = mDeviceManager.getCameraManager().getCameraCharacteristics(String.valueOf(cameraIndex));
return characteristics.get(CameraCharacteristics.LENS_FACING)
== CameraCharacteristics.LENS_FACING_FRONT;
}
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
return false;
}
boolean mirror = isFrontCamera(Integer.parseInt(mCameraId));
if (mirror) {
//Mirror image rotation is equal to the negative rotation of the device, but some external apps are not able to read negative numbers
//in image details, we do this to prevent the crash of this apps
rotation = 360 - rotation;
if (rotation == 360) {
rotation = 0;
}
}
public static Bitmap rotateImage(Bitmap imageToOrient, int degreesToRotate) {
Bitmap result = imageToOrient;
try {
if (degreesToRotate != 0) {
Matrix matrix = new Matrix();
matrix.setRotate(degreesToRotate);
result = Bitmap.createBitmap(
imageToOrient,
0,
0,
imageToOrient.getWidth(),
imageToOrient.getHeight(),
matrix,
true);
}
} catch (Exception e) {
if (Log.isLoggable(TAG, Log.ERROR)) {
Log.e(TAG, "Exception when trying to orient image", e);
}
}
return result;
}
稍后,在将图像添加到视图之前必须旋转图像,可以使用如下矩阵:
//check first in which camera we are
public boolean isFrontCamera(int cameraIndex) {
try {
if (mDeviceManager != null) {
CameraCharacteristics characteristics = mDeviceManager.getCameraManager().getCameraCharacteristics(String.valueOf(cameraIndex));
return characteristics.get(CameraCharacteristics.LENS_FACING)
== CameraCharacteristics.LENS_FACING_FRONT;
}
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
return false;
}
boolean mirror = isFrontCamera(Integer.parseInt(mCameraId));
if (mirror) {
//Mirror image rotation is equal to the negative rotation of the device, but some external apps are not able to read negative numbers
//in image details, we do this to prevent the crash of this apps
rotation = 360 - rotation;
if (rotation == 360) {
rotation = 0;
}
}
public static Bitmap rotateImage(Bitmap imageToOrient, int degreesToRotate) {
Bitmap result = imageToOrient;
try {
if (degreesToRotate != 0) {
Matrix matrix = new Matrix();
matrix.setRotate(degreesToRotate);
result = Bitmap.createBitmap(
imageToOrient,
0,
0,
imageToOrient.getWidth(),
imageToOrient.getHeight(),
matrix,
true);
}
} catch (Exception e) {
if (Log.isLoggable(TAG, Log.ERROR)) {
Log.e(TAG, "Exception when trying to orient image", e);
}
}
return result;
}
我知道您没有使用位图,但是您可以修改方法以发送byteArray或任何您需要的内容
希望它能对你有所帮助!
致以最良好的祝愿