本文介绍了禁用取向变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
再起爆自定义的摄像机,而做变化的 preVIEW模式,从横向到纵向或人像到景观,我的面类code是这样的:
previewSurface.java: -
公共类previewSurface延伸SurfaceView工具
SurfaceHolder.Callback {
公共静态最后弦乐LOG_TAG =相机preVIEW;
私人SurfaceHolder mSurfaceHolder;
私人相机mCamera;
//构造,其获得的上下文和摄像头
@燮pressWarnings(德precation)
公共previewSurface(上下文的背景下,相机摄像头){
超(上下文);
this.mCamera =摄像头;
this.mSurfaceHolder = this.getHolder();
this.mSurfaceHolder.addCallback(本);
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.mSurfaceHolder.setFixedSize(100,100);
}
@覆盖
公共无效surfaceCreated(SurfaceHolder surfaceHolder){
尝试 {
Camera.Parameters参数= mCamera.getParameters();
如果(this.getResources()。getConfiguration()。方向!= Configuration.ORIENTATION_LANDSCAPE)
{
parameters.set(方向,肖像);
mCamera.setDisplayOrientation(90);
parameters.setRotation(90);
mCamera.set previewDisplay(surfaceHolder);
mCamera.start preVIEW();
}
其他
{
//这是一个无证虽然广为人知的功能
parameters.set(方向,山水);
//为Android 2.2及以上
mCamera.setDisplayOrientation(0);
//取消注释为Android 2.0及以上
parameters.setRotation(0);
}
mCamera.set previewDisplay(surfaceHolder);
mCamera.start preVIEW();
}赶上(IOException异常E){
//留空现在
}
}
@覆盖
公共无效surfaceDestroyed(SurfaceHolder surfaceHolder){
mCamera.stop preVIEW();
mCamera.release();
}
@覆盖
公共无效surfaceChanged(SurfaceHolder surfaceHolder,INT格式,
INT宽度,高度INT){
尝试 {
Camera.Parameters参数= mCamera.getParameters();
如果(this.getResources()。getConfiguration()。方向!= Configuration.ORIENTATION_LANDSCAPE){
parameters.set(方向,肖像);
mCamera.setDisplayOrientation(90);
parameters.setRotation(90);
}
其他 {
//这是一个无证虽然广为人知的功能
parameters.set(方向,山水);
//为Android 2.2及以上
mCamera.setDisplayOrientation(0);
//取消注释为Android 2.0及以上
parameters.setRotation(0);
}
mCamera.set previewDisplay(surfaceHolder);
mCamera.start preVIEW();
}赶上(IOException异常E){
//留空现在
}
}
}
我想知道我做错了,我错过了我的code?
解决方案
您必须处理配置更改为你的应用程序。
将此行添加到您的AndroidManifest.xml。
安卓configChanges =keyboardHidden |方向|屏幕尺寸
希望它可以帮助ツ
Re-Initiating Custom Camera, while doing change in Preview modes, from Landscape to Portrait or Portrait to Landscape, my Surface class code looks like this:
PreviewSurface.java:-
public class PreviewSurface extends SurfaceView implements
SurfaceHolder.Callback {
public static final String LOG_TAG = "CameraPreview";
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
// Constructor that obtains context and camera
@SuppressWarnings("deprecation")
public PreviewSurface(Context context, Camera camera) {
super(context);
this.mCamera = camera;
this.mSurfaceHolder = this.getHolder();
this.mSurfaceHolder.addCallback(this);
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.mSurfaceHolder.setFixedSize(100, 100);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
Camera.Parameters parameters = mCamera.getParameters();
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
{
parameters.set("orientation", "portrait");
mCamera.setDisplayOrientation(90);
parameters.setRotation(90);
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
}
else
{
// This is an undocumented although widely known feature
parameters.set("orientation", "landscape");
// For Android 2.2 and above
mCamera.setDisplayOrientation(0);
// Uncomment for Android 2.0 and above
parameters.setRotation(0);
}
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
// left blank for now
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
mCamera.stopPreview();
mCamera.release();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
int width, int height) {
try {
Camera.Parameters parameters = mCamera.getParameters();
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
parameters.set("orientation", "portrait");
mCamera.setDisplayOrientation(90);
parameters.setRotation(90);
}
else {
// This is an undocumented although widely known feature
parameters.set("orientation", "landscape");
// For Android 2.2 and above
mCamera.setDisplayOrientation(0);
// Uncomment for Android 2.0 and above
parameters.setRotation(0);
}
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
// left blank for now
}
}
}
May i know where i am doing wrong, what i missed in my code ?
解决方案
You have to handle configuration change for your app .
Add this line to your AndroidManifest.xml.
android:configChanges="keyboardHidden|orientation|screenSize"
Hope it helps ツ
这篇关于禁用取向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!