2016-04-24 67 views
0

目前我正在使用Google Mobile Vision API進行人臉檢測。假設在屏幕旋轉後,活動將重新啓動,所有變量將重新初始化。但是,我知道爲什麼該應用程序停止工作並顯示NullPointerException。這是我的代碼。Android應用在屏幕旋轉後停止工作

public class MainActivity extends AppCompatActivity { 

    private ImageView mIvDetected; 
    private Bitmap mImage; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mIvDetected = (ImageView) findViewById(R.id.iv_detected); 

     //Load an image 
     BitmapFactory.Options opts = new BitmapFactory.Options(); 
     opts.inMutable = true; 
     mImage = BitmapFactory.decodeResource(getResources(), R.drawable.face2); 

     //Configure FaceDetector 
     FaceDetector detector = new FaceDetector.Builder(this) 
       .setMode(FaceDetector.ACCURATE_MODE) 
       .setLandmarkType(FaceDetector.ALL_LANDMARKS) 
       .setClassificationType(FaceDetector.ALL_CLASSIFICATIONS) 
       .setTrackingEnabled(false) 
       .build(); 

     if(false == detector.isOperational()){ 
      Toast.makeText(this, "Face detection service is not ready", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     //Add the image on a Frame object 
     Frame frame = new Frame.Builder().setBitmap(mImage).build(); 

     //Detect all faces from Frame object 
     SparseArray<Face> faceArray = detector.detect(frame); 

     //Do some drawing on faces 
     **Bitmap outBitmap = drawOnFace(faceArray);** 
     mIvDetected.setImageBitmap(outBitmap); 

     //Releasing the detector object 
     detector.release(); 
    } 

    /** Method to do some drawing on faces */ 
    private Bitmap drawOnFace(SparseArray<Face> faceArray){ 

     Paint mIdPaint = new Paint(); 
     mIdPaint.setColor(Color.RED); 
     mIdPaint.setTextSize(40.f); 

     Paint paint = new Paint(); 
     paint.setColor(Color.RED); 
     paint.setStrokeWidth(5); 
     paint.setStyle(Paint.Style.STROKE); 

     Paint mTextPaint = new Paint(); 
     mTextPaint.setAntiAlias(true); 
     mTextPaint.setDither(true); 
     mTextPaint.setTextSize(20); 
     mTextPaint.setColor(Color.GREEN); 
     mTextPaint.setStyle(Paint.Style.FILL); 

     // Create a blank bitmap same size with the source image 
     Bitmap outBitmap = Bitmap.createBitmap(mImage.getWidth(), mImage.getHeight(), Bitmap.Config.RGB_565); 

     // Put the bitmap on canvas for drawing 
     Canvas canvas = new Canvas(outBitmap); 
     canvas.drawBitmap(mImage, 0, 0, null); 

     for(int i=0; i < faceArray.size(); i++){ 
      Face face = faceArray.get(i); 

      //Drawing rectangle on each face 
      **drawRectangle(canvas, face.getPosition(), face.getWidth(), face.getHeight());** 

      //Drawing a point on each face features 
      for(Landmark landmark : face.getLandmarks()) { 
       switch (landmark.getType()){ 
        case Landmark.LEFT_EYE: 
         drawPoint(canvas, landmark.getPosition()); 
         canvas.drawText("Left eye: " + face.getIsLeftEyeOpenProbability(), landmark.getPosition().x, landmark.getPosition().y - 30, mTextPaint); 
         break; 
        case Landmark.RIGHT_EYE: 
         drawPoint(canvas, landmark.getPosition()); 
         canvas.drawText("Right eye: " + face.getIsRightEyeOpenProbability(), landmark.getPosition().x, landmark.getPosition().y - 30, mTextPaint); 
         break; 
        case Landmark.BOTTOM_MOUTH: 
         drawPoint(canvas, landmark.getPosition()); 
         canvas.drawText("Smile: " + face.getIsSmilingProbability(), landmark.getPosition().x, landmark.getPosition().y + 40, mTextPaint); 
         break; 
        case Landmark.LEFT_MOUTH: 
         drawPoint(canvas, landmark.getPosition()); 
         break; 
        case Landmark.RIGHT_MOUTH: 
         drawPoint(canvas, landmark.getPosition()); 
         break; 
        case Landmark.NOSE_BASE: 
         drawPoint(canvas, landmark.getPosition()); 
         break; 
        case Landmark.LEFT_CHEEK: 
         drawPoint(canvas, landmark.getPosition()); 
         break; 
        case Landmark.RIGHT_CHEEK: 
         drawPoint(canvas, landmark.getPosition()); 
         break; 
        case Landmark.LEFT_EAR: 
         drawPoint(canvas, landmark.getPosition()); 
         break; 
        case Landmark.LEFT_EAR_TIP: 
         drawPoint(canvas, landmark.getPosition()); 
         break; 
        case Landmark.RIGHT_EAR: 
         drawPoint(canvas, landmark.getPosition()); 
         break; 
        case Landmark.RIGHT_EAR_TIP: 
         drawPoint(canvas, landmark.getPosition()); 
         break; 
       } 
      } 

      /** Other useful details that may be of your interest */ 
      Log.d("", "FaceDetection- FaceId:" + face.getId() 
        + " Smiling:" + face.getIsSmilingProbability() 
        + " LeftEyeOpen:" + face.getIsLeftEyeOpenProbability() 
        + " RightEyeOpen:" + face.getIsRightEyeOpenProbability()); 


     } 

     return outBitmap; 
    } 

    /** This method draws a rectangle */ 
    private void drawRectangle(Canvas canvas, PointF point, float width, float height){ 
     Paint paint = new Paint(); 
     paint.setColor(Color.RED); 
     paint.setStrokeWidth(5); 
     paint.setStyle(Paint.Style.STROKE); 

     float x1 = point.x; 
     float y1 = point.y; 
     float x2 = x1 + width; 
     float y2 = y1 + height; 

     RectF rect = new RectF(x1, y1, x2, y2); 
     canvas.drawRect(rect, paint); 
    } 

    /** This method draws a point with hole */ 
    private void drawPoint(Canvas canvas, PointF point){ 
     Paint paint = new Paint(); 
     paint.setColor(Color.RED); 
     paint.setStrokeWidth(48); 
     paint.setStyle(Paint.Style.STROKE); 

     float x = point.x; 
     float y = point.y; 

     canvas.drawCircle(x, y, 1, paint); 
    } 
} 

這是我的錯誤。

12月4日至24日:59:50.231 2865年至2865年/ com.example.andy318.cameraone E/AndroidRuntime:致命異常:主 工藝:com.example.andy318.cameraone,PID:2865 的java。 lang.RuntimeException:無法恢復活動{com.example.andy318.cameraone/com.example.andy318.cameraone.MainActivity}:java.lang.NullPointerException:嘗試調用虛擬方法'android.graphics.PointF com.google.android (ActivityThread.java:3134)上的空對象引用 上的.gms.vision.face.Face.getPosition()'在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103) 上的空對象引用 (android.app.ActivityThread.handleResumeActivity)在android.app.ActivityThread.-wrap15(ActivityThread.java)上的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) .app.ActivityThread $ H.handleMessage(ActivityThread.java:1350) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android .app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ Method AndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起:java.lang.NullPointerException:嘗試調用虛擬方法'android.graphics。 PointF com.google.android.gms.vision.face.Face.getPosition()'位於空對象引用 處com.example.andy318.cameraone.MainActivity.drawOnFace(MainActivity.java:106) at com.example。 andy318.cameraone.MainActivity.onResume(MainActivity.java:69) 在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258) 在android.app.Activity.performResume(Activity.java:6312) 在機器人。 app.ActivityThread.performResumeActivity(ACTI (ActivityThread.java:9280) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) java:4077) at android.app.ActivityThread.-wrap15(ActivityThread.java) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1350) at android.os.Handler.dispatchMessage(Handler.java :102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main (ZygoteInit。的java:616)

錯誤發生在

位圖outBitmap = drawOnFace(faceArray);

的DrawRectangle(帆布,face.getPosition(),face.getWidth(),face.getHeight());

我把**放在編碼中。

我知道我的錯誤在哪裏,但我知道如何解決它。屏幕旋轉後,SparseArray faceArray具有空值。我試圖使用如果檢查空條件,但它不會顯示檢測。 (只顯示原圖)

+0

的可能的複製[什麼是空指針異常,並且我怎麼修復它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) –

+0

你只得到的關鍵,而不是對象:http://stackoverflow.com/a/8006994/6247492 – user6247492

回答

0

顯示java.lang.NullPointerException:試圖調用虛擬方法Face.getPosition()'上一個空對象引用

你的臉變量爲空!

http://developer.android.com/reference/android/media/FaceDetector.html

你在哪裏得到()從檢測? nvm,找到它了: https://developers.google.com/android/reference/com/google/android/gms/vision/face/FaceDetector.html#public-methods

也許你需要製作SparseArray裏面的最終或所有面或類似的東西?