【问题标题】:Take a picture without opening the Camera application在不打开相机应用程序的情况下拍照
【发布时间】:2019-12-06 15:11:06
【问题描述】:

我目前有一个带有拍照按钮的应用程序。但是,当我单击按钮时,默认相机应用程序会打开,然后您必须手动拍照。如何让相机应用程序无法打开,而只是自动拍照并只需按下应用程序中的按钮即可保存?我想按下我创建的按钮,它会拍照并自动保存。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    //for taking photos
    static final int REQUEST_IMAGE_CAPTURE = 1;
    String currentPhotoPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public void onResume(){
        super.onResume();

    }

//button to start image capturing process
    public void startImageCapture(View view){

        dispatchTakePictureIntent();
        galleryAddPic();

    }

//method for taking a photo
    public void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //Ensure that there is a camera activity to handle the intent
        if(takePictureIntent.resolveActivity(getPackageManager()) != null){
            //create the file where the photo should go
            File photoFile = null;
            try{
                photoFile = createImageFile();
            }catch(IOException e){
                Log.i("ERROR","Error in trying to create file for image");
            }
            //continue only if the file was successfully created
            if (photoFile != null){
                Uri photoURI = FileProvider.getUriForFile(this,"com.example.MyProject.provider",photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
                startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE);
            }

        }
    }


    private File createImageFile() throws IOException{
        //Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName, //prefix
                ".jpg", //suffix
                storageDir //directory
        );

        //Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }

    private void galleryAddPic(){
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(currentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }

}

【问题讨论】:

  • 也许您应该为此使用 MediaCodec 和 MediaRecorder,将相机输出定义为输入表面,而不是向相机发送意图
  • 那会使用 Camera2 API 吗?
  • @blackapps 我刚刚尝试了该解决方案,但大部分代码库已被贬值

标签: java android android-camera


【解决方案1】:

对我来说,最简单的解决方案是在您的应用程序中使用相机视图(您自己的 Camera2 API 包装器或现有的CameraView)并使其不可见或在其上方放置一些视图,如果您想隐藏它。
CameraView API 很简单(CameraView Getting Started):只需将视图添加到布局中,设置 LifecycleOwner,设置拍照回调并在需要拍照时调用 camera.takePicture()

【讨论】:

  • 我开始在我的应用程序中尝试 CameraView。你设置的回调参数是什么?
  • @westerie camera.addCameraListener(new CameraListener() { @Override public void onPictureTaken(PictureResult result) { // A Picture was taken! } @Override public void onVideoTaken(VideoResult result) { // A Video was taken! } // And much more })
  • 你可以看到here的所有例子。回调返回 PictureResult ,您可以从中获取位图并保存。
猜你喜欢
  • 2015-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-09
  • 1970-01-01
  • 2014-01-08
相关资源
最近更新 更多