美文网首页
1、安卓调用摄像头拍照

1、安卓调用摄像头拍照

作者: zenos876 | 来源:发表于2019-03-13 00:13 被阅读0次

    近期在做毕设,需要用到Android调用摄像头拍摄车牌号,并对车牌号码进行识别。特别记录一下如何调用系统摄像头进行拍照,并将文件保存到本地。

    环境:Android Studio
    官方指南:Camera|Android Developer

    1、首先当然要添加一下调用摄像头需要的权限,以及创建文件需要的权限等

    AndroidMainfest.xml

        </application>
        <uses-feature android:name="android.hardware.camera"
            android:required="true" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
            android:maxSdkVersion="18" />
    

    2、添加函数,创建临时文件,用来储存图片
    MainActivity.java

        String currentPhotoPath;
        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();
            System.out.println(currentPhotoPath);
            return image;
        }
    

    3、创建函数用来启动摄像头,并储存文件
    MainActivity.java

        static final int REQUEST_TAKE_PHOTO = 1;
    
        private void dispatchTakePictureIntent() {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // Ensure that there's 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 ex) {
                    // Error occurred while creating the File
                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    Uri photoURI = FileProvider.getUriForFile(this,
                            "com.example.takephotos.fileprovider",
                            photoFile);
    
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                    startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                }
            }
        }
    

    4、因为上面使用到privider,因此需要在res下创建xml文件夹、创建文件file_paths.xml,并添加对应的设置
    file_paths.xml

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
            <external-files-path name="my_images" path="." />
    </paths>
    

    AndroidMainfest.xml

     <application
            <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="com.example.takephotos.fileprovider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/file_paths"></meta-data>
            </provider>
    </application>
    

    5、在onCreate()中调用dispatchTakePictureIntent()函数
    MainActivity.java

            @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mImageView = (ImageView) findViewById(R.id.image);
            dispatchTakePictureIntent();
        }
    

    其保存的文件目录如下:
    /storage/emulated/0/Android/data/com.example.takephotos/files/Pictures/JPEG_20190312_2339464476956325377028564.jpg

    安卓拍照并且保存本地就到这里完成了。

    相关文章

      网友评论

          本文标题:1、安卓调用摄像头拍照

          本文链接:https://www.haomeiwen.com/subject/htofpqtx.html