博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Camera前后摄像头同时预览
阅读量:5041 次
发布时间:2019-06-12

本文共 4018 字,大约阅读时间需要 13 分钟。

闲来无事写的,简单的Demo,不足之处请见谅。

PS:有的手机不支持同时打开前后摄像头。

添加权限:

<uses-permission android:name="android.permission.INTERNET" />

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.microphone"/>

mainactivity.java

public class MainActivity extends Activity implements OnErrorListener, OnInfoListener {

private SurfaceView sfv1;
private SurfaceView sfv2;
private Button btn;
private Button btn1;
private Camera mcamera = null;
private SurfaceHolder mSurfaceHolder;
private SurfaceHolder mSurfaceHolder1;
private boolean bool;

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
this.mContext = super.getBaseContext();

sfv1 = (SurfaceView) findViewById(R.id.sfv1);

sfv2 = (SurfaceView) findViewById(R.id.sfv2);
btn = (Button) findViewById(R.id.btn);
btn1 = (Button) findViewById(R.id.btn1);

this.mSurfaceHolder = this.sfv1.getHolder();

this.mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
Camera mcamera = Camera.open(0);
mcamera.setDisplayOrientation(90);
try {
mcamera.setPreviewDisplay(mSurfaceHolder);
mcamera.startPreview();
return;
} catch (IOException localIOException) {
localIOException.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
this.mSurfaceHolder.setType(3);
this.mSurfaceHolder1 = this.sfv2.getHolder();
this.mSurfaceHolder1.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
Camera mcamera = Camera.open(1);
mcamera.setDisplayOrientation(90);
try {
mcamera.setPreviewDisplay(mSurfaceHolder1);
mcamera.startPreview();
return;
} catch (IOException localIOException) {
localIOException.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
this.mSurfaceHolder1.setType(3);
Log.d("lyb", "btn onclick");
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//startVideoRecording();
}

});

btn1.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View v) {

//stopVideoRecording();

});

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<SurfaceView

android:id="@+id/sfv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />

<SurfaceView

android:id="@+id/sfv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />

<LinearLayout

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button

android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="start" />

<Button

android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="stop" />
</LinearLayout>

</LinearLayout>

至于后续前后摄像头同时拍照录像怎么实现,欢迎大家分享交流。

转载于:https://www.cnblogs.com/01b-/p/5424882.html

你可能感兴趣的文章
数据结构之查找算法总结笔记
查看>>
Linux内核OOM机制的详细分析
查看>>
Android TextView加上阴影效果
查看>>
Requests库的基本使用
查看>>
C#:System.Array简单使用
查看>>
C#inSSIDer强大的wifi无线热点信号扫描器源码
查看>>
「Foundation」集合
查看>>
算法时间复杂度
查看>>
二叉树的遍历 - 数据结构和算法46
查看>>
类模板 - C++快速入门45
查看>>
centos7 搭建vsftp服务器
查看>>
RijndaelManaged 加密
查看>>
Android 音量调节
查看>>
HTML&CSS基础学习笔记1.28-给网页添加一个css样式
查看>>
windows上面链接使用linux上面的docker daemon
查看>>
Redis事务
查看>>
Web框架和Django基础
查看>>
python中的逻辑操作符
查看>>
CSS兼容性常见问题总结
查看>>
HDU 1548 A strange lift (Dijkstra)
查看>>