package com.zwk.ezandroid.util;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;
import java.io.File;
import static android.app.Activity.RESULT_CANCELED;
/**
* 拍照,照片選取,裁剪,統一封裝
*/
public class WKCameraUtil {
private static final int CAMERA_CODE = 1;
private static final int GALLERY_CODE = 2;
private static final int CROP_CODE = 3;
public Activity act;
private File photoFile;
private Uri photoUri;
public WKCameraUtil(Activity act){
// this(act,new File(App.getInstance().getExternalCacheDir(), "ezandroid\tempPhoto.jpg"));
this(act,new File(App.DirectoryImagePersonPhoto, "tempPhoto.jpg"));
}
public WKCameraUtil(Activity act, File file){
this.act=act;
photoFile = file;
}
public void start(){
String[] type = { act.getResources().getString(R.string.GENERAL_CAMERA),
act.getResources().getString(R.string.GENERAL_SELECT_PICTURE) };
new AlertDialog.Builder(act).setItems(type,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String state = Environment.getExternalStorageState();
if (!state.equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(act, "SDCard不可用,請打開檔案讀取權限", Toast.LENGTH_SHORT).show();
dialog.dismiss();
return;
}
switch (which) {
case 0:
chooseFromCamera();
break;
case 1:
chooseFromGallery();
break;
}
}
}).show();
}
/**
* 從相冊選擇圖檔
*/
private void chooseFromGallery() {
photoUri = Uri.fromFile(photoFile);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
act.startActivityForResult(intent, GALLERY_CODE);
}
// 拍照選擇圖檔
private void chooseFromCamera() {
// 調用系統相機
photoUri = FileUtil.getUriByFile(photoFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
act.startActivityForResult(intent, CAMERA_CODE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data, WKCallback call) {
if (resultCode == RESULT_CANCELED) {
T.toast("取消");
return;
}
switch (requestCode) {
case CAMERA_CODE:
startImageZoom(photoUri);
break;
case GALLERY_CODE:
startImageZoom(data.getData());
break;
case CROP_CODE:
call.onCall(Uri.fromFile(photoFile));
break;
default:
break;
}
}
/**
* 通過Uri傳遞圖像資訊以供裁剪
* @param uri
*/
private void startImageZoom(Uri uri) {
// 建構隐式Intent來啟動裁剪程式
Intent intent = new Intent("com.android.camera.action.CROP");
// 設定資料uri和類型為圖檔類型
intent.setDataAndType(uri, "image/*");
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// 顯示View為可裁剪的
intent.putExtra("crop", true);
// 裁剪的寬高的比例為1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 輸出圖檔的寬高均為150
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("output", Uri.fromFile(photoFile));
// 裁剪之後的資料是通過Intent傳回
intent.putExtra("return-data", false);
act.startActivityForResult(intent, CROP_CODE);
}
public interface WKCallback<T>{
void onCall(T t);
}
}
使用方式:
WKCameraUtil wkCamera;
private OnClickListener linstener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_person_photo:
wkCamera = new WKCameraUtil(this);
//wkCamera = new WKCameraUtil(this,new File("檔案儲存路徑"));
wkCamera.start();
break;
}
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
wkCamera.onActivityResult(requestCode,resultCode,data,new WKCameraUtil.WKCallback() {
@Override
public void onCall(Uri photoUri) {
Bitmap resultBitmap = BitmapUtil.getBitmap(photoUri, PersonCenterActivity.this);
}
});
}
public static Bitmap getBitmap(Uri photoUri, Context context) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inPurgeable = true;
options.inInputShareable = true;
options.inPreferredConfig = Config.RGB_565;
InputStream inputStream = null;
Bitmap resultBitmap = null;
try {
inputStream = context.getContentResolver()
.openInputStream(photoUri);
resultBitmap = BitmapFactory.decodeStream(inputStream, null, options);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (null!=inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return srcBitmap;
}