Skip to content

Commit

Permalink
optimize & fixed bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanboy committed Jun 23, 2017
1 parent a0dcbfc commit f522730
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 82 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
buildToolsVersion '25.0.0'

defaultConfig {
applicationId "com.jeanboy.demo.compress"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.android.tools.build:gradle:2.3.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# org.gradle.parallel=true
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Fri Jun 23 14:44:05 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
2 changes: 1 addition & 1 deletion lib-bither-compress/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
buildToolsVersion '25.0.0'

defaultConfig {
minSdkVersion 15
Expand Down
121 changes: 45 additions & 76 deletions lib-bither-compress/src/main/java/net/bither/util/NativeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,115 +4,84 @@
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;

import java.io.ByteArrayOutputStream;

import static android.media.CamcorderProfile.QUALITY_720P;

public class NativeUtil {
private static int DEFAULT_QUALITY = 95;

/**
* @param bit bitmap对象
* @param fileName 指定保存目录名
* @param optimize 是否采用哈弗曼表数据计算 品质相差5-10倍
* @Description: JNI基本压缩
*/
public static void compressBitmap(Bitmap bit, String fileName, boolean optimize) {
saveBitmap(bit, DEFAULT_QUALITY, fileName, optimize);
public final static int QUALITY_320P = 320;//480, 320
public final static int QUALITY_360P = 360;//640, 360
public final static int QUALITY_480P = 480;//640, 480
public final static int QUALITY_720P = 720;//1280, 720
public final static int QUALITY_1080P = 1080;//1920, 1080
public final static int QUALITY_2K = 1440;//2560, 1440
public final static int QUALITY_4K = 2160;//3840, 2160

public final static int QUALITY_DEFAULT = QUALITY_720P;
public final static int SIZE_1KB = 1024;
public final static int size_1MB = SIZE_1KB * 1024;


public static void compressBitmap(Bitmap bitmap, String filePath) {
compressBitmap(bitmap, filePath, size_1MB, QUALITY_DEFAULT);
}

/**
* @param image bitmap对象
* @param filePath 要保存的指定目录
* @Description: 通过JNI图片压缩把Bitmap保存到指定目录
*/
public static void compressBitmap(Bitmap image, String filePath) {
// 最大图片大小 1000KB
int maxSize = 1000;
// 获取尺寸压缩倍数
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);
public static void compressBitmap(Bitmap bitmap, String filePath, int maxByte, int quality) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
float ratio = getRatioSize(w, h, quality);

int resultW = Math.round(w / ratio);
int resultH = Math.round(h / ratio);

Bitmap result = Bitmap.createBitmap(resultW, resultH, Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Rect rect = new Rect(0, 0, resultW, resultH);
canvas.drawBitmap(bitmap, null, rect, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
int options = 80;//100不压缩品质
result.compress(Bitmap.CompressFormat.JPEG, options, baos);
// 循环判断如果压缩后图片是否大于最大值,大于继续压缩
while (baos.toByteArray().length / 1024 > maxSize) {
// 重置baos即清空baos
while (baos.toByteArray().length > maxByte) {
baos.reset();
// 每次都减少10
options -= 10;
// 这里压缩options%,把压缩后的数据存放到baos中
result.compress(Bitmap.CompressFormat.JPEG, options, baos);
options -= 10;
}
// JNI调用保存图片到SD卡 这个关键
NativeUtil.saveBitmap(result, options, filePath, true);
// 释放Bitmap
if (result != null && !result.isRecycled()) {
if (!result.isRecycled()) {
result.recycle();
result = null;
}
}


/**
* 计算缩放比
*
* @param bitWidth 当前图片宽度
* @param bitHeight 当前图片高度
* @return
* @Description:函数描述
* 计算缩放比例
*/
public static int getRatioSize(int bitWidth, int bitHeight) {
// 图片最大分辨率
int imageHeight = 1920;
int imageWidth = 1080;
// 缩放比
int ratio = 1;
// 缩放比,由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
if (bitWidth > bitHeight && bitWidth > imageHeight) {
// 如果图片宽度比高度大,以宽度为基准
ratio = bitWidth / imageHeight;
} else if (bitWidth < bitHeight && bitHeight > imageHeight) {
// 如果图片高度比宽度大,以高度为基准
ratio = bitHeight / imageHeight;
private static float getRatioSize(int w, int h, int qualityH) {
float ratio;
if (w > h) {
ratio = h * 100.00f / qualityH / 100f;
} else {
ratio = w * 100.00f / qualityH / 100f;
}
// 最小比率为1
if (ratio <= 0)
ratio = 1;
if (ratio <= 0) ratio = 1;
return ratio;
}

/**
* 调用native方法
*
* @param bit
* @param quality
* @param fileName
* @param optimize
* @Description:函数描述
*/
private static void saveBitmap(Bitmap bit, int quality, String fileName, boolean optimize) {
compressBitmap(bit, bit.getWidth(), bit.getHeight(), quality, fileName.getBytes(), optimize);
private static void saveBitmap(Bitmap bitmap, int quality, String fileName, boolean optimize) {
compressBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), quality, fileName.getBytes(), optimize);
}

/**
* 调用底层 bitherlibjni.c中的方法
*
* @param bit
* @param w
* @param h
* @param quality
* @param fileNameBytes
* @param optimize
* @return
* @Description:函数描述
*/
private static native String compressBitmap(Bitmap bit, int w, int h, int quality, byte[] fileNameBytes,
boolean optimize);
private static native String compressBitmap(Bitmap bit, int w, int h, int quality, byte[] fileNameBytes, boolean optimize);

/**
* 加载lib下两个so文件
Expand Down

0 comments on commit f522730

Please sign in to comment.