我在我的应用程序中使用Mobile Vision API进行人脸检测,到目前为止我已经成功地这样做了。它适用于我第一次运行应用程序时设置的特定图像,但在那之后。。我试图替换不同的图像进行人脸检测,它给出了错误java.lang.OutOfMemoryError
以下是我的代码
Bitmap myBitmap;
FaceDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Load An Image////
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable=true;
Bitmap b = = BitmapFactory.decodeResource(
getApplicationContext().getResources(),
R.drawable.image,options);
myBitmap = b.copy(Bitmap.Config.RGB_565, true);
b.recycle();
////////////////
detector = new FaceDetector.Builder(getApplicationContext())
.setTrackingEnabled(false)
.setLandmarkType(FaceDetector.ALL_LANDMARKS)
.build();
if (!detector.isOperational())
{ Toast.makeText(MainActivity.this, "NO face deteted."Toast.LENGTH_SHORT)
.show();}
else {
Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
SparseArray<com.google.android.gms.vision.face.Face> faces = detector.detect(frame);
FaceView faceView = (FaceView) findViewById(R.id.faceView);
faceView.setContent(myBitmap, faces);
}
这是我的logcat
java.lang.OutOfMemoryError
at com.google.android.gms.vision.Frame.zzEx(Unknown Source)
at com.google.android.gms.vision.Frame.getGrayscaleImageData(Unknown Source)
at com.google.android.gms.vision.face.FaceDetector.detect(Unknown Source)
at com.chat.elearnplayer.mobilevisionapi.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5296)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5426)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
需要你的帮助。。。
试试这个
Bitmap myBitmap;
FaceDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Load An Image////
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable=true;
Bitmap b = = BitmapFactory.decodeResource(
getApplicationContext().getResources(),
R.drawable.image,options);
myBitmap = b.copy(Bitmap.Config.RGB_565, true);
b.recycle();
//////////////////
detector = new FaceDetector.Builder(getApplicationContext())
.setTrackingEnabled(false)
.setLandmarkType(FaceDetector.ALL_LANDMARKS)
.build();
Detector<Face> safeDetector = new SafeFaceDetector(detector);
Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
SparseArray<com.google.android.gms.vision.face.Face> faces = safeDetector.detect(frame);
if (!safeDetector.isOperational()) {
Toast.makeText(MainActivity.this, "NO face deteted."Toast.LENGTH_SHORT)
.show();
} else {
FaceView faceView = (FaceView) findViewById(R.id.faceView);
faceView.setContent(myBitmap, faces);
safeDetector.release();
}
}
为了重复使用,必须在使用之后调用面部检测器CCD_ 1。
对于SafeDetector
的使用:人脸检测器有一个小图像的错误,而safeDetector是该错误的补丁
在活动导入时添加此行
import com.google.android.gms.samples.vision.face.patch.SafeFaceDetector;