我正在尝试获取当前的帧作为Bipmap,但遇到了以下错误:
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)
人脸检测未出现在屏幕上。 这是我的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();
}
这是我尝试获取Bipmap图片的代码部分:
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
}
}
尤其是在createCameraSOurce部分中,如果我没有在 detector 之前写 FaceDetector ,并且如果我使用字符串_detector.setProcessor(...)_ instead myFaceDetector.setProcessor(...)面孔出现并且应用程序运行没有错误,但“ MyFaceDetector myFaceDetector = new MyFaceDetector(detector);”的myFaceDetector从未使用过。 我只想处理人脸检测框,但是当我尝试更改某些东西时,我错了