Android camera preview capture/resizing captured image to match preview -
i trying create , application requires me take captured image , place on current camera preview 0.5 alpha. "overlay" image serves guide capturing next picture; client use overlay compare current frame in preview captured image. simple way approach retrieve last file captured , set view. however, captured image not match camera previews resolution , aspect ratio, causing overlay , preview mismatch in representation of objects (the same object can vary in size between preview , captured image). have tried:
- using camera preview callbacks store current frame data, inefficient , doesnt work (also has slight translation, dimensions accurate)
- using aforementioned previous-file method , trying scale down or scale accordingly, match aspect ratio (maybe math wrong [i used aspect ratio of preview calculate area in original image] , still possible use approach? if way in on appreciated).
- researching ways capture contents of surface view, no avail.
currently, 2 possible methods insight on how approach be:
- freeze surfaceview somehow (locking canvas of sort), set opacity , use surfaceview display camera (maybe inefficient, clunky workaround?).
- capture contents of surfaceview , store in bitmap when picturecallback called (preferred).
ofcourse, other suggestions appreciated.
note: there similar questions , i've spent hours going through them none of them correctly address issue or give enough detail.
i got work using 2 textureview components:
- one set 0.5 alpha , lays on top of other using relativelayout.
- have opaque textureview register surfacetexturelistener
- start camera triggered onsurfacetextureavailable in said listener
- set given surfacetexture previewtexture
- at button press, draw getbitmap() on canvas of transparent textureview
activity.java (implementing surfacetexturelistener):
@override public void onclick(view view) { { canvas canvas = alphacameratextureview.lockcanvas(); canvas.drawbitmap( cameratextureview.getbitmap(), 0, 0, null ); alphacameratextureview.unlockcanvasandpost( canvas ); } @override public void onsurfacetextureavailable( surfacetexture surfacetexture, int width, int height ) { try { camera = camera.open(); camera.setdisplayorientation( 90 ); camera.setpreviewtexture( surfacetexture ); camera.startpreview(); } catch (ioexception e ) { e.printstacktrace(); } } @override public boolean onsurfacetexturedestroyed( surfacetexture surfacetexture ) { camera.stoppreview(); camera.release(); return false; }
textureviews in layout.xml:
<textureview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/cameratextureview" android:layout_weight="0"/> <textureview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/alphacameratextureview" android:alpha="0.5" android:layout_weight="0" />