我正在尝试使用此项目从相机捕获和保存图像https://github.com/googlesamples/android-vision/tree/master/visionSamples/FaceTracker
捕捉图像的代码在面部跟踪活动java文件中
final CameraSource.ShutterCallback shutterCallback = new CameraSource.ShutterCallback() {
@Override
public void onShutter() {
Log.d(TAG, "onShutter");
Toast.makeText(view.getContext(),"sd",Toast.LENGTH_SHORT).show();
}
};
CameraSource.PictureCallback myPictureCallback_JPG = new CameraSource.PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0) {
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
//save file
String path = Environment.DIRECTORY_DCIM.toString();
//Toast.makeText(getApplicationContext(),path,Toast.LENGTH_SHORT).show();
OutputStream fOut = null;
File file = new File(path, "FitnessGirl"+".jpg"); // the File to save to
try{
fOut = new FileOutputStream(file);
bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
fOut.flush();
fOut.close(); // do not forget to close the stream
}catch (FileNotFoundException exception){
}catch (IOException e){
}
//save file
}};
mCameraSource.takePicture(shutterCallback, myPictureCallback_JPG);
上面的代码不起作用,有人可以帮忙吗?
对于任何发现这个的人来说,变量路径有问题只需改变
//String path = getExternalFilesDir(Environment.DIRECTORY_DCIM).getPath();
to:
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
并添加行 file.createNewFile(); //新文件后
File file = new File(path, "FitnessGirl"+".jpg");
try {
file.createNewFile();
}catch(Exception e){
e.printStackTrace();
}