如何防止Android中的摄像头应用程序将方向更改为横向?
我已经通过我的应用程序使用相机本机应用程序拍照。我使用下面的代码仅在纵向模式下显示相机应用程序如何防止Android中的摄像头应用程序将方向更改为横向?,android,android-camera,Android,Android Camera,我已经通过我的应用程序使用相机本机应用程序拍照。我使用下面的代码仅在纵向模式下显示相机应用程序 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); takePictureIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATI
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
takePictureIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
startActivityForResult(takePictureIntent, actionCode);
但是,它与上面的代码不兼容
任何建议都将不胜感激:-)您无法控制启动的外部应用程序的方向,因此无法做到这一点。
但是您可以创建自己的相机活动。将您的mCamera视为您的相机,您可以通过添加
mCamera.setDisplayOrientation(90)来创建相机活动、设置cameraPReview并在protrait模式下设置预览代码>开始预览之前
以下是potrait模式下FrameLayout中的摄影机预览示例:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
if(mCamera!=null){
mCamera.setPreviewDisplay(holder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();}
} catch (IOException e) {
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
}
}
您可以从MainActivity设置预览,如下所示:
mPreview = new CameraPreview(getApplicationContext(), camera);
preview.addView(mPreview);
请通过android开发者链接:
默认情况下,摄影机预览的方向为横向。请检查以下内容:
注意:相机预览不必处于横向模式。启动
在Android 2.2(API级别8)中,可以使用setDisplayOrientation()命令
方法来设置预览图像的旋转。为了改变
在用户重新定向手机时,在
方法,首先停止预览
使用Camera.stopReview()更改方向,然后启动
使用Camera.startPreview()再次预览
您还可以参考以了解更多信息
也可以在录制视频时调用mediaRecorder.setOrientionHint(rotation)
另外,如果要使相机图像以与显示器相同的方向显示,可以使用以下代码
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
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;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
它从API级别14开始工作,当预览处于活动状态时可以调用此方法谢谢@PratibhaS!这是你为定制相机建议的代码,对吗?但我想使用本机摄像头应用程序。在本机中,无法更改方向。。相反,你必须选择定制相机。已经在android开发者官方网站上声明谢谢@E.Abdel!这是你为定制相机建议的代码,对吗?但我想使用本机摄像头应用程序。你不能更改其他应用程序的配置,请尝试创建自己的摄像头活动。这很简单