How to capture image automatically when application start in android? -
hi friends working on application.
in application when application start pic should automatically captured camera.
my code is:
1. camera demo
public class camerademo extends activity { private static final string tag = "camerademo"; camera camera; preview preview; button buttonclick; chronometer ch; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); preview = new preview(this); ((framelayout) findviewbyid(r.id.preview)).addview(preview); buttonclick = (button) findviewbyid(r.id.buttonclick); ch=(chronometer)findviewbyid(r.id.chronometer1); ch.start(); preview.camera.takepicture(shuttercallback,rawcallback,jpegcallback); // if write above code here shows error. buttonclick.setonclicklistener( new onclicklistener() { public void onclick(view v) { buttonclick.setvisibility(view.invisible); preview.camera.takepicture(shuttercallback, rawcallback,jpegcallback); // here works perfactly toast.maketext(camerademo.this,""+ch.gettext(), toast.length_long).show(); } }); log.d(tag, "oncreate'd"); } shuttercallback shuttercallback = new shuttercallback() { public void onshutter() { log.d(tag, "onshutter'd"); } }; /** handles data raw picture */ picturecallback rawcallback = new picturecallback() { public void onpicturetaken(byte[] data, camera camera) { log.d(tag, "onpicturetaken - raw"); } }; /** handles data jpeg picture */ picturecallback jpegcallback = new picturecallback() { public void onpicturetaken(byte[] data, camera camera) { fileoutputstream outstream = null; try { // write local sandbox file system // outstream = camerademo.this.openfileoutput(string.format("%d.jpg", system.currenttimemillis()), 0); // or write sdcard outstream = new fileoutputstream(string.format("/sdcard/tmp/%d.jpg", system.currenttimemillis())); outstream.write(data); outstream.close(); log.d(tag, "onpicturetaken - wrote bytes: " + data.length); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { } log.d(tag, "onpicturetaken - jpeg"); } }; }
2. preview.java
package com.example; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.hardware.camera; import android.hardware.camera.previewcallback; import android.util.log; import android.view.surfaceholder; import android.view.surfaceview; class preview extends surfaceview implements surfaceholder.callback { private static final string tag = "preview"; surfaceholder mholder; public camera camera; @suppresswarnings("deprecation") preview(context context) { super(context); // install surfaceholder.callback notified when // underlying surface created , destroyed. mholder = getholder(); mholder.addcallback(this); mholder.settype(surfaceholder.surface_type_push_buffers); } public void surfacecreated(surfaceholder holder) { // surface has been created, acquire camera , tell // draw. camera = camera.open(); try { camera.setpreviewdisplay(holder); camera.setpreviewcallback(new previewcallback() { public void onpreviewframe(byte[] data, camera arg1) { fileoutputstream outstream = null; try { outstream = new fileoutputstream(string.format("/sdcard/tmp/%d.jpg", system.currenttimemillis())); outstream.write(data); outstream.close(); log.d(tag, "onpreviewframe - wrote bytes: " + data.length); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { } preview.this.invalidate(); } }); } catch (ioexception e) { e.printstacktrace(); } } public void surfacedestroyed(surfaceholder holder) { // surface destroyed when return, stop preview. // because cameradevice object not shared resource, it's // important release when activity paused. camera.stoppreview(); camera = null; } public void surfacechanged(surfaceholder holder, int format, int w, int h) { // size known, set camera parameters , begin // preview. camera.parameters parameters = camera.getparameters(); parameters.setpreviewsize(w, h); camera.setparameters(parameters); camera.startpreview(); } @override public void draw(canvas canvas) { super.draw(canvas); paint p= new paint(color.red); log.d(tag,"draw"); canvas.drawtext("preview", canvas.getwidth()/2, canvas.getheight()/2, p ); } }
for had given above permisson:
<uses-permission android:name="android.permission.camera" />
my error log is:
fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{com.example/com.example.camerademo}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2343) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2395) @ android.app.activitythread.access$600(activitythread.java:162) @ android.app.activitythread$h.handlemessage(activitythread.java:1364) @ android.os.handler.dispatchmessage(handler.java:107) @ android.os.looper.loop(looper.java:194) @ android.app.activitythread.main(activitythread.java:5371) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:833) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:600) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.example.camerademo.oncreate(camerademo.java:41) @ android.app.activity.performcreate(activity.java:5122) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1081) @ android.app.activitythread.performlaunchactivity(activitythread.java:2307) ... 11 more
my problem is:
when using code in button click event work perfectly. need when start application image should capture automatically.
same code not working in oncreate() event , application stops unfortunately.
please tell solution of this.
thank you.
your surfaceview
not yet created when call
preview.camera.takepicture(shuttercallback,rawcallback,jpegcallback);
so camera null @ time..
try call after dealy..
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.camera); preview = new preview(this); ((framelayout) findviewbyid(r.id.preview)).addview(preview); new java.util.timer().schedule( new java.util.timertask() { @override public void run() { // code here preview.camera.takepicture(shuttercallback,rawcallback,jpegcallback); } }, 2000 ); }
this works perfect me... hope works too.. best luck :)
Comments
Post a Comment