I am trying to program an application that should display on the screen of a mobile phone what is being removed from the front camera [The application does not record or save anything in the phone’s memory]. Also, in the case when the face is removed (and detected), it should appear with the help of a rectangle.
For this, I use:
- A Surfaceview to display what is captured by the front camera.
- A FaceDetectionListener for detecting faces at the camera input.
While the application correctly displays what is removed by the front camera. He also discovers the right faces. But I can’t draw a border rectangle around the detected face.
Here are some snippets to show how I'm trying to solve a problem.
Activity:
@SuppressLint("InflateParams")
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class FaceDetectorTutorial extends Activity {
MySurface mMySurface;
private SurfaceView surfaceView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_face_detector_tutorial);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
surfaceView = (SurfaceView)findViewById(R.id.camPreview);
mMySurface = new MySurface(this, surfaceView);
}
}
MySurface Class:
class MySurface extends SurfaceView implements SurfaceHolder.Callback {
Paint paint = new Paint();
public Camera camera;
String Tag = "Log: ";
private SurfaceHolder surfaceHolder;
boolean preview = false;
MySurface(Context context, SurfaceView msurfaceView) {
super(context);
paint.setColor(Color.RED);
paint.setStrokeWidth(3);
surfaceHolder = msurfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@SuppressLint("NewApi")
FaceDetectionListener faceDetectionListener = new FaceDetectionListener(){
@Override
public void onFaceDetection(Face[] faces, Camera camera) {
if (faces.length > 0){
Rect Boundary = faces[0].rect;
System.out.println(Boundary);
tryDrawing(Boundary);
}
}};
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
if(preview){
camera.stopFaceDetection();
camera.stopPreview();
preview = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
camera.startFaceDetection();
preview = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
int cameraId = -1;
int numberOfCameras = camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
cameraId = i;
break;
}
}
camera = Camera.open(cameraId);
camera.setFaceDetectionListener(faceDetectionListener);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopFaceDetection();
camera.stopPreview();
camera.release();
camera = null;
preview = false;
}
private void tryDrawing(Rect Boundary) {
Log.i(Tag, "Trying to draw...");
Canvas canvas = surfaceHolder.lockCanvas();
if (canvas == null) {
Log.e(Tag, "Cannot draw onto the canvas as it null");
} else {
drawMyStuff(canvas,Boundary);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
private void drawMyStuff(final Canvas canvas, Rect Boundary) {
canvas.drawRect(Boundary.left, Boundary.top, Boundary.right, Boundary.bottom, paint);
Log.i(Tag, "Drawing...");
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/camPreview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
From logcat, I understand that the error blocks the surface. But I do not understand why.
11-23 17:13:51.791: I/System.out(12515): Rect(-187, -495 - 328, 196)
11-23 17:13:51.791: I/Log:(12515): Trying to draw...
11-23 17:13:51.791: E/SurfaceHolder(12515): Exception locking surface
11-23 17:13:51.791: E/SurfaceHolder(12515): java.lang.IllegalArgumentException
11-23 17:13:51.791: E/SurfaceHolder(12515): at android.view.Surface.nativeLockCanvas(Native Method)
11-23 17:13:51.791: E/SurfaceHolder(12515): at android.view.Surface.lockCanvas(Surface.java:452)
11-23 17:13:51.791: E/SurfaceHolder(12515): at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:781)
11-23 17:13:51.791: E/SurfaceHolder(12515): at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:757)
11-23 17:13:51.791: E/SurfaceHolder(12515): at com.example.facedetectiontutorial.MySurface.tryDrawing(FaceDetectorTutorial.java:175)
11-23 17:13:51.791: E/SurfaceHolder(12515): at com.example.facedetectiontutorial.MySurface.access$0(FaceDetectorTutorial.java:172)
11-23 17:13:51.791: E/SurfaceHolder(12515): at com.example.facedetectiontutorial.MySurface$1.onFaceDetection(FaceDetectorTutorial.java:109)
11-23 17:13:51.791: E/SurfaceHolder(12515): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:815)
11-23 17:13:51.791: E/SurfaceHolder(12515): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 17:13:51.791: E/SurfaceHolder(12515): at android.os.Looper.loop(Looper.java:137)
11-23 17:13:51.791: E/SurfaceHolder(12515): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-23 17:13:51.791: E/SurfaceHolder(12515): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 17:13:51.791: E/SurfaceHolder(12515): at java.lang.reflect.Method.invoke(Method.java:511)
11-23 17:13:51.791: E/SurfaceHolder(12515): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-23 17:13:51.791: E/SurfaceHolder(12515): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-23 17:13:51.791: E/SurfaceHolder(12515): at dalvik.system.NativeStart.main(Native Method)
11-23 17:13:51.893: E/Log:(12515): Cannot draw onto the canvas as it null
I made a decision in the accepted answer to the following question:
Android drawing on surface and canvas
But it looks like I'm applying it wrong. Can someone tell me what I have to change?