你的位置:首页 > 操作系统

[操作系统]Camera前后摄像头同时预览


闲来无事写的,简单的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.

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

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