拍照 相册 (显示) 图片拍照旋转 工具类

public class ImageUtils {

    public static final int GET_IMAGE_BY_CAMERA = 5001;
    public static final int SYS_INTENT_REQUEST = 5002;
    public static String path;

    /**
     * 调用相机拍照
     */
    @SuppressLint("SimpleDateFormat")
    public static void cameraPhoto(Activity context, String fileName) {
        String sdStatus = Environment.getExternalStorageState();
        /* 检测sd是否可用 */
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
            Toast.makeText(context, "SD卡不可用!", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            File file = createFile(context, fileName);
            // 指定拍照
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // 加载路径
            // 指定存储路径,这样就可以保存原图了
            // imageUriFromCamera =;
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            // 拍照返回图片
            context.startActivityForResult(intent, GET_IMAGE_BY_CAMERA);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 在指定文件夾下面創建文件用来保存文件
     * 
     * @param fileName
     * @return
     * @throws IOException
     */
    private static File createFile(Activity context, String fileName)
            throws IOException {
        // 获取当前程序路径
        path = Environment.getExternalStorageDirectory().getPath()
                + "/zcImage/";
        File fileDir = new File(path);
        /** 检测文件夹是否存在,不存在则创建文件夹 **/
        if (!fileDir.exists() && !fileDir.isDirectory())
            fileDir.mkdirs();
        File file = new File(fileDir, fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }

    /**
     * @param data
     *            拍照后获取照片
     */
    public static void cameraCamera(Activity context, ImageView imageView,
            Intent data, String fileName) {
        Bundle bundle = data.getExtras();
        Bitmap bitmap = (Bitmap) bundle.get("data");
        showImgs(context, bitmap, imageView, fileName);
    }

    /**
     * 展示选择的图片
     * 
     * @param bitmap
     * @param isSysUp
     */
    public static void showImgs(final Activity context, Bitmap bitmap,
            ImageView imageView, final String fileName) {
        Bitmap _bitmap = compressionBigBitmap(bitmap);
        _bitmap = compressImage(_bitmap);
        int degree = readPictureDegree(fileName);
        if (degree <= 0) {
            imageView.setImageBitmap(_bitmap);
        } else {
            // 创建操作图片是用的matrix对象
            Matrix matrix = new Matrix();
            // 旋转图片动作
            matrix.postRotate(degree);
            // 创建新图片
            _bitmap = Bitmap.createBitmap(_bitmap, 0, 0, _bitmap.getWidth(),
                    _bitmap.getHeight(), matrix, true);
            imageView.setImageBitmap(_bitmap);
        }
    }

    /**
     * @param bitmap
     * @return 压缩后的bitmap
     */
    private static Bitmap compressionBigBitmap(Bitmap bitmap) {
        Bitmap destBitmap = null;
        /* 图片宽度调整为100,大于这个比例的,按一定比例缩放到宽度为100 */
        if (bitmap.getWidth() > 300) {
            float scaleValue = (float) (300f / bitmap.getWidth());
            Matrix matrix = new Matrix();
            matrix.postScale(scaleValue, scaleValue);
            destBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), matrix, true);
        } else {
            return bitmap;
        }
        return destBitmap;
    }

    /**
     * 将bitmap图片压缩
     * 
     * @param image
     * @return
     */
    private static Bitmap compressImage(Bitmap image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
        int options = 100;
        while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
            baos.reset();// 重置baos即清空baos
            options -= 10;// 每次都减少10
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
        return bitmap;
    }

    private static int readPictureDegree(String fileName) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(fileName);
            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 static void systemPhoto(Activity context) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        context.startActivityForResult(intent, SYS_INTENT_REQUEST);

    }

    /**
     * 根据图片的URi获取图片的路径
     * 
     * @param uri
     * @param activity
     * @return
     */
    public static String uriToPath(Uri uri,Activity context) {
        String[] proj = { MediaStore.Images.Media.DATA };
        ContentResolver cr = context.getContentResolver();
        Cursor cursor = cr.query(uri, proj,null, null, null);
        int actual_image_column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String imageFilePath = cursor.getString(actual_image_column_index);
        cursor.close();
        return imageFilePath;
    }



    /**
     * 将bitmap对象转化成base64
     * 
     * @param bitmap
     * @return
     */
    public static String getImgString(Bitmap bitmap) {
        ByteArrayOutputStream bos = null;
        try {
            bos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 10, bos);
            bos.flush();
            bos.close();
            byte[] imgBytes = bos.toByteArray();
            return Base64.encodeToString(imgBytes, Base64.DEFAULT);
        } catch (Exception e) {
            return null;
        } finally {
            try {
                bos.flush();
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 旋转图片 解决三星手机拍照 图片旋转的问题
     * 
     * @param angle
     * @param bitmap
     * @return Bitmap
     */
    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;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值