android - Can we click images with camera preview overlay? -
i have overlay in camera preview. want clicked images saved overlay. possible?
below code use overlay image on top of camera preview surface holder. hope helps someone.
ok got solution.
code: package com.sailabs.photo; import android.annotation.targetapi; import android.app.activity; import android.content.context; import android.content.res.configuration; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.pixelformat; import android.graphics.drawable.drawable; import android.hardware.camera; import android.hardware.camera.picturecallback; import android.hardware.camera.shuttercallback; import android.os.build; import android.os.bundle; import android.os.environment; import android.util.log; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.view.onclicklistener; import android.view.window; import android.view.windowmanager; import android.widget.button; import android.widget.relativelayout; import android.widget.toast; import java.io.bufferedreader; import java.io.bytearrayoutputstream; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.inetaddress; import java.net.inetsocketaddress; import java.net.networkinterface; import java.net.serversocket; import java.net.socket; import java.net.socketexception; import java.util.enumeration; public class mainactivity extends activity implements surfaceholder.callback { private camera camera = null; private surfaceview camerasurfaceview = null; private surfaceholder camerasurfaceholder = null; private boolean previewing = false; relativelayout relativelayout; private context mycontext; private boolean camerafront = false; public int glo = 0; // private textview text = null; private button capture = null; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); getwindow().addflags(windowmanager.layoutparams.flag_keep_screen_on); mycontext = this; getwindow().setformat(pixelformat.translucent); requestwindowfeature(window.feature_no_title); getwindow().setflags( windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_main); relativelayout=(relativelayout) findviewbyid(r.id.containerimg); relativelayout.setdrawingcacheenabled(true); camerasurfaceview = (surfaceview) findviewbyid(r.id.surfaceview1); // text = (textview) findviewbyid(r.id.textview); // camerasurfaceview.setlayoutparams(new framelayout.layoutparams(640, 480)); camerasurfaceholder = camerasurfaceview.getholder(); camerasurfaceholder.addcallback(this); // camerasurfaceholder.settype(surfaceholder. // surface_type_push_buffers); capture = (button)findviewbyid(r.id.button1); capture.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { /* text.settext("1"); try{ thread.sleep(1000); }catch (exception e) { e.printstacktrace(); } text.settext("2"); try{ thread.sleep(1000); }catch (exception e) { e.printstacktrace(); } text.settext("3"); try{ thread.sleep(1000); }catch (exception e) { e.printstacktrace(); }*/ capturethread capturethread = new capturethread(); capturethread.start(); // todo auto-generated method stub } }); } @targetapi(build.version_codes.gingerbread) private int findfrontfacingcamera() { int cameraid = -1; // search front facing camera int numberofcameras = camera.getnumberofcameras(); (int = 0; < numberofcameras; i++) { camera.camerainfo info = new camera.camerainfo(); camera.getcamerainfo(i, info); if (info.facing == camera.camerainfo.camera_facing_front) { cameraid = i; camerafront = true; break; } } return cameraid; } shuttercallback camerashuttercallback = new shuttercallback() { @override public void onshutter() { // todo auto-generated method stub } }; picturecallback camerapicturecallbackraw = new picturecallback() { @override public void onpicturetaken(byte[] data, camera camera) { // todo auto-generated method stub } }; picturecallback camerapicturecallbackjpeg = new picturecallback() { @override public void onpicturetaken(byte[] data, camera camera) { // todo auto-generated method stub bitmap camerabitmap = bitmapfactory.decodebytearray (data, 0, data.length); int wid = camerabitmap.getwidth(); int hgt = camerabitmap.getheight(); // toast.maketext(getapplicationcontext(), wid+""+hgt, toast.length_short).show(); bitmap newimage = bitmap.createbitmap (wid, hgt, bitmap.config.argb_8888); canvas canvas = new canvas(newimage); canvas.drawbitmap(camerabitmap, 0f, 0f, null); drawable drawable = getresources().getdrawable(r.drawable.mark3); drawable.setbounds(20, 30, drawable.getintrinsicwidth()+20, drawable.getintrinsicheight()+30); drawable.draw(canvas); //file storagepath = new file(environment.getexternalstoragedirectory() + "/pics/"); // storagepath.mkdirs(); file mediastoragedir = new file("/sdcard/", "pics"); // file myimage = new file(storagepath, +".jpg"); file myimage = new file(mediastoragedir.getpath() + file.separator + "pic" + glo + ".png"); log.d("naval", "file path :" + myimage); glo++; try { fileoutputstream out = new fileoutputstream(myimage); newimage.compress(bitmap.compressformat.jpeg, 80, out); out.flush(); out.close(); } catch(filenotfoundexception e) { log.d("in saving file", e + ""); } catch(ioexception e) { log.d("in saving file", e + ""); } camera.startpreview(); // newimage.recycle(); // newimage = null; // intent intent = new intent(); // intent.setaction(intent.action_view); // intent.setdataandtype(uri.parse("file://" + myimage.getabsolutepath()), "image/*"); // startactivity(intent); } }; @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { // todo auto-generated method stub if(previewing) { camera.stoppreview(); previewing = false; } try { camera.parameters parameters = camera.getparameters(); // parameters.setpreviewsize(640, 480); // parameters.setpicturesize(640, 480); if (this.getresources().getconfiguration().orientation != configuration.orientation_landscape) { camera.setdisplayorientation(90); } // parameters.setrotation(90); camera.setparameters(parameters); camera.setpreviewdisplay(camerasurfaceholder); camera.startpreview(); previewing = true; } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } @targetapi(build.version_codes.gingerbread) @override public void surfacecreated(surfaceholder holder) { // todo auto-generated method stub try { camera = camera.open(findfrontfacingcamera()); } catch(runtimeexception e) { toast.maketext(getapplicationcontext(), "device camera not working properly, please try after sometime.", toast.length_long).show(); } } @override public void surfacedestroyed(surfaceholder holder) { // todo auto-generated method stub camera.stoppreview(); camera.release(); camera = null; previewing = false; } class capturethread extends thread { @override public void run() { int count = 0; while (count < 30) { // mfilename = mlocation + "/pic" + count + ".jpg"; try { log.d("naval","camera clicked"); camera.takepicture(camerashuttercallback, camerapicturecallbackraw, camerapicturecallbackjpeg); //mcamera.takepicture(null, null, mpicture); count++; thread.sleep(1000); } catch (exception exception) { exception.printstacktrace(); } } glo = 0; // images save 0 in pics folder try { // wait(5000); } catch (exception e) { e.printstacktrace(); } creategif(); } public void creategif() { bytearrayoutputstream bos = new bytearrayoutputstream(); animatedgifencoder encoder = new animatedgifencoder(); bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 6; encoder.setdelay(50); encoder.start(bos); (int = 0; < 30; i++) { bitmap bmap = bitmapfactory.decodefile("/storage/emulated/0/pics/pic" + + ".png", options); log.d("naval", "added image"); encoder.addframe(bmap); // encoder.setquality(100); } encoder.finish(); writetofile(bos.tobytearray()); try { log.d("naval - lets sleep", "turn again click"); thread.sleep(5000); socketserverthread sc = new socketserverthread(); sc.initialize(); sc.start(); //capture.performclick(); } catch (exception e) { e.printstacktrace(); } } public void writetofile(byte[] array) { try { string path = environment.getexternalstoragedirectory() + "/gif/gif.gif"; fileoutputstream stream = new fileoutputstream(path); stream.write(array); } catch (exception e) { e.printstacktrace(); } } } private class socketserverthread extends thread { int count = 0; string message = ""; serversocket serversocket = null; // string message = ""; static final int socketserverport = 4444; public string ip = getipaddress(); public socketserverthread()throws ioexception { // serversocket log.d("naval", " constructor thread"); } public void initialize () { try { log.d("naval", "initialize socket"); serversocket = new serversocket(); serversocket.setreuseaddress(true); serversocket.bind(new inetsocketaddress(6677)); }catch (ioexception e) { e.printstacktrace(); } } @override public void run() { try { // inetaddress inetaddress = inetaddress.getbyname("192.168.1.1"); // serversocket = new serversocket(socketserverport); if(serversocket == null) { log.d("naval", "ye null hai bhai"); } while (true) { log.d("naval","waiting packet @ port , ip :"+ip+"a"+serversocket.getinetaddress()+"b"+serversocket.getlocalport()); socket socket = serversocket.accept(); log.d("naval", " packet get"); string encoding = ""; /* receiving client*/ try { inputstream inputstream = socket.getinputstream(); bufferedreader r = new bufferedreader(new inputstreamreader(inputstream)); stringbuilder total = new stringbuilder(); string line; while ((line = r.readline()) != null) { total.append(line); } log.d("naval", total.tostring()); message = total.tostring(); if(message.equals("start")) { try{ // r.close(); // inputstream.close(); serversocket.close(); socket.close(); }catch (exception e) { e.printstacktrace(); } mainactivity.this.runonuithread(new runnable() { @override public void run() { log.d("naval","click performed"); capture.performclick(); } }); // log.d("naval","breaking while loop"); break; } } catch (ioexception ioe) { ioe.printstacktrace(); } //socket.close(); log.d("naval","closing socket"); count++; // message += "#" + count + " " // + socket.getinetaddress() + ":" // + socket.getport() + "\n"; } //serversocket.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } public string getipaddress() { string ip = ""; try { enumeration<networkinterface> enumnetworkinterfaces = networkinterface .getnetworkinterfaces(); while (enumnetworkinterfaces.hasmoreelements()) { networkinterface networkinterface = enumnetworkinterfaces .nextelement(); enumeration<inetaddress> enuminetaddress = networkinterface .getinetaddresses(); while (enuminetaddress.hasmoreelements()) { inetaddress inetaddress = enuminetaddress .nextelement(); if (inetaddress.issitelocaladdress()) { ip += "server running @ : " + inetaddress.gethostaddress(); } } } } catch (socketexception e) { // todo auto-generated catch block e.printstacktrace(); ip += "something wrong! " + e.tostring() + "\n"; } return ip; } } }
Comments
Post a Comment