星空网 > 软件开发 > 操作系统

Camera前后摄像头同时预览

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

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

添加权限:

<uses-permission android:name="android.permission.INTE.NET" />
<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.

<LinearLayout 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>

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




原标题:Camera前后摄像头同时预览

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Sponsored:https://www.goluckyvip.com/tag/6137.html
Day卖爆:https://www.goluckyvip.com/tag/6138.html
卡派停运:https://www.goluckyvip.com/tag/6139.html
涨价倒计时:https://www.goluckyvip.com/tag/614.html
操作步骤:https://www.goluckyvip.com/tag/6140.html
Search:https://www.goluckyvip.com/tag/6141.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
南美旅游报价(探索南美洲的旅行费用):https://www.vstour.cn/a/411234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流