简体   繁体   English

无需打开相机应用程序即可拍照

[英]Take a picture without opening the Camera application

I currently have an application with a button that takes a photo.我目前有一个带有拍照按钮的应用程序。 However, when I click the button the default camera application opens, then you manually have to take a photo.但是,当我单击该按钮时,默认的相机应用程序会打开,然后您必须手动拍照。 How do I get the camera application to not open and just take a picture automatically and save it just by pressing the button in my application?如何让相机应用程序不打开并自动拍照并只需按下应用程序中的按钮即可保存? I would like to press the button that I created and it takes a photo and saves it automatically.我想按下我创建的按钮,它会拍照并自动保存。

MainActivity.java主活动.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);
    }

}

For me the easiest solution is using camera view (your own wrapper of Camera2 API or existing CameraView ) inside your application and making it invisible or placing some view above it, if you want to hide it.对我来说,最简单的解决方案是在应用程序中使用相机视图(您自己的 Camera2 API 包装器或现有的CameraView )并使其不可见或在其上方放置一些视图(如果您想隐藏它)。
CameraView API is simple ( CameraView Getting Started ): just add view into layout, set LifecycleOwner, set callback on taking picture and call camera.takePicture() , when you need to take picture. CameraView API 很简单( CameraView 入门):只需将视图添加到布局中,设置 LifecycleOwner,设置拍照回调并在需要拍照时调用camera.takePicture()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM