画像の一覧を表示して、その画像のファイルパスを
顔検出のメソッドに渡したいです。
しかし以下のエラーがでて、うまくいきません。
修正すべき部分があれば提案してください。
よろしくお願いします。
参考サイト
http://workpiles.com/2014/07/android-gallary-intent/
https://seesaawiki.jp/w/moonlight_aska/d/%b4%e9%a4%f2%b8%a1%bd%d0%a4%b9%a4%eb
2020-03-30 18:16:21.538 10387-10387/com.example.face_7 E/.example.face_: Unknown bits set in runtime_flags: 0x8000 2020-03-30 18:16:21.669 10387-10423/com.example.face_7 E/Perf: Fail to get file list com.example.face_7 2020-03-30 18:16:21.669 10387-10423/com.example.face_7 E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array 2020-03-30 18:16:21.669 10387-10423/com.example.face_7 E/Perf: Fail to get file list com.example.face_7 2020-03-30 18:16:21.669 10387-10423/com.example.face_7 E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
画像のファイルパスを取得するメソッドです。
public class MainActivity extends AppCompatActivity { private final int MAX_FACE_NUM = 3; private FaceDetector.Face[] mFaces = new FaceDetector.Face[MAX_FACE_NUM]; private Bitmap mBmpRes = null; private ImageView mView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startActivity(); } public void startActivity() { if (Build.VERSION.SDK_INT < 19) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/jpeg"); startActivityForResult(Intent.createChooser(intent, "Pick a source"), 0); } else { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/jpeg"); startActivityForResult(Intent.createChooser(intent, "Pick a source"), 1); } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) return; if (requestCode == 0) { String[] columns = {MediaStore.MediaColumns.DATA}; Cursor cursor = getContentResolver().query(data.getData(), columns, null, null, null); if (cursor.moveToFirst()) { ReadPicture(cursor.getString(0)); //ここで渡します! } } else if (requestCode == 1) { String id = DocumentsContract.getDocumentId(data.getData()); String selection = "_id=?"; String[] selectionArgs = new String[]{id.split(":")[1]}; Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.MediaColumns.DATA}, selection, selectionArgs, null); if (cursor.moveToFirst()) { ReadPicture(cursor.getString(0)); //ここで渡します! } cursor.close(); } }
顔検出のメソッドです。
private void ReadPicture(String paths) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; Bitmap bmpOrg = BitmapFactory.decodeFile(paths, options); mBmpRes = bmpOrg.copy(Bitmap.Config.RGB_565, true); if (bmpOrg != null) { detectFace(bmpOrg); mView = (ImageView) findViewById(R.id.imageView); mView.setImageBitmap(mBmpRes); } } private void detectFace(Bitmap img) { FaceDetector detector = new FaceDetector(img.getWidth(), img.getHeight(),MAX_FACE_NUM); int num = detector.findFaces(img, mFaces); for (int i = 0; i < num; i++) { PointF mid = new PointF(); mFaces[i].getMidPoint(mid); setMark(mFaces[i]); } } private void setMark(FaceDetector.Face face) { Canvas canvas = new Canvas(mBmpRes); Paint paint = new Paint(); paint.setColor(Color.RED); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); PointF mid = new PointF(); face.getMidPoint(mid); canvas.drawCircle(mid.x, mid.y,face.eyesDistance(), paint); }
あなたの回答
tips
プレビュー