圖片壓縮演算法 3M壓縮到200K
MainActivity:
BitmapUtils:
package com.example.yasuo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.graphics.Bitmap;
import android.os.Environment;
/**
* 將bitmap儲存在SD卡
* @author xinruzhou
*
*/
public class BitmapUtils {
/**
*
* @param b Bitmap
* <a href="\"http://www.eoeandroid.com/home.php?mod=space&uid=7300\"" target="\"_blank\"">@return</a> 圖片儲存的位置
* @throws FileNotFoundException
*/
public static String saveImg(Bitmap b,String name) throws Exception{
String path = Environment.getExternalStorageDirectory().getPath()+File.separator+"test/headImg/";
File mediaFile = new File(path + File.separator + name + ".jpg");
if(mediaFile.exists()){
mediaFile.delete();
}
if(!new File(path).exists()){
new File(path).mkdirs();
}
mediaFile.createNewFile();
FileOutputStream fos = new FileOutputStream(mediaFile);
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
b.recycle();
b = null;
System.gc();
return mediaFile.getPath();
}
}
MD5Utils:
package com.example.yasuo;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
public class MainActivity extends Activity {
final String PATH = Environment.getExternalStorageDirectory()
+ "/Bst/a.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
try {
getThumbUploadPath(PATH,480);
} catch (Exception e) {
Log.e("eeee", e.toString());
}
}
private String getThumbUploadPath(String oldPath,int bitmapMaxWidth) throws Exception {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(oldPath, options);
int height = options.outHeight;
int width = options.outWidth;
int reqHeight = 0;
int reqWidth = bitmapMaxWidth;
reqHeight = (reqWidth * height)/width;
// 在記憶體中建立bitmap物件,這個物件按照縮放大小建立的
options.inSampleSize = calculateInSampleSize(options, bitmapMaxWidth, reqHeight);
// System.out.println("calculateInSampleSize(options, 480, 800);==="
// + calculateInSampleSize(options, 480, 800));
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(oldPath, options);
//Log.e("asdasdas", "reqWidth->"+reqWidth+"---reqHeight->"+reqHeight);
Bitmap bbb = compressImage(Bitmap.createScaledBitmap(bitmap, bitmapMaxWidth, reqHeight, false));
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
return BitmapUtils.saveImg(bbb, MD5Utils.md5(timeStamp));
}
private int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
private Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 80, baos);// 質量壓縮方法,這裡100表示不壓縮,把壓縮後的資料存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 100) { // 迴圈判斷如果壓縮後圖片是否大於100kb,大於繼續壓縮
options -= 10;// 每次都減少10
baos.reset();// 重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這裡壓縮options%,把壓縮後的資料存放到baos中
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把壓縮後的資料baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream資料生成圖片
return bitmap;
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
public class MainActivity extends Activity {
final String PATH = Environment.getExternalStorageDirectory()
+ "/Bst/a.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
try {
getThumbUploadPath(PATH,480);
} catch (Exception e) {
Log.e("eeee", e.toString());
}
}
private String getThumbUploadPath(String oldPath,int bitmapMaxWidth) throws Exception {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(oldPath, options);
int height = options.outHeight;
int width = options.outWidth;
int reqHeight = 0;
int reqWidth = bitmapMaxWidth;
reqHeight = (reqWidth * height)/width;
// 在記憶體中建立bitmap物件,這個物件按照縮放大小建立的
options.inSampleSize = calculateInSampleSize(options, bitmapMaxWidth, reqHeight);
// System.out.println("calculateInSampleSize(options, 480, 800);==="
// + calculateInSampleSize(options, 480, 800));
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(oldPath, options);
//Log.e("asdasdas", "reqWidth->"+reqWidth+"---reqHeight->"+reqHeight);
Bitmap bbb = compressImage(Bitmap.createScaledBitmap(bitmap, bitmapMaxWidth, reqHeight, false));
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
return BitmapUtils.saveImg(bbb, MD5Utils.md5(timeStamp));
}
private int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
private Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 80, baos);// 質量壓縮方法,這裡100表示不壓縮,把壓縮後的資料存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 100) { // 迴圈判斷如果壓縮後圖片是否大於100kb,大於繼續壓縮
options -= 10;// 每次都減少10
baos.reset();// 重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這裡壓縮options%,把壓縮後的資料存放到baos中
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把壓縮後的資料baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream資料生成圖片
return bitmap;
}
}
BitmapUtils:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.graphics.Bitmap;
import android.os.Environment;
/**
* 將bitmap儲存在SD卡
* @author xinruzhou
*
*/
public class BitmapUtils {
/**
*
* @param b Bitmap
* <a href="\"http://www.eoeandroid.com/home.php?mod=space&uid=7300\"" target="\"_blank\"">@return</a> 圖片儲存的位置
* @throws FileNotFoundException
*/
public static String saveImg(Bitmap b,String name) throws Exception{
String path = Environment.getExternalStorageDirectory().getPath()+File.separator+"test/headImg/";
File mediaFile = new File(path + File.separator + name + ".jpg");
if(mediaFile.exists()){
mediaFile.delete();
}
if(!new File(path).exists()){
new File(path).mkdirs();
}
mediaFile.createNewFile();
FileOutputStream fos = new FileOutputStream(mediaFile);
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
b.recycle();
b = null;
System.gc();
return mediaFile.getPath();
}
}
MD5Utils:
package com.example.yasuo;
import java.security.MessageDigest;
public class MD5Utils {
/**
* MD5加密
* @param str 要加密的字串
* @return 加密後的字串
*/
public static String md5(String str) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
e.printStackTrace();
return "";
}
char[] charArray = str.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++) {
byteArray<i> = (byte) charArray<i>;
}
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes<i>) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
}
import java.security.MessageDigest;
public class MD5Utils {
/**
* MD5加密
* @param str 要加密的字串
* @return 加密後的字串
*/
public static String md5(String str) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
e.printStackTrace();
return "";
}
char[] charArray = str.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++) {
byteArray<i> = (byte) charArray<i>;
}
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes<i>) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
}
相關文章
- Nginx網路壓縮 CSS壓縮 圖片壓縮 JSON壓縮NginxCSSJSON
- 前端圖片壓縮 - H5&Uni-App圖片壓縮前端H5APP
- ??圖片壓縮CanvasCanvas
- IOS圖片壓縮iOS
- canvas 壓縮圖片Canvas
- 圖片壓縮知識梳理(0) 圖片壓縮學習計劃
- iOS 圖片壓縮方法iOS
- 前端圖片壓縮方案前端
- png格式如何壓縮,圖片壓縮工具哪個好
- SmallImage for Mac(圖片壓縮工具)Mac
- js上傳圖片壓縮JS
- js圖片壓縮推薦JS
- JNI實現圖片壓縮
- android下圖片壓縮Android
- java後臺壓縮圖片Java
- 圖片壓縮怎樣操作?分享幾種實用的批次圖片壓縮技巧
- 影像體積壓縮工具JPEG Jackal更好的壓縮圖片
- 前端的圖片壓縮image-compressor(可在圖片上傳前實現圖片壓縮)前端
- 一個強大的圖片壓縮演算法—近微信壓縮機制的Luban演算法
- 圖片壓縮不求人,3個親測實用高效的圖片壓縮神器
- 前端圖片壓縮及上傳前端
- 利用 canvas 實現圖片壓縮Canvas
- Image Optimizer for Mac(圖片壓縮工具)Mac
- Bitmap的圖片壓縮彙總
- js 壓縮圖片 H5JSH5
- Flutter實現Luban圖片壓縮庫演算法Flutter演算法
- excel檔案裡的圖片怎麼壓縮?excel檔案裡圖片的壓縮方法Excel
- JAVA壓縮和解壓縮Java
- zip壓縮和解壓縮
- 【前端】壓縮圖片以及圖片相關概念前端
- vue 上傳圖片進行壓縮圖片Vue
- Android壓縮圖片後再上傳圖片Android
- 使用 JavaScript 壓縮和翻轉圖片JavaScript
- JS—圖片壓縮上傳(單張)JS
- Squeezer for Mac(Mac圖片壓縮軟體)Mac
- Android 中圖片壓縮分析(上)Android
- gulp-imagemin版本9圖片壓縮
- Android-壓縮大圖到容量超小的圖片Android