Last active
August 9, 2016 14:16
-
-
Save panthole/253131f1b7afa66556ba78a44e5df3e5 to your computer and use it in GitHub Desktop.
Android图片压缩保存
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Bitmap; | |
import android.graphics.Bitmap.CompressFormat; | |
import android.graphics.Bitmap.Config; | |
import android.graphics.BitmapFactory; | |
import com.iqiyi.qixiu.utils.Log; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
public class JPEGHelper { | |
private static final String TAG = "JPEGHelper"; | |
static { | |
System.loadLibrary("cjpeg"); | |
} | |
public JPEGHelper() { | |
} | |
public static Bitmap compressBySize(String srcPath, String dstPath, String imageName, | |
int targetWidth, int targetHeight) { | |
BitmapFactory.Options opts = new BitmapFactory.Options(); | |
opts.inJustDecodeBounds = true;// 不去真的解析图片,只是获取图片的头部信息,包含宽高等; | |
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, opts); | |
// 得到图片的宽度、高度; | |
int imgWidth = opts.outWidth; | |
int imgHeight = opts.outHeight; | |
// 分别计算图片宽度、高度与目标宽度、高度的比例;取大于等于该比例的最小整数; | |
int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth); | |
int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight); | |
if (widthRatio > 1 || widthRatio > 1) { | |
if (widthRatio > heightRatio) { | |
opts.inSampleSize = widthRatio; | |
} else { | |
opts.inSampleSize = heightRatio; | |
} | |
} | |
// 设置好缩放比例后,加载图片进内容; | |
opts.inJustDecodeBounds = false; | |
bitmap = BitmapFactory.decodeFile(srcPath, opts); | |
if (!"".equals(dstPath)) { | |
try { | |
File f = new File(dstPath, imageName); | |
f.createNewFile(); | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos); | |
byte[] bitmapdata = bos.toByteArray(); | |
FileOutputStream fos = new FileOutputStream(f); | |
fos.write(bitmapdata); | |
fos.flush(); | |
fos.close(); | |
} catch (Exception e) { | |
} | |
} | |
return bitmap; | |
} | |
public native int compress(String srcPath, String dstPath, int w, int h); | |
public int compress(String srcPath, String dstPath, String imageName,int w, int h) { | |
Log.d(TAG, "compress srcPath = " + srcPath); | |
Log.d(TAG, "compress dstPath = " + dstPath); | |
BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = true; | |
BitmapFactory.decodeFile(srcPath, options); | |
Log.d(TAG, "compress source width = " | |
+ options.outWidth | |
+ " height = " | |
+ options.outHeight | |
+ " mineType = " | |
+ options.outMimeType); | |
float scalex, scaley; | |
scalex = (float) options.outWidth / w; | |
scaley = (float) options.outHeight / h; | |
int width = options.outWidth; | |
int height = options.outHeight; | |
if (scalex <= 1 && scaley <= 1) { | |
width = options.outWidth; | |
height = options.outHeight; | |
} else if (scalex < scaley) { | |
width = (int) (options.outWidth / scaley); | |
height = (int) (options.outHeight / scaley); | |
} else if (scalex >= scaley) { | |
width = (int) (options.outWidth / scalex); | |
height = (int) (options.outHeight / scalex); | |
} | |
Log.d(TAG, "compress dst width = " + width + " height = " + height); | |
int retval = 1; | |
try { | |
File f = new File(dstPath, imageName); | |
f.createNewFile(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
//if (isSupportedByCjpeg(options.outMimeType)) { | |
// Log.d(TAG, "The photo is supported by cjpeg"); | |
// retval = compress(srcPath, dstPath + imageName, width, height); | |
//} else { | |
retval = saveAsJpegFile(srcPath, dstPath + imageName, width, height); | |
//} | |
Log.d(TAG, "retval = " + retval); | |
return retval; | |
} | |
private boolean isSupportedByCjpeg(String minetype) { | |
boolean result = false; | |
if (minetype != null) { | |
if (minetype.equalsIgnoreCase("image/jpeg")) { | |
result = true; | |
} | |
} | |
return result; | |
} | |
private int saveAsJpegFile(String srcPath, String dstPath, int w, int h) { | |
int result = 0; | |
BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inPreferredConfig = Config.RGB_565; | |
Bitmap origBmp = BitmapFactory.decodeFile(srcPath, options); | |
if (origBmp == null) { | |
Log.d(TAG, "Can't decode srcPath = " + srcPath); | |
return 1; | |
} | |
try { | |
origBmp.compress(CompressFormat.JPEG, 100, new FileOutputStream(dstPath)); | |
result = compress(srcPath, dstPath, w, h); | |
} catch (FileNotFoundException e) { | |
result = 1; | |
e.printStackTrace(); | |
} | |
origBmp.recycle(); | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://www.dropbox.com/s/9zl0zowcebr90eh/libcjpeg.so?dl=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment