簡體   English   中英

無需打開相機應用程序即可拍照

[英]Take a picture without opening the Camera application

我目前有一個帶有拍照按鈕的應用程序。 但是,當我單擊該按鈕時,默認的相機應用程序會打開,然后您必須手動拍照。 如何讓相機應用程序不打開並自動拍照並只需按下應用程序中的按鈕即可保存? 我想按下我創建的按鈕,它會拍照並自動保存。

主活動.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);
    }

}

對我來說,最簡單的解決方案是在應用程序中使用相機視圖(您自己的 Camera2 API 包裝器或現有的CameraView )並使其不可見或在其上方放置一些視圖(如果您想隱藏它)。
CameraView API 很簡單( CameraView 入門):只需將視圖添加到布局中,設置 LifecycleOwner,設置拍照回調並在需要拍照時調用camera.takePicture()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM