本文介绍了如何修复相机的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请注意照相机(拍摄的图像不)的观点翻转至左(上图),在活动
的方向是正确的,但摄像头视图被搞砸了,请帮我家伙:)谢谢你。
下面是 XML
文件:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:重力=中心|顶
机器人:方向=垂直>
< SurfaceView
机器人:ID =@ + ID /摄像头preVIEW
机器人:layout_margin =10dp
机器人:layout_width =300dp
机器人:layout_height =300dp/>
< / LinearLayout中>
< / LinearLayout中>
< / LinearLayout中>
这里是的Java
code:
公共类CustomCameraActivity扩展活动实现SurfaceHolder.Callback {
摄像头摄像头;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
布尔previewing = FALSE;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
的setContentView(R.layout.camera);
surfaceView =(SurfaceView)findViewById(R.id.camera preVIEW);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(本);
}
@覆盖
公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,高度INT){
如果(previewing){
camera.stop preVIEW();
previewing = FALSE;
}
如果(相机!= NULL){
尝试 {
camera.set previewDisplay(surfaceHolder);
camera.start preVIEW();
previewing = TRUE;
}赶上(IOException异常E){
e.printStackTrace();
}
}
}
@覆盖
公共无效surfaceCreated(SurfaceHolder持有者){
相机= Camera.open();
}
@覆盖
公共无效surfaceDestroyed(SurfaceHolder持有者){
camera.stop preVIEW();
camera.release();
摄像头= NULL;
previewing = FALSE;
}
}
解决方案
我找到了解决办法here.通过@Ed Jellard答案。
我只需要添加 camera.setDisplayOrientation(90);
在 surfaceCreated(SurfaceHolder持有人)
的方法,现在在显示器上的直角。
看见快乐的T-REX:)
Notice how the view of the camera (NOT THE CAPTURED IMAGE) was flipped to left (image above), the orientation of the Activity
is correct, but the camera view is messed up, please help me guys :) thank you.
Here is the xml
file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/camerapreview"
android:layout_margin="10dp"
android:layout_width="300dp"
android:layout_height="300dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
And here is the java
code :
public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback {
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.camera);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
解决方案
I found the solution here. Answer by @Ed Jellard.
i just have to add camera.setDisplayOrientation(90);
on surfaceCreated(SurfaceHolder holder)
method, now the display is on the right angle.
see the happy T-REX :)
这篇关于如何修复相机的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!