图片压缩:可以从两个方面入手:
第一:将图片剪切:
//压缩图片按倍数压:
private Options getBitmapOption(int inSampleSize){
System.gc();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inSampleSize = inSampleSize;
return options;
}
第二:质量压缩:
重要的函数: bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//如果签名是png的话,则不管quality是多少,都不会进行质量的压缩
//获取bitmap内存
public static long getSizeOfBitmap(Bitmap bitmap)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//这里100的话表示不压缩质量
long length=baos.toByteArray().length/1024;//读出图片的kb大小
return length;
}
/压缩图片到指定范围
public static byte[] compressBitmap(Bitmap bitmap,float size){
if(bitmap==null||getSizeOfBitmap(bitmap)<=size){
return null;//如果图片本身的大小已经小于这个大小了,就没必要进行压缩
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//如果签名是png的话,则不管quality是多少,都不会进行质量的压缩
int quality=100;
while (baos.toByteArray().length / 1024f>size) {
quality=quality-4;// 每次都减少4 //如果把太大的图片压缩太多倍的的话,会压缩很久
if(quality<=0){
break;
}
baos.reset();// 重置baos即清空baos
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
}
return baos.toByteArray();
}
而在开发中,我一般压缩都是两者混合着用:
比如下面例子:
我将一个图片压缩到大概60kb左右,然后上传:
try
{
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(
httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
+tupianlujin+ filename.substring(filename.lastIndexOf("/") + 1)
+ "\""
+ end);
dos.writeBytes(end);
/////////////////////////////////////////////////////////////////////////////////////////图片压缩///////////////////////////////////////
//压缩图片:先转为Bitmap;然后裁剪,然后在转为文件格式
Bitmap bitmap=BitmapFactory.decodeFile(filename,getBitmapOption(2)); //首先减半先
InputStream fis ;
if(getSizeOfBitmap(bitmap)<=61.2f) //若大图小于61.2k,直接传
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
fis = new ByteArrayInputStream(baos.toByteArray());
}
else //压缩到特定范围
{
bitmap=BitmapFactory.decodeFile(filename,getBitmapOption(2)); //裁剪4分之一 //这步可以不要
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if(compressBitmap(bitmap,61.2f)==null)
{
fis = new ByteArrayInputStream(baos.toByteArray());
}
else
{
fis = new ByteArrayInputStream(compressBitmap(bitmap,61.2f));
}
}
/////////////////////////////////////////////////////////////////////////////////////////图片压缩///////////////////////////////////////
byte[] buffer = new byte[8192]; // 8k
int count = 0;
while ((count = fis.read(buffer)) != -1)
{
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine();
//Toast.makeText(this, result, Toast.LENGTH_LONG).show();
dos.close();
is.close();
}
catch (Exception e)
{
setTitle(e.getMessage());
}