其他分享
首页 > 其他分享> > 图片压缩-陈浩然,移动网页开发

图片压缩-陈浩然,移动网页开发

作者:互联网

BYTE r, g, b;

data = NULL;

data = malloc(w * h * 3);

tmpdata = data;

int j = 0, i = 0;

int color;

for (i = 0; i < h; i++) {

for (j = 0; j < w; j++) {

color = *((int *) pixelscolor);

r = ((color & 0x00FF0000) >> 16);

g = ((color & 0x0000FF00) >> 8);

b = color & 0x000000FF;

*data = b;

*(data + 1) = g;

*(data + 2) = r;

data = data + 3;

pixelscolor += 4;

}

}

AndroidBitmap_unlockPixels(env, bitmapcolor);

int resultCode= generateJPEG(tmpdata, w, h, quality, fileName, optimize);

free(tmpdata);

if(resultCode==0){

jstring result=(*env)->NewStringUTF(env, error);

error=NULL;

return result;

}

return (*env)->NewStringUTF(env, “1”); //success

}

compressBitmap()为native关联方法,saveBitmap() 压缩调用方法

private static native String compressBitmap(Bitmap bit, int w, int h, int quality, byte[] fileNameBytes,

boolean optimize);

private static void saveBitmap(Bitmap bmp, int quality, String fileName, boolean optimize) {

compressBitmap(bit, bit.getWidth(), bit.getHeight(), quality, fileName.getBytes(), optimize);

}

4. 结合三种方式的终极压缩

首先通过尺寸压缩,压缩到手机常用的一个分辨率(1280*960 微信好像是压缩到这个分辨率),然后我们要把图片压缩到100KB以内,通过质量压缩来计算options需要设置为多少,最后调用JNI压缩,这边我测试了下,压缩出来的清晰度和原图几乎差不多,压缩时间大概1秒钟左右

public static int getRatioSize(int bitWidth, int bitHeight) {

// 图片最大分辨率

int imageHeight = 1280;

int imageWidth = 960;

// 缩放比

int ratio = 1;

// 缩放比,由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

if (bitWidth > bitHeight && bitWidth > imageWidth) {

// 如果图片宽度比高度大,以宽度为基准

ratio = bitWidth / imageWidth;

} else if (bitWidth < bitHeight && bitHeight > imageHeight) {

// 如果图片高度比宽度大,以高度为基准

ratio = bitHeight / imageHeight;

}

// 最小比率为1

if (ratio <= 0)

ratio = 1;

return ratio;

}

public static void compressBitmap(Bitmap image, String filePath) {

// 最大图片大小 100KB

int maxSize = 100;

// 获取尺寸压缩倍数

int ratio = NativeUtil.getRatioSize(image.getWidth(), image.getHeight());

// 压缩Bitmap到对应尺寸

Bitmap result = Bitmap.createBitmap(image.getWidth() / ratio, image.getHeight() / ratio, Config.ARGB_8888);

Canvas canvas = new Canvas(result);

Rect rect = new Rect(0, 0, image.getWidth() / ratio, image.getHeight() / ratio);

canvas.drawBitmap(image, null, rect, null);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中

int options = 100;

result.compress(Bitmap.CompressFormat.JPEG, options, baos);

// 循环判断如果压缩后图片是否大于100kb,大于继续压缩

while (baos.toByteArray().length / 1024 > maxSize) {

// 重置baos即清空baos

baos.reset();

// 每次都减少10

options -= 10;

// 这里压缩options%,把压缩后的数据存放到baos中

result.compress(Bitmap.CompressFormat.JPEG, options, baos);

}

// JNI调用保存图片到SD卡 这个关键

NativeUtil.saveBitmap(result, options, filePath, true);

// 释放Bitmap

if (result != null && !result.isRecycled()) {

result.recycle();

result = null;

}

}

五. NativeUtil类的源码

16.9.29更新

1、添加getBitmapFromFile()方法,解决OOM和图片旋转的问题

2、添加compressBitmap()方法,传递当前图片本地路径和解压后图片保存路径两个参数,即可,实现压缩

package net.bither.util;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import android.graphics.Bitmap;

import android.graphics.Bitmap.Config;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Matrix;

import android.graphics.Rect;

import android.media.ExifInterface;

/**

*/

