Custom camera class in android -


i create camerasurfaceview.java

and added camerasurfaceview in containerview.

also, input permission , feature.

<uses-permission android:name="android.permission.camera" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> 

but doesn't work.

how fix this?

here sample code,

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     tools:context=".mainactivity">      <framelayout         android:id="@+id/camerapreview"         android:layout_width="350dp"         android:layout_height="350dp"         android:layout_centervertical="true"         android:layout_centerhorizontal="true" />  </relativelayout> 

mainactivity.java

import android.app.activity; import android.content.pm.activityinfo; import android.graphics.pixelformat; import android.os.bundle; import android.widget.framelayout;  public class mainactivity extends activity {     framelayout containerview;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         getwindow().setformat(pixelformat.unknown);          containerview= (framelayout)findviewbyid(r.id.camerapreview);          camerasurfaceview camerasurfaceview = new camerasurfaceview(mainactivity.this);         containerview.addview(camerasurfaceview);     }  } 

camerasurfaceview.java

import android.content.context; import android.hardware.camera; import android.view.surfaceholder; import android.view.surfaceview;  import java.io.ioexception;  public class camerasurfaceview extends surfaceview implements surfaceholder.callback { camera camera = null; surfaceholder surfaceholder; boolean previewing = false;  public camerasurfaceview(context context) {     super(context);     surfaceholder = getholder();     if (surfaceholder != null) {         surfaceholder.addcallback(this);     } }   @override public void surfacechanged(surfaceholder holder, int format, int width, int height) {     if(previewing){         camera.stoppreview();         previewing = false;     }      if (camera != null){         try {             camera.setpreviewdisplay(surfaceholder);             camera.startpreview();             previewing = true;         } catch (ioexception e) {             e.printstacktrace();         }     } }  @override public void surfacecreated(surfaceholder holder) {     camera = camera.open();     camera.setdisplayorientation(90); }  @override public void surfacedestroyed(surfaceholder holder) {     camera.stoppreview();     camera.release();     camera = null;     previewing = false; } 

thanks.

you need pass camera object in camerasurfaceview

 public camerasurfaceview(context context,camera camera) {         super(context);         this.camera = camera;         surfaceholder = getholder();         if (surfaceholder != null) {             surfaceholder.addcallback(this);         }     } 

in main activty need open camera //initialize,

private camera mcamera;      camerasurfaceview camerasurfaceview = new camerasurfaceview(mainactivity.this,mcamera); 

Comments