Android Studio, Capture FrameLayout View, While Camera is StopPreview() -


this cameraview.java (i got http://blog.rhesoft.com/)

public class cameraview extends surfaceview implements surfaceholder.callback{   private surfaceholder mholder; private camera mcamera;  public cameraview(context context, camera camera){     super(context);     mcamera = camera;     mcamera.setdisplayorientation(90);     mholder = getholder();     mholder.addcallback(this);     mholder.settype(surfaceholder.surface_type_normal); }  @override public void surfacecreated(surfaceholder surfaceholder) {     try{         mcamera.setpreviewdisplay(surfaceholder);         mcamera.startpreview();     } catch (ioexception e) {         log.d("error", "camera error on surfacecreated " + e.getmessage());     } }  @override public void surfacechanged(surfaceholder surfaceholder, int i, int i2, int i3) {     if(mholder.getsurface() == null)         return;     try{         mcamera.stoppreview();     } catch (exception e){      }      try{         mcamera.setpreviewdisplay(mholder);         mcamera.startpreview();     } catch (ioexception e) {         log.d("error", "camera error on surfacechanged " + e.getmessage());     } }  @override public void surfacedestroyed(surfaceholder surfaceholder) {     mcamera.stoppreview();     mcamera.release(); }} 

and mainactivity.java.

public class mainactivity extends appcompatactivity {  private camera mcamera = null; private cameraview mcameraview = null; private framelayout camera_view;  protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      try{         mcamera = camera.open();         //you can use open(int) use different cameras     } catch (exception e){         log.d("error", "failed camera: " + e.getmessage());     }      if(mcamera != null) {         mcameraview = new cameraview(this, mcamera);//create surfaceview show camera data         camera_view = (framelayout)findviewbyid(r.id.camera_view);         camera_view.addview(mcameraview);//add surfaceview layout     }      //btn close application     final imagebutton imgclose = (imagebutton)findviewbyid(r.id.imgclose);     final imagebutton capimg = (imagebutton) findviewbyid(r.id.imgcapture);     imgclose.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             imgclose.setvisibility(view.invisible);             capimg.setvisibility(view.visible);             mcamera.startpreview();         }     });     capimg.setonclicklistener(new view.onclicklistener(){         public void onclick(view v){             mcamera.stoppreview();             imgclose.setvisibility(view.visible);             capimg.setvisibility(view.invisible);         }     }); }  @override public void onbackpressed(){     system.exit(0); }} 

and activity_main.xml.

<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity">  <framelayout     android:id="@+id/camera_view"     android:layout_width="match_parent"     android:layout_height="match_parent">  </framelayout>  <imagebutton     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/imgclose"     android:layout_gravity="right|top"     android:background="@android:drawable/ic_menu_close_clear_cancel"     android:padding="20dp"     android:visibility="invisible" />  <imagebutton     android:layout_width="98dp"     android:layout_height="98dp"     android:id="@+id/imgcapture"     android:layout_gravity="center_horizontal|bottom"     android:background="@android:drawable/ic_menu_camera"     android:padding="20dp"/> 

can capture framelayout preview image or programing preview delete red color? can give me clue?

so if understand correctly, wish image data shown when stop preview? if can mcamera.takepicture() method. takes 3 parameters, of useful callbacks. here did show you.

        btn_capture.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {            if (mcamera == null)                 return;             mcamera.takepicture(null, null, mpicture);     }     }); 

this button click listener floating image button (any button work fine). third parameter callback returns array of pixels can convert bitmap.

private camera.picturecallback mpicture = new camera.picturecallback() {     @override     public void onpicturetaken(byte[] data, camera camera) {         mcamera.stoppreview();         bitmap bmap = bitmapfactory.decodebytearray(data, 0, data.length);         preview.removeview(mpreview);         img_captured.setimagebitmap(bmap);     } }; 

this callback passed in takepicture() method. byte[] data image trying get. can see converted bitmap , displayed imageview after removing surfaceview (which holds camera preview). note, takepicture() method stops preview automatically don't stop preview before taking photo. can how did in callback. also, if want take photo, can start preview again.

i hope helps!! let me know if left out! way, documented on android developer site.

http://developer.android.com/training/camera/cameradirect.html#tasktakepicture


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -