zoukankan      html  css  js  c++  java
  • 拍照并保存到指定文件夹

    关键代码:

    protected void takePhoto(View v){
            if( v.getId() == R.id.btn ) {
                File dir = new File(Environment.getExternalStorageDirectory(),"Guo");
                if(!dir.exists()){
                    dir.mkdir();
                }
    
                curFile = new File(dir, System.currentTimeMillis() + ".jpg");
                if( !curFile.exists() ){
                    try{
                        curFile.createNewFile();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
    
                }
                Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(curFile));
                startActivityForResult(it, TAKE_PHOTO);
            }
    }

    处理相机返回数据,即图片数据:

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
            switch (requestCode){
                case TAKE_PHOTO:
                    if(resultCode == RESULT_OK){
                        //picture.setImageURI(Uri.fromFile(curFile));
                        int degree = readPictureDegree(curFile.getAbsolutePath());
                        BitmapFactory.Options opts=new BitmapFactory.Options();
                        opts.inSampleSize=2;
                        Bitmap cbitmap = BitmapFactory.decodeFile(curFile.getAbsolutePath(),opts);
                        Bitmap bitmap = rotaingImageView(degree, cbitmap);
               sysImageNotify();
                picture.setImageBitmap(bitmap); 
              }
              break;
        }
    }

    照相机拍照后,有些图片是获取图片的旋转角度

    // 获取旋转角度
    public  int readPictureDegree(String path) {
            int degree  = 0;
            try {
                ExifInterface exifInterface = new ExifInterface(path);
                int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        degree = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        degree = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        degree = 270;
                        break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return degree;
        }

      

    旋转图片:

    /*
        * 旋转图片
        */
        public  Bitmap rotaingImageView(int angle , Bitmap bitmap) {
            //旋转图片 动作
            Matrix matrix = new Matrix();;
            matrix.postRotate(angle);
            // 创建新的图片
            Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(), matrix, true);return resizedBitmap;
        }

    图片保存进入文件夹后,在系统图库中显示

    protected void sysImageNotify(){
    	// 文件插入系统图库
            try {
                MediaStore.Images.Media.insertImage(
                        getContentResolver(),
                        curFile.getAbsolutePath(), curFile.getName(), null);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // 最后通知图库更新
            sendBroadcast(
                    new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                    Uri.parse("file://" + Environment.getExternalStorageDirectory())
                )
            );
    
     }
    

      

     所需权限:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA"/>
  • 相关阅读:
    Windows 08 R2_组策略
    Nginx常用配置实例(4)
    Nginx日常维护操作(3)
    Nginx配置文件(2)
    Nginx概述和安装(1)
    Zabbix实战-简易教程--通过公众平台企业号发送短信
    HDFS ZKFC自动切换原理分析
    HDFS ZKFC自动切换原理分析
    DataNode启动优化改进:磁盘检测并行化
    DataNode启动优化改进:磁盘检测并行化
  • 原文地址:https://www.cnblogs.com/itfenqing/p/6747207.html
Copyright © 2011-2022 走看看