android - How can I show image taken from camera -
i'm trying write android app, works camera. show preview of camera in textureview, working. if press button camera take picture , show on imageview on second half of screen. everytime press button app stops working, tells me error
android.view.viewrootimpl$calledfromwrongthreadexception: original thread created view hierarchy can touch views.
so looked up, , seems can't change image of imageview try to. don't know how else solve problem. can give me advise? here code: image changed in "imagereader.onimageavailablelistener" in method takepicture().
public class mainactivityold extends activity { private textureview mtextureview; private imageview mimageview; private cameradevice mcameradevice; private size mpreviewsize; private capturerequest.builder mpreviewbuilder; private cameracapturesession mpreviewsession; private button mbtnshot; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //no titlebar requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_main); mtextureview = (textureview) findviewbyid(r.id.texture); mtextureview.setsurfacetexturelistener(msurfacelistener); mimageview = (imageview) findviewbyid(r.id.lastpicture); mbtnshot = (button) findviewbyid(r.id.btn_takepicture); mbtnshot.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { takepicture(); } }); } private void takepicture() { log.e("mytag", "take picture"); cameramanager manager = (cameramanager) getsystemservice(context.camera_service); try { cameracharacteristics characteristics = manager.getcameracharacteristics(mcameradevice.getid()); size[] jpegsizes = characteristics.get(cameracharacteristics.scaler_stream_configuration_map).getoutputsizes(imageformat.jpeg); int width = jpegsizes[0].getwidth(); int height = jpegsizes[0].getheight(); imagereader reader = imagereader.newinstance(width, height, imageformat.jpeg, 1); list<surface> outputsurfaces = new arraylist<surface>(2); outputsurfaces.add(reader.getsurface()); outputsurfaces.add(new surface(mtextureview.getsurfacetexture())); final capturerequest.builder capturebuilder = mcameradevice.createcapturerequest(cameradevice.template_still_capture); capturebuilder.addtarget(reader.getsurface()); capturebuilder.set(capturerequest.control_mode, camerametadata.control_mode_auto); imagereader.onimageavailablelistener readerlistener = new imagereader.onimageavailablelistener(){ @override public void onimageavailable(imagereader reader) { image image = reader.acquirelatestimage(); bytebuffer buffer = image.getplanes()[0].getbuffer(); byte[] bytes = new byte[buffer.capacity()]; buffer.get(bytes); bitmap bitmap = bitmapfactory.decodebytearray(bytes, 0, bytes.length); mimageview.setimagebitmap(bitmap); } }; handlerthread thread = new handlerthread("camerapicture"); thread.start(); final handler backgroundhandler = new handler(thread.getlooper()); reader.setonimageavailablelistener(readerlistener,backgroundhandler); final cameracapturesession.capturecallback capturelistener = new cameracapturesession.capturecallback() { @override public void oncapturecompleted(cameracapturesession session, capturerequest request, totalcaptureresult result) { super.oncapturecompleted(session, request, result); startpreview(); } }; mcameradevice.createcapturesession(outputsurfaces, new cameracapturesession.statecallback() { @override public void onconfigured(cameracapturesession session) { try { session.capture(capturebuilder.build(), capturelistener, backgroundhandler); } catch (cameraaccessexception e) { e.printstacktrace(); } } @override public void onconfigurefailed(cameracapturesession session) { } }, backgroundhandler); } catch (cameraaccessexception e) { e.printstacktrace(); } } private void opencamera() { cameramanager manager = (cameramanager) getsystemservice(context.camera_service); try { string cameraid = manager.getcameraidlist()[0]; cameracharacteristics characteristics = manager.getcameracharacteristics(cameraid); streamconfigurationmap map = characteristics.get(cameracharacteristics.scaler_stream_configuration_map); mpreviewsize = map.getoutputsizes(surfacetexture.class)[0]; manager.opencamera(cameraid, mstatecallback, null); } catch (cameraaccessexception e) { e.printstacktrace(); } log.e("mytag", "opencamera"); } private textureview.surfacetexturelistener msurfacelistener = new textureview.surfacetexturelistener() { @override public void onsurfacetextureavailable(surfacetexture surface, int width, int height) { log.e("mylog", "onsurfacetextureavailable, width=" + width + ",height=" + height); opencamera(); } @override public void onsurfacetexturesizechanged(surfacetexture surface, int width, int height) { } @override public boolean onsurfacetexturedestroyed(surfacetexture surface) { return false; } @override public void onsurfacetextureupdated(surfacetexture surface) { } }; private cameradevice.statecallback mstatecallback = new cameradevice.statecallback() { @override public void onopened(cameradevice camera) { log.e("mytag", "onopened"); mcameradevice = camera; startpreview(); } @override public void ondisconnected(cameradevice camera) { log.e("mytag", "ondisconnected"); } @override public void onerror(cameradevice camera, int error) { log.e("mytag", "ondisconnected"); } }; protected void startpreview() { if (mcameradevice == null || mtextureview.isavailable() || mpreviewsize == null) { log.e("mytag", "startpreview failed, still working, ignore it"); // return; } surfacetexture texture = mtextureview.getsurfacetexture(); texture.setdefaultbuffersize(mpreviewsize.getwidth(), mpreviewsize.getheight()); surface surface = new surface(texture); try { mpreviewbuilder = mcameradevice.createcapturerequest(cameradevice.template_preview); } catch (cameraaccessexception e) { e.printstacktrace(); } mpreviewbuilder.addtarget(surface); try { mcameradevice.createcapturesession(arrays.aslist(surface), new cameracapturesession.statecallback() { @override public void onconfigured(cameracapturesession session) { mpreviewsession = session; updatepreview(); } @override public void onconfigurefailed(cameracapturesession session) { toast.maketext(mainactivityold.this, "onconfigurefailed", toast.length_long).show(); } }, null); } catch (cameraaccessexception e) { e.printstacktrace(); } } protected void updatepreview() { mpreviewbuilder.set(capturerequest.control_mode, camerametadata.control_mode_auto); handlerthread thread = new handlerthread("camerapreview"); thread.start(); handler backgroundhandler = new handler(thread.getlooper()); try { mpreviewsession.setrepeatingrequest(mpreviewbuilder.build(), null, backgroundhandler); } catch (cameraaccessexception e) { e.printstacktrace(); } } }
i think better of trying stream surface view, not sure if can done imageview.
try tutorial should going in right direction
or this more date example provided google
Comments
Post a Comment