Android Camera SurfaceView create bitmap -
hi i've been doing 4 days can't solve it. have class camera preview class , created bitmap function inside class , want call function in class bitmap.
this coding calling bitmap function:
public class stage extends glsurfaceview { private float w, h; private int screenwidth, screenheight; private string img; private boolean sc; private bitmap screen, imgb; private boolean c; myrenderer mrenderer; //here declare class of camera <<<<<<< camerasurfaceview csv; public stage(context context, attributeset attrs) { super(context, attrs); seteglconfigchooser(8, 8, 8, 8, 0, 0); getholder().setformat(pixelformat.transparent); setzorderontop(true); mrenderer = new myrenderer(); setrenderer(mrenderer); setrendermode(glsurfaceview.rendermode_when_dirty); //here camera class declare......... csv = new camerasurfaceview(context); } public class myrenderer implements glsurfaceview.renderer { public void setsc(boolean yn){ sc = yn; requestrender(); } public void setc(boolean y){ c=y; } public final void ondrawframe(gl10 gl) { gl.glclear(gles10.gl_color_buffer_bit); if(sc==true){ if(c==true){ //capture camera scene , convert bitmap //here code of bitmap camera class imgb = csv.getcamerabitmap(); } else{ } string file_path = environment.getexternalstoragedirectory().getabsolutepath() + "/opengl"; file dir = new file(file_path); if(!dir.exists()){ dir.mkdirs(); } string format = new simpledateformat("yyyymmddhhmmss", java.util.locale.getdefault()).format(new date()); file file = new file(file_path, format + ".png"); fileoutputstream fout; try { fout = new fileoutputstream(file); imgb.compress(bitmap.compressformat.png, 85, fout); fout.flush(); fout.close(); } catch (exception e) { e.printstacktrace(); } sc = false; } } public final void onsurfacechanged(gl10 gl, int width, int height) { gl.glclearcolor(0, 0, 0, 0); if(width > height) { h = 600; w = width * h / height; } else { w = 600; h = height * w / width; } screenwidth = width; screenheight = height; gl.glviewport(0, 0, screenwidth, screenheight); gl.glmatrixmode(gl10.gl_projection); gl.glloadidentity(); gl.glorthof(0, w, h, 0, -1, 1); gl.glmatrixmode(gl10.gl_modelview); gl.glloadidentity(); } public final void onsurfacecreated(gl10 gl, eglconfig config) { // set alpha blending gl.glenable(gl10.gl_alpha_test); gl.glenable(gl10.gl_blend); gl.glblendfunc(gl10.gl_one, gl10.gl_one_minus_src_alpha); // in 2d. why needs depth? gl.gldisable(gl10.gl_depth_test); // enable vertex arrays (we'll use them draw primitives). gl.glenableclientstate(gl10.gl_vertex_array); // enable texture coordination arrays. gl.glenableclientstate(gl10.gl_texture_coord_array); tex.load(getcontext()); } } }
this camera class:
public class camerasurfaceview extends surfaceview implements surfaceholder.callback { private static final string tag = "camerasurfaceview"; private surfaceholder msurfaceholder; private camera mcamera = null; private bitmap mbitmap; private context mcontext; private camera.parameters mparameters; private byte[] bytearray; private list<camera.size> msupportedpreviewsizes; private camera.size mpreviewsize; bitmap camerabitmap; public camerasurfaceview (context context) { this(context, null); } public camerasurfaceview (context context, attributeset attrs) { this(context, attrs, 0); } public camerasurfaceview (context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); mcontext = context; try { msurfaceholder = getholder(); msurfaceholder.addcallback(this); msurfaceholder.settype(surfaceholder.surface_type_push_buffers); } catch (exception e) { e.printstacktrace(); } } @override public void surfacecreated(final surfaceholder surfaceholder) { if (mcamera == null) { try { mcamera = camera.open(); } catch (runtimeexception ignored) { } } try { if (mcamera != null) { windowmanager winmanager = (windowmanager) mcontext.getsystemservice(context.window_service); mcamera.setpreviewdisplay(msurfaceholder); } } catch (exception e) { if (mcamera != null) mcamera.release(); mcamera = null; } if (mcamera == null) { return; } else { mcamera.setpreviewcallback(new camera.previewcallback() { @override public void onpreviewframe(byte[] bytes, camera camera) { if (mparameters == null) { return; } bytearray = bytes; camerabitmap = getbitmap(); } }); } setwillnotdraw(false); } @override public void surfacechanged(surfaceholder surfaceholder, int format, int width, int height) { try { mparameters = mcamera.getparameters(); list<camera.size> camerasize = mparameters.getsupportedpreviewsizes(); mpreviewsize = camerasize.get(0); (camera.size s : camerasize) { if ((s.width * s.height) > (mpreviewsize.width * mpreviewsize.height)) { mpreviewsize = s; } } mparameters.setpreviewsize(mpreviewsize.width, mpreviewsize.height); mcamera.setparameters(mparameters); mcamera.startpreview(); } catch (exception e) { if (mcamera != null) { mcamera.release(); mcamera = null; } } } @override public void surfacedestroyed(surfaceholder surfaceholder) { if (mcamera != null) { mcamera.setpreviewcallback(null); mcamera.stoppreview(); mcamera.release(); mcamera = null; } } public bitmap getbitmap() { try { if (mparameters == null) return null; if (mpreviewsize == null) return null; int format = mparameters.getpreviewformat(); yuvimage yuvimage = new yuvimage(bytearray, format, mpreviewsize.width, mpreviewsize.height, null); bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream(); rect rect = new rect(0, 0, mpreviewsize.width, mpreviewsize.height); yuvimage.compresstojpeg(rect, 75, bytearrayoutputstream); bitmapfactory.options options = new bitmapfactory.options(); options.inpurgeable = true; options.ininputshareable = true; mbitmap = bitmapfactory.decodebytearray(bytearrayoutputstream.tobytearray(), 0, bytearrayoutputstream.size(), options); bytearrayoutputstream.flush(); bytearrayoutputstream.close(); } catch (ioexception ioe) { ioe.printstacktrace(); } return mbitmap; } public bitmap getcamerabitmap() { return camerabitmap; } }
any guidance appreciated~
to start working camera, must display camerasurfaceview - create "in air" not enough. please see recent related discussion: take photo using service on oneplus 1 - using windowmanager hack.
you don't need takepicture()
part, waiting preview frame cloned bytearray asynchronous.
the bottom line is, can not receive bitmap synchronously. can request bitmap, , have delivered in callback, e.g.
camerasurfaceview csv = new camerasurfaceview(getcontext()); ((activity) getcontext()).addcontentsview(csv); csv.requestbitmap(imageview);
and somewhere in camerasurfaceview.java:
public void onpreviewframe(byte[] bytes, camera camera) { bytearray = bytes; bitmap imgb = getbitmap(); imageview.setbitmap(imgb); }
Comments
Post a Comment