问题
I'm trying to get current frame as a Bipmap I'm running into these errors:
09-03 08:12:33.353 28008-28204/com.google.android.gms.samples.vision.face.facetracker E/CameraSource: Exception thrown from receiver.
java.lang.ArrayIndexOutOfBoundsException
at android.graphics.Bitmap.checkPixelsAccess(Bitmap.java:1492)
at android.graphics.Bitmap.getPixels(Bitmap.java:1433)
at com.google.android.gms.samples.vision.face.facetracker.FaceTrackerActivity$MyFaceDetector.detect(FaceTrackerActivity.java:318)
at com.google.android.gms.vision.Detector.receiveFrame(Unknown Source)
at com.google.android.gms.vision.CameraSource$zzb.run(Unknown Source)
at java.lang.Thread.run(Thread.java:761)
Face Detection not don't appear on the screen. This is my Main_activity (colled FaceTrackingActivity):
private void createCameraSource() {
Context context = getApplicationContext();
FaceDetector detector = new FaceDetector.Builder(context)
.setClassificationType(FaceDetector.ALL_CLASSIFICATIONS)
.setMode(FaceDetector.ACCURATE_MODE)
.setLandmarkType(FaceDetector.ALL_LANDMARKS)
.build();
MyFaceDetector myFaceDetector = new MyFaceDetector(detector);
myFaceDetector.setProcessor(
new MultiProcessor.Builder<>(new GraphicFaceTrackerFactory())
.build());
if (!detector.isOperational()) {
Log.w(TAG, "Face detector dependencies are not yet available.");
}
mCameraSource = new CameraSource.Builder(context, myFaceDetector)
.setRequestedPreviewSize(640, 480)
.setFacing(CameraSource.CAMERA_FACING_FRONT)
.setRequestedFps(30.0f)
.build();
}
and This is a code part where I'm trying to get a Bipmap image:
class MyFaceDetector extends Detector<Face> {
private Detector<Face> mDelegate;
MyFaceDetector(Detector<Face> delegate) {
mDelegate = delegate;
}
public SparseArray<Face> detect(Frame frame) {
YuvImage yuvImage = new YuvImage(frame.getGrayscaleImageData().array(), ImageFormat.NV21, frame.getMetadata().getWidth(), frame.getMetadata().getHeight(), null); // Create YUV image from byte[]
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, frame.getMetadata().getWidth(), frame.getMetadata().getHeight()), 100, byteArrayOutputStream);// Convert YUV image to Jpeg
byte[] jpegArray = byteArrayOutputStream.toByteArray();
Bitmap bmp = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length); // Convert Jpeg to Bitmap
//image processing part
}
}
In particular, inside createCameraSOurce part, if I don't write FaceDetector before detector and if I use the string _detector.setProcessor(...)_instead myFaceDetector.setProcessor(...) face appear and app run without errors but myFaceDetector of "MyFaceDetector myFaceDetector = new MyFaceDetector(detector);" is never used. I would like to process only the face detection box, but when I try to change something, I'm wrong
来源:https://stackoverflow.com/questions/52145789/android-studio-face-detection-failed-e-camerasource-exception-thrown-from-rec