1. public void onPreviewFrame(byte[] data, Camera camera) {
2. Log.d(TAG, "onPreviewFrame");
3. // face detection: first convert the image from NV21 to RGB_565
4. YuvImage yuv = new YuvImage(data, ImageFormat.NV21,
i. mWorkBitmap.getWidth(), mWorkBitmap.getHeight(), null);
5. // TODO: make rect a member and use it for width and height values above
6. Rect rect = new Rect(0, 0, mWorkBitmap.getWidth(), mWorkBitmap.getHeight());
7. // TODO: use a threaded option or a circular buffer for converting streams?
8. //see http://ostermiller.org/convert_java_outputstream_inputstream.html
9. ByteArrayOutputStream baout = new ByteArrayOutputStream();
10. if (!yuv.compressToJpeg(rect, 100, baout)) {
11. Log.e(TAG, "compressToJpeg failed");
12. }
13. BitmapFactory.Options bfo = new BitmapFactory.Options();
14. bfo.inPreferredConfig = Bitmap.Config.RGB_565;
15. mWorkBitmap = BitmapFactory.decodeStream(
16. new ByteArrayInputStream(baout.toByteArray()), null, bfo);
17. Arrays.fill(mFaces, null); // use arraycopy instead?
18. Arrays.fill(eyesMidPts, null); // use arraycopy instead?
19. mFaceDetector.findFaces(mWorkBitmap, mFaces);
20. Log.d(TAG, ""+ mFaces.length);
21. for (int i = 0; i < mFaces.length; i++)
22. {
23. face = mFaces[i];
24. try {
25. PointF eyesMP = new PointF();
26. face.getMidPoint(eyesMP);
27. eyesDistance[i] = face.eyesDistance();
28. eyesMidPts[i] = eyesMP;
29. Log.i("Face",
30. i + " " + face.confidence() + " " + face.eyesDistance() + " "
31. + "Pose: ("+ face.pose(FaceDetector.Face.EULER_X) + ","
32. + face.pose(FaceDetector.Face.EULER_Y) + ","
33. + face.pose(FaceDetector.Face.EULER_Z) + ")"
34. + "Eyes Midpoint: ("+eyesMidPts[i].x + "," + eyesMidPts[i].y +")"
35. );
36. }
37. catch (Exception e)
38. {
39. if (true) Log.e("Face", i + " is null");
40. }
41. }
42. invalidate(); // use a dirty Rect?
43. // Requeue the buffer so we get called again
44. mCamera.addCallbackBuffer(data);
45. }
|