拍照和图片旋转

//拍照

public void photograph() {

        // 文件夹/DCIM/Camera
            String path = Environment.getExternalStorageDirectory()
                    + "/DCIM/Camera";
            File path1 = new File(path);
            if (!path1.exists()) {
                path1.mkdirs();
            }
            File file = new File(path1, System.currentTimeMillis() + ".jpg");
            avatarName = file.getAbsolutePath();
            Uri uri = Uri.fromFile(file);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivityForResult(intent, 1);

    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK ) {
            String sdState = Environment.getExternalStorageState();
            if (!sdState.equals(Environment.MEDIA_MOUNTED)) {
                Log.e("", "sd card unmount");
                return;
            }
            try {
                //首先取得屏幕对象  
                avatarBitmap = null;
                Display display = this.getWindowManager().getDefaultDisplay();  
                //获取屏幕的宽和高  
                int dw = display.getWidth();  
                int dh = display.getHeight();  
                /**
                 * 为了计算缩放的比例,我们需要获取整个图片的尺寸,而不是图片
                 * BitmapFactory.Options类中有一个布尔型变量inJustDecodeBounds,将其设置为true
                 * 这样,我们获取到的就是图片的尺寸,而不用加载图片了。
                 * 当我们设置这个值的时候,我们接着就可以从BitmapFactory.Options的outWidth和outHeight中获取到值
                 */  
                BitmapFactory.Options op = new BitmapFactory.Options();  
                op.inJustDecodeBounds = true;  
                BitmapFactory.decodeFile(avatarName, op);//调用这个方法以后,op中的outWidth和outHeight就有值了
                int wRatio = (int) Math.ceil(op.outWidth / (float) dw); //计算宽度比例  
                int hRatio = (int) Math.ceil(op.outHeight / (float) dh); //计算高度比例  
                Log.v("Width Ratio:", wRatio + "");  
                Log.v("Height Ratio:", hRatio + "");  
                /**
                 * 接下来,我们就需要判断是否需要缩放以及到底对宽还是高进行缩放。
                 * 如果高和宽不是全都超出了屏幕,那么无需缩放。
                 * 如果高和宽都超出了屏幕大小,则如何选择缩放呢》
                 * 这需要判断wRatio和hRatio的大小
                 * 大的一个将被缩放,因为缩放大的时,小的应该自动进行同比率缩放。
                 * 缩放使用的还是inSampleSize变量
                 */  
                if (wRatio > 1 && hRatio > 1) {  
                    if (wRatio > hRatio) {  
                        op.inSampleSize = wRatio;  
                    } else {  
                        op.inSampleSize = hRatio;  
                    }  
                }  
                op.inJustDecodeBounds = false; //注意这里,一定要设置为false,因为上面我们将其设置为true来获取图片尺寸了  
                avatarBitmap =  BitmapFactory.decodeFile(avatarName, op);
                //图片旋转
                avatarBitmap = ImageUtils.rotaingImageView(ImageUtils.readPictureDegree(avatarName), avatarBitmap);

            } catch (Exception e) {  
                e.printStackTrace();
            }


            if(avatarBitmap != null){
                FileOutputStream fout = null;
                try {
                    fout = new FileOutputStream(avatarName);
                    avatarBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
                    //上传到服务器
                    putAvatar();
                    avatarBitmap.recycle();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        fout.flush();
                        fout.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

/**
     * 读取图片属性:旋转的角度
     *
     * @param path
     *            图片绝对路径
     * @return degree旋转的角度
     */
    public static 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;
    }
    /**
     * 图片旋转
     * @param angle
     * @param bitmap
     * @return
     */
    public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
        // 旋转图片 动作
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        // System.out.println("angle2=" + angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizedBitmap;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章