可以把1.5M的壓縮到100Kb這樣哦,可以看到最后log,壓縮后的大小為100kb以內(nèi)
github地址:https://github.com/George-Soros/ImageTest
package com.tj.imagetest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import butterknife.Bind;
import butterknife.ButterKnife;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;
public class MainActivity extends AppCompatActivity {
@Bind(R.id.btn_photo)
Button mBtnPhoto;
@Bind(R.id.img_photo)
PhotoView mPhotoView;
private Uri mImageUri; //圖片路徑Uri
private String mImagePath;
private String mFileName; //圖片名稱
public static final int TAKE_PHOTO = 1;
public static final int CROP_PHOTO = 2;
private Bitmap mBitmap;
private PhotoViewAttacher mAttacher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ButterKnife.bind(this);
mBtnPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startCamera();
}
});
}
/***
* 啟動(dòng)相機(jī)去拍攝, 并截圖
*
*/
public void startCamera(){
//圖片名稱 時(shí)間命名
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date(System.currentTimeMillis());
mFileName = format.format(date);
Log.d("data", "mFileName=" + mFileName);
//存儲(chǔ)至DCIM文件夾
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM);
File outputImage = new File(path, mFileName +".jpg");
try {
if(outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
mImagePath = path + "/"+ mFileName + ".jpg";
Log.d("data","mImagePath="+mImagePath);
} catch(IOException e) {
e.printStackTrace();
}
//將File對(duì)象轉(zhuǎn)換為Uri并啟動(dòng)照相程序
mImageUri = Uri.fromFile(outputImage);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); //照相
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri); //指定圖片輸出地址
startActivityForResult(intent,CROP_PHOTO); //啟動(dòng)照相
//拍完照startActivityForResult() 結(jié)果返回onActivityResult()函數(shù)
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
return;
}
switch(requestCode) {
case TAKE_PHOTO:
Intent intent = new Intent("com.android.camera.action.CROP"); //剪裁
intent.setDataAndType(mImageUri, "image/*");
intent.putExtra("scale", true);
//設(shè)置寬高比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//設(shè)置裁剪圖片寬高
intent.putExtra("outputX", 400);
intent.putExtra("outputY", 340);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
//廣播刷新相冊(cè)
Intent intentBc = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intentBc.setData(mImageUri);
this.sendBroadcast(intentBc);
startActivityForResult(intent, CROP_PHOTO); //設(shè)置裁剪參數(shù)顯示圖片至ImageView
break;
case CROP_PHOTO:
try {
//圖片解析成Bitmap對(duì)象
mBitmap = BitmapFactory.decodeStream(
getContentResolver().openInputStream(mImageUri));
mAttacher = new PhotoViewAttacher(mPhotoView);
mPhotoView.setImageBitmap(mBitmap);
mAttacher.update();
//file:///storage/emulated/0/DCIM/20160707132811.jpg
Log.d("d","mImagePath="+mImagePath);
ImageUtils.bitmapToString(mImagePath);
} catch(FileNotFoundException e) {
e.printStackTrace();
}
break;
default:
break;
}
}
}
package com.tj.imagetest;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import java.io.ByteArrayOutputStream;
/**
* Created by Administrator on 2016/7/7 0007.
*/
public class ImageUtils {
// 根據(jù)路徑獲得圖片并壓縮,返回bitmap用于顯示
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
//計(jì)算圖片的縮放值
public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
//把bitmap轉(zhuǎn)換成String
public static String bitmapToString(String filePath) {
Bitmap bm = getSmallBitmap(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//1.5M的壓縮后在100Kb以內(nèi),測(cè)試得值,壓縮后的大小=94486字節(jié),壓縮后的大小=74473字節(jié)
//這里的JPEG 如果換成PNG,那么壓縮的就有600kB這樣
bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);
byte[] b = baos.toByteArray();
Log.d("d", "壓縮后的大小=" + b.length);
return Base64.encodeToString(b, Base64.DEFAULT);
}
}
其中PhotoView不明白,看http://www.itdecent.cn/p/6e38712e310f