Camera preview both front and back

Source: Internet
Author: User

Idle to have nothing to write, simple demo, the shortcomings please forgive me.

PS: Some phones do not support both front and rear cameras.

Add Permissions:

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

As for the follow-up camera at the same time photo video How to achieve, welcome to share communication.

Camera preview both front and back

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.