Camera前后摄像头同时预览

闲来无事写的,简单的Demo。

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

都懂的,首先要添加权限。

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

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/sfv1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1" />

android:id="@+id/sfv2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

android:id="@+id/btn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="start" />

android:id="@+id/btn1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="stop" />

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容