Android Camera two preview from single camera -
i want build application android camera 2 preview single camera enter image description here
i tried build app,i using surfaceview in android studio have problem, enter image description here
i want example code, can me please? thank much.
i tried app.
androidsurfaceviewexample.java
package th.in.cybernoi.cardboardview; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.text.simpledateformat; import java.util.date; import android.app.activity; import android.content.context; import android.content.pm.packagemanager; import android.hardware.camera; import android.hardware.camera.picturecallback; import android.hardware.camera.camerainfo; import android.hardware.camera.shuttercallback; import android.os.bundle; import android.util.log; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.windowmanager; import android.view.view.onclicklistener; import android.widget.button; import android.widget.linearlayout; import android.widget.textview; import android.widget.toast; public class androidsurfaceviewexample extends activity implements surfaceholder.callback { textview testview; camera camera; surfaceview surfaceview; surfaceholder surfaceholder; picturecallback rawcallback; shuttercallback shuttercallback; picturecallback jpegcallback; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); surfaceview = (surfaceview) findviewbyid(r.id.surfaceview); surfaceholder = surfaceview.getholder(); // install surfaceholder.callback notified when // underlying surface created , destroyed. surfaceholder.addcallback(this); // deprecated setting, required on android versions prior 3.0 surfaceholder.settype(surfaceholder.surface_type_push_buffers); jpegcallback = new picturecallback() { public void onpicturetaken(byte[] data, camera camera) { fileoutputstream outstream = null; try { outstream = new fileoutputstream(string.format("/sdcard/%d.jpg", system.currenttimemillis())); outstream.write(data); outstream.close(); log.d("log", "onpicturetaken - wrote bytes: " + data.length); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { } toast.maketext(getapplicationcontext(), "picture saved", 2000).show(); refreshcamera(); } }; } //surfacrview public void captureimage(view v) throws ioexception { //take picture camera.takepicture(null, null, jpegcallback); } public void refreshcamera() { if (surfaceholder.getsurface() == null) { // preview surface not exist return; } // stop preview before making changes try { camera.stoppreview(); } catch (exception e) { // ignore: tried stop non-existent preview } // set preview size , make resize, rotate or // reformatting changes here // start preview new settings try { camera.setpreviewdisplay(surfaceholder); camera.startpreview(); } catch (exception e) { } } public void surfacechanged(surfaceholder holder, int format, int w, int h) { // size known, set camera parameters , begin // preview. refreshcamera(); } public void surfacecreated(surfaceholder holder) { try { // open camera camera = camera.open(); } catch (runtimeexception e) { // check exceptions system.err.println(e); return; } camera.parameters param; param = camera.getparameters(); // modify parameter param.setpreviewsize(352, 288); camera.setparameters(param); try { // surface has been created, tell camera draw // preview. camera.setpreviewdisplay(surfaceholder); camera.startpreview(); } catch (exception e) { // check exceptions system.err.println(e); return; } } public void surfacedestroyed(surfaceholder holder) { // stop preview , release camera camera.stoppreview(); camera.release(); camera = null; } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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="horizontal" tools:context="th.in.cybernoi.cardboardview.mainactivity"> <surfaceview android:id="@+id/surfaceview" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <surfaceview android:id="@+id/surfaceview" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </linearlayout>
update edit code :9/02/16
- edit androidsurfaceviewexample.java
- add public void onpreviewframe()
public class androidsurfaceviewexample extends activity implements surfaceholder.callback { textview testview; camera camera; surfaceholder camera01; surfaceholder camera02; surfaceview surfaceviewleft; surfaceholder surfaceholderleft; surfaceview surfaceviewright; surfaceholder surfaceholderright; picturecallback rawcallback; shuttercallback shuttercallback; picturecallback jpegcallback; /** * attention: auto-generated implement app indexing api. * see https://g.co/appindexing/androidstudio more information. */ private googleapiclient client; /** * called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); surfaceviewleft = (surfaceview) findviewbyid(r.id.surfaceviewleft); surfaceholderleft = surfaceviewleft.getholder(); surfaceviewright = (surfaceview) findviewbyid(r.id.surfaceviewright); surfaceholderright = surfaceviewright.getholder(); // install surfaceholder.callback notified when // underlying surface created , destroyed. surfaceholderleft.addcallback(this); surfaceholderright.addcallback(this); // deprecated setting, required on android versions prior 3.0 surfaceholderleft.settype(surfaceholder.surface_type_push_buffers); surfaceholderright.settype(surfaceholder.surface_type_push_buffers); /* jpegcallback = new picturecallback() { public void onpicturetaken(byte[] data, camera camera) { fileoutputstream outstream = null; try { outstream = new fileoutputstream(string.format("/sdcard/%d.jpg", system.currenttimemillis())); outstream.write(data); outstream.close(); log.d("log", "onpicturetaken - wrote bytes: " + data.length); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { } toast.maketext(getapplicationcontext(), "picture saved", 2000).show(); refreshcamera(); } }; */ // attention: auto-generated implement app indexing api. // see https://g.co/appindexing/androidstudio more information. client = new googleapiclient.builder(this).addapi(appindex.api).build(); } //surfacrview public void refreshcamera() { if (surfaceholderleft.getsurface() == null) { // preview surface not exist return; } // stop preview before making changes try { camera.stoppreview(); } catch (exception e) { // ignore: tried stop non-existent preview } // set preview size , make resize, rotate or // reformatting changes here // start preview new settings try { camera.setpreviewdisplay(surfaceholderleft); camera.startpreview(); } catch (exception e) { } } public void onpreviewframe() { if (surfaceholderright.getsurface() == null) { // preview surface not exist return; } // stop preview before making changes try { camera.stoppreview(); } catch (exception e) { // ignore: tried stop non-existent preview } // set preview size , make resize, rotate or // reformatting changes here // start preview new settings try { surfaceholderleft.lockcanvas(); camera.setpreviewdisplay(surfaceholderright); camera.setpreviewcallback((camera.previewcallback) surfaceholderleft); camera.startpreview(); } catch (exception e) { } } public void surfacechanged(surfaceholder holder, int format, int w, int h) { // size known, set camera parameters , begin // preview. refreshcamera(); onpreviewframe(); } public void surfacecreated(surfaceholder holder) { try { // open camera camera = camera.open(); } catch (runtimeexception e) { // check exceptions system.err.println(e); return; } camera.parameters param; param = camera.getparameters(); // modify parameter param.setpreviewsize(352, 288); camera.setparameters(param); try { // surface has been created, tell camera draw // preview. camera.setpreviewdisplay(surfaceholderleft); camera.startpreview(); } catch (exception e) { // check exceptions system.err.println(e); return; } } public void surfacedestroyed(surfaceholder holder) { // stop preview , release camera camera.stoppreview(); camera.release(); camera = null; } @override public void onstart() { super.onstart(); // attention: auto-generated implement app indexing api. // see https://g.co/appindexing/androidstudio more information. client.connect(); action viewaction = action.newaction( action.type_view, // todo: choose action type. "androidsurfaceviewexample page", // todo: define title content shown. // todo: if have web page content matches app activity's content, // make sure auto-generated web page url correct. // otherwise, set url null. uri.parse("http://host/path"), // todo: make sure auto-generated app deep link uri correct. uri.parse("android-app://th.in.cybernoi.cardboardview/http/host/path") ); appindex.appindexapi.start(client, viewaction); } @override public void onstop() { super.onstop(); // attention: auto-generated implement app indexing api. // see https://g.co/appindexing/androidstudio more information. action viewaction = action.newaction( action.type_view, // todo: choose action type. "androidsurfaceviewexample page", // todo: define title content shown. // todo: if have web page content matches app activity's content, // make sure auto-generated web page url correct. // otherwise, set url null. uri.parse("http://host/path"), // todo: make sure auto-generated app deep link uri correct. uri.parse("android-app://th.in.cybernoi.cardboardview/http/host/path") ); appindex.appindexapi.end(client, viewaction); client.disconnect(); } }
each element in layout needs have unique id. have identified both screen elements @+id/surfaceview
. try making them both unique. see modified layout file below - don't forget update source code populate both screen elements.
<?xml version="1.0" encoding="utf-8"?> <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="horizontal" tools:context="th.in.cybernoi.cardboardview.mainactivity"> <surfaceview android:id="@+id/surfaceviewleft" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <surfaceview android:id="@+id/surfaceviewright" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </linearlayout>
add code program reference both surfaceview objects. duplicate code have. should check rest of program other references surfaceview object need updated.
surfaceviewleft = (surfaceview) findviewbyid(r.id.surfaceviewleft); surfaceholderleft = surfaceviewleft.getholder(); // install surfaceholder.callback notified when // underlying surface created , destroyed. surfaceholderleft.addcallback(this); // deprecated setting, required on android versions prior 3.0 surfaceholderleft.settype(surfaceholder.surface_type_push_buffers); surfaceviewright = (surfaceview) findviewbyid(r.id.surfaceviewright); surfaceholderright = surfaceviewright.getholder(); // install surfaceholder.callback notified when // underlying surface created , destroyed. surfaceholderright.addcallback(this); // deprecated setting, required on android versions prior 3.0 surfaceholderright.settype(surfaceholder.surface_type_push_buffers);
the key realize although left , right views same type of object (they both surfaceviews
) are unaware of each other. if want happen 1 view, must write code works explicitly on view. if want happen in other view, must write code works explicitly on other view. example of having 2 instances of same object.
update: upon further digging, see above changes neccesary not sufficient. call camera.setpreviewdisplay(surfaceholderleft);
allows single preview surfaceview
defined, can't preview on 2 surfaces using method.
to accomplish trying do, recommend creating bitmap of preview image (which can obtained in callback called onpreviewframe()
, similar onpicturetaken() callback. bitmap data passed in through first parameter) a
canvasfor each surface view (by calling
surfaceholder.lockcanvas(), , draw bitmap saved onto each canvas calling
canvas.drawbitmap()`. should draw original image onto each surface view.
since drawing each surfaceview directly, should rid of line sets 1 of surfaceviews preview competing camera access draw there. rid of line camera.setpreviewdisplay(surfaceholderleft);
, instead write camera.setpreviewcallback(yourcallbackgoeshere);
good luck.
Comments
Post a Comment