android 调用摄像头

1、在AndroidMainfest.xml中的配置

 
 
 
 
 
 
 

中的配置


    
    

android:name里的值:包名+FileProvider

android:resource:文件还没有创建,所以会先报错

下面创建resource里的xml文件

(1)把视图模式改为"Project"

android 调用摄像头_第1张图片

(2)app -> src -> main -> res,在res下新建一个文件夹xml,在xml文件夹下新建xml文件

(右击 -> new -> XML Source File,文件名与resource里保持一致,例:此处文件名应为“file_path”)



    
    
    

2、在MainActivity中的配置

    ImageView pic;
    TextView text;
    Button gray;
    public static final  String TAG="MainActivity";
    public static final int TAKE_POTHO=1;
    private Uri uri;

    @Override
            public void onClick(View v) {
                //convert2Gray();
                //ocrNormal();
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                File outImage=new File(getExternalCacheDir(),timeStamp+"output_image.jpg");
                try{
                    if(outImage.exists())
                    {
                        outImage.delete();
                    }
                    outImage.createNewFile();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                if(Build.VERSION.SDK_INT>=24)
                {
                    uri= FileProvider.getUriForFile(MainActivity.this,"com.example.demo_tryopencv_2.fileprovider",outImage);
                }
                else
                {
                    uri=Uri.fromFile(outImage);
                }
                Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
                startActivityForResult(intent,TAKE_POTHO);
            }
        });
        /
    }

    /**
     * 拍照
     */

        @Override
        protected void onActivityResult(int requestCode,int resultCode,Intent data)
        {
            super.onActivityResult(requestCode,resultCode,data);
            switch (requestCode)
            {
                case TAKE_POTHO:
                    if(resultCode==RESULT_OK)
                    {
                        try{
                            Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
                            pic.setImageBitmap(bitmap);
                        }catch (FileNotFoundException e)
                        {
                            e.printStackTrace();
                        }
                    }
                    break;
                default:
                    break;
            }
        }

 

 

 

 

你可能感兴趣的:(android 调用摄像头)