Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePicture,0);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// Bitmap bitmap =(Bitmap)data.getExtras().get("Data");
// imageView.setImageBitmap(bitmap);
FaceDetector detector;
ImageView myImageView = (ImageView) findViewById(R.id.ivImage);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable=true;
fileUri = getOutputMediaFileUri(FileColumns.MEDIA_TYPE_IMAGE);
final Bitmap myBitmap = BitmapFactory.decodeFile(fileUri.toString(),options);
// Bitmap myBitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.me);
Paint myRectPaint = new Paint();
myRectPaint.setStrokeWidth(5);
myRectPaint.setColor(Color.RED);
myRectPaint.setStyle(Paint.Style.STROKE);
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
tempCanvas.drawBitmap(myBitmap, 0, 0, null);
FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext()).setTrackingEnabled(false).build();
Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
SparseArray<com.google.android.gms.vision.face.Face> faces = faceDetector.detect(frame);
if(!faceDetector.isOperational())
{
new AlertDialog.Builder(this).setMessage("Face Detector :(").show();
return;
}
for(int i=0; i<faces.size(); i++)
{
Face thisFace = faces.valueAt(i);
float x1 = thisFace.getPosition().x;
float y1 = thisFace.getPosition().y;
float x2 = x1 + thisFace.getWidth();
float y2 = y1 + thisFace.getHeight();
tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);
}
myImageView.setImageDrawable(new BitmapDrawable(getResources(),tempBitmap));
}
错误
此时
12-04 19:54:41.419 14215-14215 / com.example.user.demo E / BitmapFactory: 无法解码流:java.io.FileNotFoundException: 文件:/storage/emulated/0/Pictures/IMAGE_DIRECTORY_NAME/IMG_20171204_195441.jpg: 打开失败:ENOENT(没有这样的文件或目录)12-04 19:54:41.462 14215-14215 / com.example.user.demo E / AndroidRuntime:FATAL EXCEPTION: 主要 处理:com.example.user.demo,PID:14215
java.lang.RuntimeException:传递结果失败 ResultInfo {who = null,request = 0,result = -1,data = Intent { act = inline-data dat = content:// media / external / images / media / 139760 typ = image / jpeg(有额外的)}}到活动 {com.example.user.demo / com.example.user.demo.MainActivity}: java.lang.NullPointerException:尝试调用虚方法'int 空对象引用上的android.graphics.Bitmap.getWidth()' 在com.example.user.demo.MainActivity.onActivityResult - &gt;&gt;&gt;。位图 tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(),Bitmap.Config.RGB_565);