camerasource takepicture无效

时间:2016-04-19 10:29:44

标签: java android android-camera

我正在尝试使用此项目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);

上面的代码不起作用,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您可以使用此代码从相机拍摄照片:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA)}
            onCaptureImageResult(data);
        }
    }
}

 private void onCaptureImageResult(Intent data) {
    Bitmap bitmap = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    img_upload_pic.setImageBitmap(bitmap);
}

在您的Menifest文件中添加权限:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />