• 拍照并保存到指定文件夹


    关键代码:

    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"/>
  • 相关阅读:
    数据库遇到的2个奇葩的事情
    虚IP解决程序连只读服务器故障漂移
    SQL Server 主库DML操作慢故障处理过程
    优雅地使用pt-archiver进行数据归档(转)
    Mysql表读写、索引等操作的sql语句效率优化问题
    MySQL Performance-Schema(一) 配置篇
    MySQL Performance-Schema(三) 实践篇
    MySQL Performance-Schema(二) 理论篇
    MySQL案例-并行复制乱序提交引起的同步异常
    (转)MySQL- 5.7 sys schema笔记,mysql-schema
  • 原文地址:https://www.cnblogs.com/itfenqing/p/6747207.html
Copyright © 2020-2023  润新知