public class NativeUtil {

private static int DEFAULT_QUALITY = 95;

/**

*/

public static void compressBitmap(Bitmap bit, String fileName, boolean optimize) {

saveBitmap(bit, DEFAULT_QUALITY, fileName, optimize);

}

/**

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

目录

*/

public static void compressBitmap(Bitmap image, String filePath) {

// 最大图片大小 150KB

int maxSize = 150;

// 获取尺寸压缩倍数

int ratio = NativeUtil.getRatioSize(image.getWidth(),image.getHeight());

// 压缩Bitmap到对应尺寸

Bitmap result = Bitmap.createBitmap(image.getWidth() / ratio,image.getHeight() / ratio,Config.ARGB_8888);

Canvas canvas = new Canvas(result);

Rect rect = new Rect(0, 0, image.getWidth() / ratio, image.getHeight() / ratio);

canvas.drawBitmap(image,null,rect,null);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中

int options = 100;

result.compress(Bitmap.CompressFormat.JPEG, options, baos);

// 循环判断如果压缩后图片是否大于100kb,大于继续压缩

while (baos.toByteArray().length / 1024 > maxSize) {

// 重置baos即清空baos

baos.reset();

// 每次都减少10

options -= 10;

// 这里压缩options%,把压缩后的数据存放到baos中

result.compress(Bitmap.CompressFormat.JPEG, options, baos);

}

// JNI保存图片到SD卡 这个关键

NativeUtil.saveBitmap(result, options, filePath, true);

// 释放Bitmap

if (!result.isRecycled()) {

result.recycle();

}

}

/**

*/

public static void compressBitmap(String curFilePath, String targetFilePath) {

// 最大图片大小 150KB

int maxSize = 150;

//根据地址获取bitmap

Bitmap result = getBitmapFromFile(curFilePath);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中

int quality = 100;

result.compress(Bitmap.CompressFormat.JPEG, quality, baos);

// 循环判断如果压缩后图片是否大于100kb,大于继续压缩

while (baos.toByteArray().length / 1024 > maxSize) {

// 重置baos即清空baos

baos.reset();

// 每次都减少10

quality -= 10;

// 这里压缩quality,把压缩后的数据存放到baos中

result.compress(Bitmap.CompressFormat.JPEG, quality, baos);

}

// JNI保存图片到SD卡 这个关键

NativeUtil.saveBitmap(result, quality, targetFilePath, true);

// 释放Bitmap

if (!result.isRecycled()) {

result.recycle();

}

}

/**

*/

public static int getRatioSize(int bitWidth, int bitHeight) {

// 图片最大分辨率

int imageHeight = 1280;

int imageWidth = 960;

// 缩放比

int ratio = 1;

// 缩放比,由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

if (bitWidth > bitHeight && bitWidth > imageWidth) {

// 如果图片宽度比高度大,以宽度为基准

ratio = bitWidth / imageWidth;

} else if (bitWidth < bitHeight && bitHeight > imageHeight) {

// 如果图片高度比宽度大,以高度为基准

ratio = bitHeight / imageHeight;

}

// 最小比率为1

if (ratio <= 0)

ratio = 1;

return ratio;

}

/**

*/

public static Bitmap getBitmapFromFile(String filePath){

BitmapFactory.Options newOpts = new BitmapFactory.Options();

newOpts.inJustDecodeBounds = true;//只读边,不读内容

BitmapFactory.decodeFile(filePath, newOpts);

int w = newOpts.outWidth;

int h = newOpts.outHeight;

// 获取尺寸压缩倍数

newOpts.inSampleSize = NativeUtil.getRatioSize(w,h);

newOpts.inJustDecodeBounds = false;//读取所有内容

newOpts.inDither = false;

newOpts.inPurgeable=true;

newOpts.inInputShareable=true;

newOpts.inTempStorage = new byte[32 * 1024];

Bitmap bitmap = null;

File file = new File(filePath);

FileInputStream fs = null;

try {

fs = new FileInputStream(file);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

try {

if(fs!=null){

bitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(),null,newOpts);

//旋转图片

int photoDegree = readPictureDegree(filePath);

if(photoDegree != 0){

Matrix matrix = new Matrix();

matrix.postRotate(photoDegree);

// 创建新的图片

bitmap = Bitmap.createBitmap(bitmap, 0, 0,

bitmap.getWidth(), bitmap.getHeight(), matrix, true);

}

}

} catch (IOException e) {

e.printStackTrace();

} finally{

if(fs!=null) {

try {

fs.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return bitmap;

}

/**

*/

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;

}

/**

*/

private static void saveBitmap(Bitmap bit, int quality, String fileName, boolean optimize) {

compressBitmap(bit, bit.getWidth(), bit.getHeight(), quality, fileName.getBytes(), optimize);

}

/**

*/

private static native String compressBitmap(Bitmap bit, int w, int h, int quality, byte[] fileNameBytes,

boolean optimize);

/**

*/

标签:网页,int,压缩,Bitmap,result,baos,浩然,ratio
来源: https://blog.csdn.net/m0_65512329/article/details/122030881