Bitmap工具类

1,将图片垂直翻转

 /**
     * 垂直翻转
     * @param sourceBitmap
     * @return
     */
    public static Bitmap flipBitmapVertically(Bitmap sourceBitmap) {
        Matrix matrix = new Matrix();
        matrix.postScale(1, -1); // 设置垂直翻转
        return Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true);
    }

2,按比例缩放图片

/**
     * 按比例缩放图片
     *
     * @param origin 原图
     * @param ratio  比例
     * @return 新的bitmap
     */
    public static Bitmap scaleBitmap(Bitmap origin, float ratio) {
        if (origin == null) {
            return null;
        }
        int width = origin.getWidth();
        int height = origin.getHeight();
        Matrix matrix = new Matrix();
        matrix.preScale(ratio, ratio);

        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
        if (newBM.equals(origin)) {
            return newBM;
        }
        origin.recycle();
        return newBM;
    }

3,计算缩放比

 // 计算缩放比
    public static int calculateInSampleSize(BitmapFactory.Options options,
                                            int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

4,调整大小

 /**
     * 调整 Bitmap 的尺寸
     * @param bm
     * @param targetWidth
     * @param targetHeight
     * @return
     */
    public static Bitmap zoomImg(Bitmap bm, int targetWidth, int targetHeight) {
        int srcWidth = bm.getWidth();
        int srcHeight = bm.getHeight();
        float widthScale = targetWidth * 1.0f / srcWidth;
        float heightScale = targetHeight * 1.0f / srcHeight;
        Matrix matrix = new Matrix();
        matrix.postScale(widthScale, heightScale, 0, 0);
        // 如需要可自行设置 Bitmap.Config.RGB_8888 等等
        Bitmap bmpRet = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bmpRet);
        Paint paint = new Paint();
        canvas.drawBitmap(bm, matrix, paint);
        return bmpRet;
    }

5,旋转bitmap

/**
     * 旋转 Bitmap
     *
     * @param bitmap  原始 Bitmap 对象
     * @param degrees 旋转角度
     * @return 旋转后的 Bitmap 对象
     */
    public static Bitmap rotateBitmap(Bitmap bitmap, float degrees) {
        Matrix matrix = new Matrix();
        matrix.postRotate(degrees);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

6,应用颜色滤镜到 Bitmap

/**
     * 应用颜色滤镜到 Bitmap
     *
     * @param bitmap 原始 Bitmap 对象
     * @param filter 颜色滤镜
     * @return 应用滤镜后的 Bitmap 对象
     */
    public static Bitmap applyFilter(Bitmap bitmap, ColorFilter filter) {
        Paint paint = new Paint();
        paint.setColorFilter(filter);
        Bitmap filteredBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        Canvas canvas = new Canvas(filteredBitmap);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        return filteredBitmap;
    }

7,叠加两个 Bitmap

/**
     * 叠加两个 Bitmap
     *
     * @param bitmap1 第一个 Bitmap 对象
     * @param bitmap2 第二个 Bitmap 对象
     * @return 叠加后的 Bitmap 对象
     */
    public static Bitmap overlayBitmaps(Bitmap bitmap1, Bitmap bitmap2) {
        Bitmap mergedBitmap = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), bitmap1.getConfig());
        Canvas canvas = new Canvas(mergedBitmap);
        canvas.drawBitmap(bitmap1, 0, 0, null);
        canvas.drawBitmap(bitmap2, 0, 0, null);
        return mergedBitmap;
    }

8,将 Bitmap 压缩为指定的质量

/**
     * 将 Bitmap 压缩为指定的质量
     *
     * @param bitmap       原始 Bitmap 对象
     * @param desiredQuality 压缩的目标质量,取值范围为 0-100
     * @return 压缩后的 Bitmap 对象
     */
    public static Bitmap compressByQuality(Bitmap bitmap, int desiredQuality) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, desiredQuality, outputStream);
        byte[] byteArray = outputStream.toByteArray();
        return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    }

9,将 Bitmap 压缩到指定的目标大小

/**
     * 将 Bitmap 压缩到指定的目标大小
     *
     * @param bitmap           原始 Bitmap 对象
     * @param desiredMaxSize   压缩的目标大小,单位为字节
     * @return 压缩后的 Bitmap 对象
     */
    public static Bitmap compressBySize(Bitmap bitmap, long desiredMaxSize) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        while (outputStream.toByteArray().length > desiredMaxSize && quality > 0) {
            outputStream.reset();
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            quality -= 5;
        }
        byte[] byteArray = outputStream.toByteArray();
        return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    }

10,将 Bitmap 压缩到指定的目标尺寸

/**
     * 将 Bitmap 压缩到指定的目标尺寸
     *
     * @param bitmap         原始 Bitmap 对象
     * @param desiredWidth   压缩后的目标宽度
     * @param desiredHeight  压缩后的目标高度
     * @return 压缩后的 Bitmap 对象
     */
    public static Bitmap compressByResolution(Bitmap bitmap, int desiredWidth, int desiredHeight) {
        return Bitmap.createScaledBitmap(bitmap, desiredWidth, desiredHeight, true);
    }

你可能感兴趣的:(java,算法,开发语言)