Skip to content

Commit

Permalink
add baseline support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunzxyong committed Feb 7, 2019
1 parent 5a30e49 commit 16c8b72
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion tiny/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ publish {
uploadName = 'Tiny'
groupId = 'com.zxy.android'
artifactId = 'tiny'
publishVersion = '1.0.0'
publishVersion = '1.1.0'
repoName = 'maven'
dryRun = 'false'
desc = 'an image compression framework.'
Expand Down
7 changes: 7 additions & 0 deletions tiny/src/main/java/com/zxy/tiny/Tiny.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,24 @@ public static class BitmapCompressOptions {
*/
public Bitmap.Config config = CompressKit.DEFAULT_CONFIG;

/**
* By default, using 1280 px. suggest value for (1280|2560).
*/
public int baseline;

/**
* The width of the bitmap.
* <p>
* If the value is zero,the default compression maximum width is the screen width or {@link CompressKit#DEFAULT_MAX_COMPRESS_SIZE}.
* see {@link Tiny.BitmapCompressOptions#baseline}.
*/
public int width;

/**
* The height of the bitmap.
* <p>
* If the value is zero,the default compression maximum height is the screen height or {@link CompressKit#DEFAULT_MAX_COMPRESS_SIZE}.
* see {@link Tiny.BitmapCompressOptions#baseline}.
*/
public int height;
}
Expand Down
30 changes: 15 additions & 15 deletions tiny/src/main/java/com/zxy/tiny/core/BitmapCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ public static Bitmap compress(Bitmap bitmap, Tiny.BitmapCompressOptions options,

int baseline;
if (isViewMode) {
if (screenPair.second >= CompressKit.DEFAULT_MAX_COMPRESS_SIZE ||
screenPair.first >= CompressKit.DEFAULT_MAX_COMPRESS_SIZE) {
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : CompressKit.DEFAULT_MAX_COMPRESS_SIZE;
if (screenPair.second >= CompressKit.getBaseline(options.baseline) ||
screenPair.first >= CompressKit.getBaseline(options.baseline)) {
baseline = hasCustomSize ? Math.min(CompressKit.getBaseline(options.baseline), Math.max(compressWidth, compressHeight)) : CompressKit.getBaseline(options.baseline);
result = matrixCompress(bitmap, baseline, false);
} else {
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : screenPair.second;
baseline = hasCustomSize ? Math.min(CompressKit.getBaseline(options.baseline), Math.max(compressWidth, compressHeight)) : screenPair.second;
result = matrixCompress(bitmap, baseline, false);
}
} else {
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : CompressKit.DEFAULT_MAX_COMPRESS_SIZE;
baseline = hasCustomSize ? Math.min(CompressKit.getBaseline(options.baseline), Math.max(compressWidth, compressHeight)) : CompressKit.getBaseline(options.baseline);
result = matrixCompress(bitmap, baseline, false);
}

Expand Down Expand Up @@ -90,18 +90,18 @@ public static Bitmap compress(byte[] bytes, Tiny.BitmapCompressOptions options,

int baseline;
if (isViewMode) {
if (screenPair.second >= CompressKit.DEFAULT_MAX_COMPRESS_SIZE ||
screenPair.first >= CompressKit.DEFAULT_MAX_COMPRESS_SIZE) {
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : CompressKit.DEFAULT_MAX_COMPRESS_SIZE;
if (screenPair.second >= CompressKit.getBaseline(options.baseline) ||
screenPair.first >= CompressKit.getBaseline(options.baseline)) {
baseline = hasCustomSize ? Math.min(CompressKit.getBaseline(options.baseline), Math.max(compressWidth, compressHeight)) : CompressKit.getBaseline(options.baseline);
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, baseline);
result = matrixCompress(sampleCompress(bytes, sampleSize, options), baseline, true);
} else {
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : screenPair.second;
baseline = hasCustomSize ? Math.min(CompressKit.getBaseline(options.baseline), Math.max(compressWidth, compressHeight)) : screenPair.second;
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, baseline);
result = matrixCompress(sampleCompress(bytes, sampleSize, options), baseline, true);
}
} else {
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : CompressKit.DEFAULT_MAX_COMPRESS_SIZE;
baseline = hasCustomSize ? Math.min(CompressKit.getBaseline(options.baseline), Math.max(compressWidth, compressHeight)) : CompressKit.getBaseline(options.baseline);
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, baseline);
result = matrixCompress(sampleCompress(bytes, sampleSize, options), baseline, true);
}
Expand Down Expand Up @@ -141,18 +141,18 @@ public static Bitmap compress(int resId, Tiny.BitmapCompressOptions options, boo

int baseline;
if (isViewMode) {
if (screenPair.second >= CompressKit.DEFAULT_MAX_COMPRESS_SIZE ||
screenPair.first >= CompressKit.DEFAULT_MAX_COMPRESS_SIZE) {
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : CompressKit.DEFAULT_MAX_COMPRESS_SIZE;
if (screenPair.second >= CompressKit.getBaseline(options.baseline) ||
screenPair.first >= CompressKit.getBaseline(options.baseline)) {
baseline = hasCustomSize ? Math.min(CompressKit.getBaseline(options.baseline), Math.max(compressWidth, compressHeight)) : CompressKit.getBaseline(options.baseline);
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, baseline);
result = matrixCompress(sampleCompress(resId, sampleSize, options), baseline, true);
} else {
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : screenPair.second;
baseline = hasCustomSize ? Math.min(CompressKit.getBaseline(options.baseline), Math.max(compressWidth, compressHeight)) : screenPair.second;
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, baseline);
result = matrixCompress(sampleCompress(resId, sampleSize, options), baseline, true);
}
} else {
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : CompressKit.DEFAULT_MAX_COMPRESS_SIZE;
baseline = hasCustomSize ? Math.min(CompressKit.getBaseline(options.baseline), Math.max(compressWidth, compressHeight)) : CompressKit.getBaseline(options.baseline);
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, baseline);
result = matrixCompress(sampleCompress(resId, sampleSize, options), baseline, true);
}
Expand Down
4 changes: 4 additions & 0 deletions tiny/src/main/java/com/zxy/tiny/core/CompressKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public final class CompressKit {

private static final int DEFAULT_DECODE_BUFFER_SIZE = 16 * 1024;

public static int getBaseline(int baseline) {
return baseline <= 0 ? DEFAULT_MAX_COMPRESS_SIZE : baseline;
}

public static BitmapFactory.Options getDefaultDecodeBoundsOptions() {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Expand Down

0 comments on commit 16c8b72

Please sign in to comment.