java - How to get camera preview frame after every 500ms -
i developing sample application gives me color code of pointed image or object via camera in android. application similar application , using this application code this.
using application code able camera preview frames continuously , gives me color code of current previewframe. want make delay. want 1 camera preview frame after every 500ms.
how can that, modification need in code.
code :
class preview extends surfaceview implements surfaceholder.callback, previewcallback { public interface previewlistener { public void onpreviewupdated(int[] pixels, int width, int height); } previewlistener listener; surfaceholder mholder; camera mcamera = null; byte[] buffer; int buffersize; private boolean isfrontcamera = false; boolean lighton = false; private boolean ispaused = false; //this variable responsible getting , setting camera settings private parameters parameters; //this variable stores camera preview size private size previewsize; //this array stores pixels hexadecimal pairs private int[] pixels; 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); try { // instantiate entercolorlistener can send events host listener = (previewlistener) context; } catch (classcastexception e) { // activity doesn't implement interface, throw exception throw new classcastexception(context.tostring() + " must implement previewlistener"); } } public void surfacecreated(surfaceholder holder) { // surface has been created, acquire camera , tell // draw. try { camerainfo info = new camerainfo(); camera.getcamerainfo(0, info); if (info.facing == camerainfo.camera_facing_front) { this.isfrontcamera = true; } mcamera = camera.open(0); // attempt camera instance. index b/c front cameras ok too. } catch (exception e){ // camera not available (in use or not exist) log.e("camera error", "could not open camera"); return; } try { mcamera.setdisplayorientation(90); mcamera.setpreviewdisplay(holder); } catch (ioexception exception) { mcamera.release(); mcamera = null; } } // cheatcoder@github public void flash() { if (supportsflash()) { if (!lighton) { lighton = true; mcamera.stoppreview(); mcamera.setpreviewcallbackwithbuffer(this);// mcamera.setpreviewcallback(this);//setpreviewcallbackwithbuffer(this); parameters.setflashmode(parameters.flash_mode_torch); mcamera.setparameters(parameters); mcamera.startpreview(); } else { lighton = false; mcamera.stoppreview(); mcamera.setpreviewcallbackwithbuffer(this); //setpreviewcallback(this);//setpreviewcallbackwithbuffer(this); parameters.setflashmode(parameters.flash_mode_off); mcamera.setparameters(parameters); mcamera.startpreview(); } } } // http://ikravchenko.blogspot.com/2013/09/nexus-7-2013-torch-issue.html public boolean supportsflash() { if (getcontext().getpackagemanager().hassystemfeature(packagemanager.feature_camera_flash)) { parameters = mcamera.getparameters(); if (parameters.getflashmode() != null) { list<string> supportedflashmodes = parameters.getsupportedflashmodes(); if (supportedflashmodes == null || supportedflashmodes.isempty() || supportedflashmodes.size() == 1 && supportedflashmodes.get(0).equals(camera.parameters.flash_mode_off)) { return false; } return true; } } return false; } public void surfacedestroyed(surfaceholder holder) { // surface destroyed when return, stop preview. // because cameradevice object not shared resource, it's // important release when activity paused. if (mcamera != null) { mcamera.stoppreview(); mcamera.setpreviewcallback(null);//setpreviewcallbackwithbuffer(null); mcamera.release(); mcamera = null; } } public boolean isfrontcamera() { return isfrontcamera; } /* todo: fix bug null pointer exception */ public void surfacechanged(surfaceholder holder, int format, int w, int h) { // size known, set camera parameters , begin // preview. if (mcamera != null) { parameters = mcamera.getparameters(); //to autofocus, need set parameters if available //http://stackoverflow.com/questions/11623266/camera-parameters-setfocusmode-is-not-working list<string> focusmodes = parameters.getsupportedfocusmodes(); if (focusmodes != null) { if (focusmodes.contains(parameters.focus_mode_continuous_picture)) parameters.setfocusmode(parameters.focus_mode_continuous_picture); else if (focusmodes.contains(parameters.focus_mode_continuous_video)) parameters.setfocusmode(parameters.focus_mode_continuous_video); else if (focusmodes.contains(parameters.focus_mode_auto)) parameters.setfocusmode(parameters.focus_mode_auto); } //have previewsizes because not devices support arbitrary previews //the following stack overflow int width = this.getwidth(); int height = this.getheight(); size best = null; list<camera.size> previewsizes = parameters.getsupportedpreviewsizes(); // need choose appropriate previewsize app (int = 0; < previewsizes.size(); i++) { size size = previewsizes.get(i); if ((size.width <= width && size.height <= height) || (size.height <= width && size.width <= height)) { if (best==null) { best=size; } else { int resultarea=best.width*best.height; int newarea=size.width*size.height; if (newarea>resultarea) { best=size; } } } } // make sure picked. previewsizes guarenteed have @ least 1 thing. if (best != null) { previewsize = best; } else { previewsize = previewsizes.get(0); } parameters.setpreviewsize(previewsize.width, previewsize.height); pixels = new int[previewsize.width * previewsize.height]; mcamera.setparameters(parameters); //sets camera callback 1 defined in class mcamera.setpreviewcallbackwithbuffer(this);//setpreviewcallback(this);//setpreviewcallbackwithbuffer(this); buffersize = previewsize.width*previewsize.height*imageformat.getbitsperpixel(parameters.getpreviewformat())/8; buffer = new byte[buffersize]; resetbuffer(); if (!ispaused) mcamera.startpreview(); } } public void pause(boolean ispaused) { this.ispaused = ispaused; if (ispaused) { parameters.setflashmode(parameters.flash_mode_off); mcamera.setparameters(parameters); mcamera.stoppreview(); } else { if (mcamera != null) { mcamera.setpreviewcallbackwithbuffer(this);//setpreviewcallback(this);//setpreviewcallbackwithbuffer(this); if(lighton) parameters.setflashmode(parameters.flash_mode_torch); mcamera.setparameters(parameters); mcamera.startpreview(); } } } public void resetbuffer() { if (mcamera != null) { mcamera.addcallbackbuffer(buffer); } } @override public void onpreviewframe(byte[] data, camera camera) { //transforms nv21 pixel data rgb pixels decodeyuv420sp(pixels, data, previewsize.width, previewsize.height); listener.onpreviewupdated(pixels, previewsize.width, previewsize.height); } //method ketai project! not mine! see below... void decodeyuv420sp(int[] rgb, byte[] yuv420sp, int width, int height) { final int framesize = width * height; (int j = 0, yp = 0; j < height; j++) { int uvp = framesize + (j >> 1) * width, u = 0, v = 0; (int = 0; < width; i++, yp++) { int y = (0xff & ((int) yuv420sp[yp])) - 16; if (y < 0) y = 0; if ((i & 1) == 0) { v = (0xff & yuv420sp[uvp++]) - 128; u = (0xff & yuv420sp[uvp++]) - 128; } int y1192 = 1192 * y; int r = (y1192 + 1634 * v); int g = (y1192 - 833 * v - 400 * u); int b = (y1192 + 2066 * u); if (r < 0) r = 0; else if (r > 262143) r = 262143; if (g < 0) g = 0; else if (g > 262143) g = 262143; if (b < 0) b = 0; else if (b > 262143) b = 262143; rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff); } } } }
you can calculate elapsed time in onpreviewframe(). example:
boolean isfirsttime = true; long starttime = 0; previewcallback callback = new previewcallback() { @override public void onpreviewframe(byte[] data, camera camera) { // todo auto-generated method stub if (isfirsttime) { isfirsttime = false; starttime = systemclock.currentthreadtimemillis(); decodeyuv420sp(pixels, data, previewsize.width, previewsize.height); listener.onpreviewupdated(pixels, previewsize.width, previewsize.height); } else { long currenttime = systemclock.currentthreadtimemillis(); long elapsedtime = currenttime - starttime; if (elapsedtime >= 500) { // trigger event starttime = currenttime; decodeyuv420sp(pixels, data, previewsize.width, previewsize.height); listener.onpreviewupdated(pixels, previewsize.width, previewsize.height); } } } };
do not forget reset boolean value , start time when switch preview status.
Comments
Post a Comment