美文网首页
图片压缩

图片压缩

作者: 编程的猫 | 来源:发表于2020-06-02 17:31 被阅读0次

图片压缩质量压缩,比例压缩,采样率压缩,JPEG压缩
思路:根据控件的尺寸或图片要放大显示的尺寸作为参数去压缩图片
原图为6M,以下是各种压缩的效果


image.png

以下贴出代码:

质量压缩
   /**
     * 质量压缩
     *
     * @param bitmap  要压缩的bitmap
     * @param quality 要压缩的质量系数,范围0-100,越小压缩率越高
     */
    public Bitmap getCompressBitmapByQuality(Bitmap bitmap, int quality) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
        return BitmapFactory.decodeByteArray(bos.toByteArray(), 0, bos.toByteArray().length);
    }
比例压缩
    /**
     * 比例压缩
     */
    private Bitmap getCompressBitmapByScale(Bitmap bitmap, int maxW, int maxH) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();

        float sx = (float) maxW / (float) w;
        float sy = (float) maxH / (float) h;

        Matrix matrix = new Matrix();
        matrix.setScale(sx, sy);

        return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
    }
采样率压缩
/**
     * 采样率压缩
     */
    private Bitmap getCompressBitmapBySampleSize(Bitmap bitmap, int sampleSize) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = sampleSize;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        return BitmapFactory.decodeByteArray(bos.toByteArray(), 0,
                bos.toByteArray().length, options);
    }
JPEG压缩
/**
     * 使用so来压缩图片
     */
    public void compressImage(View view) {
        // Android Q中会将文件存储到该应用的沙盒文件路径下,不需要申请文件读写的权限
        File picturePath = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        String path = picturePath + File.separator + "compressImg";

        Log.i("TAG", "  路径:" + path);
        //沙盒下创建文件夹
        File file = new File(path);
        if (file.exists() || file.mkdirs()) {
            //BitmapFactory.Options options = new BitmapFactory.Options();
            //Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tm2);
            //options.inJustDecodeBounds = false;
            //Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tm2, options);
            Bitmap bitmap = readBitmap();
            ImageCompress compress = new ImageCompress();
            long start = System.currentTimeMillis();
            String imgPath = path + File.separator + "compressSo.jpg";
            int i = compress.compressBitmap(bitmap, 30, imgPath);
            if (i == 1) {
                long l = System.currentTimeMillis() - start;
                Log.i("TAG", "  压缩并存储成功  耗时:" + l);
            } else {
                Log.i("TAG", "  压缩并存储失败");
            }
        }
    }

package com.example.image;

import android.graphics.Bitmap;

import java.nio.ByteBuffer;

public class ImageCompress {

    static {
        System.loadLibrary("imageCompress");
    }

    /**
     * NDK方法加载图片
     *
     * @param bitmap   图片bitmap
     * @param quality  压缩的质量
     * @param fileName 压缩后的路径
     * @return
     */
    public native int compressBitmap(Bitmap bitmap, int quality,
                                            String fileName);

    public native ByteBuffer getImage(String fileName);

    public native ByteBuffer getImage2(Bitmap bitmap, int quality);

    public native void release(ByteBuffer byteBuffer);
}

项目的地址:

相关文章

  • 图片压缩组件

    图片压缩 图片压缩

  • iOS 图片压缩方法

    两种图片压缩方法 两种图片压缩方法:压缩图片质量(Quality),压缩图片尺寸(Size)。 压缩图片质量 通过...

  • iOS 图片压缩限制大小最优解

    iOS 图片压缩限制大小最优解 图片的两种压缩方法 1.1 压缩图片质量 1.2 压缩图片尺寸 压缩图片使图片文件...

  • iOS 图片压缩限制大小最优解

    概要: 图片的两种压缩方法1.1 压缩图片质量1.2 压缩图片尺寸压缩图片使图片文件小于指定大小2.1 压缩图片质...

  • iOS 图片压缩限制大小

    一、两种图片压缩方法 两种压缩图片的方法:压缩图片质量(Quality),压缩图片尺寸(Size)。 压缩图片质量...

  • iOS 图片压缩方法

    两种图片压缩方法 两种压缩图片的方法:压缩图片质量(Quality),压缩图片尺寸(Size)。 压缩图片质量 N...

  • 图片压缩方法

    两种图片压缩方法 压缩图片质量(quality)、压缩图片尺寸(size) 压缩图片质量 或 前者可以控制压缩比例...

  • 图片懒加载之高斯模糊

    压缩原始图片 将原始图片压缩至1~2kb甚至更小的图片nature.jpg 压缩 java 图片压缩natur...

  • iOS 图片压缩方法

    图片压缩可以通过两种方式实现,压缩图片质量和压缩图片尺寸。如果对图片清晰度有要求的,可以先压缩图片质量,在压缩图片...

  • iOS 图片压缩方法

    iOS 图片压缩方法 更多图片处理方法见图片组件 BBWebImage iOS 图片压缩方法 两种图片压缩方法 两...

网友评论

      本文标题:图片压缩

      本文链接:https://www.haomeiwen.com/subject/sdtpzhtx.html