0

特にアンドロイドが初めてです。私のプログラムがカメラを起動して画像を取得し、顔の検出に使用する顔の周りに長方形のウィンドウをペイントするアプリケーションを作成しています。今ここに私のコードがあります:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity {

final int CAMERA_PIC_REQUEST = 0;
private int imageWidth, imageHeight;
private int numberOfFace = 5;
private FaceDetector myFaceDetect; 
private FaceDetector.Face[] myFace;
float myEyesDistance;
int numberOfFaceDetected;
Bitmap thumbnail = null;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    Button strt = (Button) findViewById(R.id.start_camera);

        strt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent camera_intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(camera_intent, CAMERA_PIC_REQUEST);


                BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
                BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; 
                imageWidth = thumbnail.getWidth();
                imageHeight = thumbnail.getHeight();
                myFace = new FaceDetector.Face[numberOfFace];
                myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
                numberOfFaceDetected = myFaceDetect.findFaces(thumbnail, myFace);


                Canvas canvas = null;

                canvas.drawBitmap(thumbnail, 0, 0, null);

                Paint myPaint = new Paint();
                myPaint.setColor(Color.GREEN);
                myPaint.setStyle(Paint.Style.STROKE); 
               myPaint.setStrokeWidth(3);

                for(int i=0; i < numberOfFaceDetected; i++)
                {
                    Face face = myFace[i];
                    PointF myMidPoint = new PointF();
                    face.getMidPoint(myMidPoint);
                    myEyesDistance = face.eyesDistance();

                    canvas.drawRect(
                            (int)(myMidPoint.x - myEyesDistance),
                            (int)(myMidPoint.y - myEyesDistance),
                            (int)(myMidPoint.x + myEyesDistance),
                            (int)(myMidPoint.y + myEyesDistance),
                            myPaint);
         }                  
                       }
          });

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
    case 0:
        if(resultCode==RESULT_OK){

           thumbnail = (Bitmap) data.getExtras().get("data");

            }
    }
}


@Override
protected void onSaveInstanceState(Bundle outState) {

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Logcat は次を示します。

08-20 12:13:16.218: E/AndroidRuntime(26801): FATAL EXCEPTION: main
08-20 12:13:16.218: E/AndroidRuntime(26801): java.lang.RuntimeException: Unable to      resume activity {com.mh9.makeover/com.mh9.makeover.MainActivity}:   java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0,    result=-1, data=Intent { act=inline-data (has extras) }} to activity   {com.mh9.makeover/com.mh9.makeover.MainActivity}: java.lang.NullPointerException
08-20 12:13:16.218: E/AndroidRuntime(26801):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at android.os.Looper.loop(Looper.java:130)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at android.app.ActivityThread.main(ActivityThread.java:3687)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at java.lang.reflect.Method.invokeNative(Native Method)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at java.lang.reflect.Method.invoke(Method.java:507)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at dalvik.system.NativeStart.main(Native Method)
08-20 12:13:16.218: E/AndroidRuntime(26801): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.mh9.makeover/com.mh9.makeover.MainActivity}: java.lang.NullPointerException
08-20 12:13:16.218: E/AndroidRuntime(26801):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2111)
08-20 12:13:16.218: E/AndroidRuntime(26801):    ... 12 more
08-20 12:13:16.218: E/AndroidRuntime(26801): Caused by: java.lang.NullPointerException
08-20 12:13:16.218: E/AndroidRuntime(26801):    at com.mh9.makeover.MainActivity.onActivityResult(MainActivity.java:78)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
08-20 12:13:16.218: E/AndroidRuntime(26801):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
08-20 12:13:16.218: E/AndroidRuntime(26801):    ... 13 more

ここで 2 つの質問があります。1 つ目は、なぜこの例外が発生するのかということです。説明付きの修正をいただければ幸いです。2 番目の質問: やろうとしていることを達成できますか?? つまり、画像を撮って保存した後、顔の周りに長方形の窓を描くためにそれを渡したいのです!

4

1 に答える 1

3

次の行の onActivityResult() メソッドのカメラ インテントから余分なものがないため、NullPointerException が発生します。

thumbnail = (Bitmap) data.getExtras().get("data");

これは通常、一部の (高解像度) デバイスで発生します。これは、キャプチャした画像を保存するファイルを指定して、デバイスにカメラ インテントを設定する必要があるためです。

それ以外の :

  Intent camera_intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(camera_intent, CAMERA_PIC_REQUEST);

これを試して :

  // create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

onactivityresult メソッドの代わりに:

  if(resultCode==RESULT_OK){

       thumbnail = (Bitmap) data.getExtras().get("data");

        }

これを試して :

 if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
    if (resultCode == RESULT_OK) {
        // Image captured and saved to fileUri specified in the Intent
        Toast.makeText(this, "Image saved to:\n" +
                 data.getData(), Toast.LENGTH_LONG).show();
    } else if (resultCode == RESULT_CANCELED) {
        // User cancelled the image capture
    } else {
        // Image capture failed, advise user
    }
}

詳細については、こちらのドキュメントをご覧ください

頑張って!

于 2013-08-20T08:14:02.273 に答える