How to change camera from front to back and back to front on button click in android -
i working on android tablet application. want change camera front , front on button. how can achieve ? have tried several example not getting proper response.
i adding code also.
public class photopreview extends activity implements surfaceholder.callback { private camera camera; private imagebutton cameraclick; private imagebutton cameraswap; surfaceview surfaceview; private surfaceholder mholder; boolean previewing = false; string path = ""; layoutinflater controlinflater = null; bitmap bmp; button cameracancel; private sharedpreferences myprefs; private int camid; /** called when activity first created. */ @suppresswarnings("deprecation") @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); system.out.println("photo preview called $$$$$$$$$$$$$$$$$$ "); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.photo_preview); myprefs = this.getsharedpreferences("myprefs", mode_private); camid = myprefs.getint("camid",1); system.out.println("cam id $$$$$$$$$$$$$$$$$$ "+camid); getwindow().setformat(pixelformat.unknown); surfaceview = (surfaceview) findviewbyid(r.id.camerapreview); mholder = surfaceview.getholder(); mholder.settype(surfaceholder.surface_type_push_buffers); mholder.addcallback(this); controlinflater = layoutinflater.from(getbasecontext()); view viewcontrol = controlinflater.inflate(r.layout.control, null); layoutparams layoutparamscontrol = new layoutparams( layoutparams.match_parent, layoutparams.match_parent); this.addcontentview(viewcontrol, layoutparamscontrol); cameraclick = (imagebutton) findviewbyid(r.id.cameraclick); cameraclick.setonclicklistener(cameraclicklistener); cameracancel = (button) findviewbyid(r.id.cameracancel); cameracancel.setonclicklistener(cameracancelclicklistener); cameraswap = (imagebutton) findviewbyid(r.id.cameraswap); cameraswap.setonclicklistener(swapcameraclicklistener); } /* * click event of photo capture button */ private onclicklistener cameraclicklistener = new onclicklistener() { @override public void onclick(final view v) { if (camera != null) { camera.takepicture(shuttercallback, rawcallback, jpegcallback); } } }; /* * click event of camera cancel button */ private onclicklistener cameracancelclicklistener = new onclicklistener() { @override public void onclick(final view v) { intent intent = new intent(photopreview.this, mainscreenactivity.class); startactivity(intent); } }; /* * click event of camera cancel button */ private onclicklistener swapcameraclicklistener = new onclicklistener() { @override public void onclick(final view v) { if (camid == 0) { sharedpreferences.editor prefseditor = myprefs.edit(); prefseditor.putint("camid", 1); prefseditor.commit(); } else { sharedpreferences.editor prefseditor = myprefs.edit(); prefseditor.putint("camid", 0); prefseditor.commit(); } system.out.println("cam id ^^^^^^^^^^^^^^^^^^^^ "+camid); intent intent = new intent(photopreview.this, photopreview.class); startactivity(intent); } }; // handles when shutter open shuttercallback shuttercallback = new shuttercallback() { public void onshutter() { } }; /** handles data raw picture */ picturecallback rawcallback = new picturecallback() { public void onpicturetaken(byte[] data, camera camera) { } }; /** handles data jpeg picture */ picturecallback jpegcallback = new picturecallback() { public void onpicturetaken(byte[] data, camera camera) { bmp = bitmapfactory.decodebytearray(data, 0, data.length); string root = environment.getexternalstoragedirectory().tostring(); file mydir = new file(root + "/easy_measurement_images"); mydir.mkdirs(); random generator = new random(); int n = 10000; n = generator.nextint(n); string fname = "image-" + n + ".jpg"; file file = new file(mydir, fname); if (file.exists()) file.delete(); try { fileoutputstream out = new fileoutputstream(file); bmp.compress(bitmap.compressformat.png, 90, out); out.flush(); out.close(); } catch (exception e) { e.printstacktrace(); } intent intent = new intent(photopreview.this, verticaladjustmentactivity.class); startactivity(intent); } }; @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { // set camera preview size,orientation,rotation using parameters if (camera != null) { camera.parameters parameters = camera.getparameters(); parameters.set("orientation", "portrait"); camera.setparameters(parameters); camera.startpreview(); } } @override public void surfacecreated(surfaceholder holder) { system.out.println("cam id %%%%%%%%%%%%%%$$$$$$$$$$$$$$$$$$ "+camid); camera = camera.open(camid); if (camera != null) { try { camera.setpreviewdisplay(holder); } catch (ioexception e) { e.printstacktrace(); } } } @override public void surfacedestroyed(surfaceholder holder) { system.out.println("surface destroyed ***************"); if (camera != null) { camera.stoppreview(); camera.release(); camera = null; } }
}
you can open camera using camera.open(int i). can use 1 camera @ time. release 1 , open another. example
public void onclick(view v) { camera.stoppreview(); camera.release(); if(currentcameraid == camera.camerainfo.camera_facing_back){ currentcameraid = camera.camerainfo.camera_facing_front; } else { currentcameraid = camera.camerainfo.camera_facing_back; } camera = camera.open(currentcameraid); setcameradisplayorientation(cameraactivity.this, currentcameraid, camera); try { camera.setpreviewdisplay(previewholder); } catch (ioexception e) { e.printstacktrace(); } camera.startpreview();
}
Comments
Post a Comment