對android拍照和選取照片進行封裝

nianxin 2016-06-07 11:02

在android開發中,經常涉及到從系統相冊中選擇照片作為頭像,或則調用系統相機,然后照相作為頭像。大部分的項目都會用到,然后就打算把他封裝的通用一點。

目標:1.簡單,方便調用。2.能夠對圖片進行裁剪3.復用性要強。在別的項目只需要導入libray就可以。

那么設計我們這一個小功能的實現吧!

首先,我們要知道怎么調用這個工具類:

import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import com.example.camera.CameraProxy; import com.example.camera.CameraResult; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends Activity implements CameraResult { private CameraProxy cameraProxy; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cameraProxy = new CameraProxy(this, MainActivity.this); ButterKnife.inject(this); } @Override public void onSuccess(String filePath) { Toast.makeText(this, "file:》》》" + filePath, Toast.LENGTH_LONG).show(); } @Override public void onFail(String message) { Log.d("same_info", "message:" + message); } //拍照 @OnClick(R.id.bt_click_1) public void takePhoto() { cameraProxy.getPhoto2Camera("xxxx.jpg"); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); cameraProxy.onResult(requestCode, resultCode, data); } }

方便吧,你只需要在Activity中存在有CameraProxy的實例,然后在onActivityResult,把數據傳給CameraProxy,就可以在onSuccess接收,你所要處理的圖片的。總體代碼量沒有超過10行哦。

現在,我們一起來設計這個工具類吧。我們需要一個接口。能夠完成,調用選擇圖片,或則調用系統相機。把我們需要的照片傳給我們。這樣的話,我們就可以在接口中的回調來處理自己的業務邏輯了。

/** * Created by nianxin on 15/9/22. * <p/> * 對圖片操作的代理 */ public class CameraProxy { //相機核心類 private CameraCore cameraCore; public CameraProxy(CameraResult cameraResult, Activity activity) { cameraCore = new CameraCore(cameraResult, activity); } //拍照 public void getPhoto2Camera(String path) { Uri uri = Uri.fromFile(new File(path)); cameraCore.getPhoto2Camera(uri); } //拍照截圖 public void getPhoto2CameraCrop(String path) { Uri uri = Uri.fromFile(new File(path)); cameraCore.getPhoto2CameraCrop(uri); } //選擇照片 public void getPhoto2Album(String path) { Uri uri = Uri.fromFile(new File(path)); cameraCore.getPhoto2Album(uri); } //選擇照片,截圖 public void getPhoto2AlbumCrop(String path) { Uri uri = Uri.fromFile(new File(path)); cameraCore.getPhoto2AlbumCrop(uri); } //接受ActivityResult public void onResult(int requestCode, int resultCode, Intent data) { cameraCore.onResult(requestCode, resultCode, data); } }

CameraProxy調用CameraCore中的代碼,具體實現在CameraCore.

import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.provider.MediaStore; import android.text.TextUtils; import java.io.File; /** * Created by nianxin on 15/9/22. */ public class CameraCore { //調用系統相機的Code public static final int REQUEST_TAKE_PHOTO_CODE = 1001; //拍照裁剪的Code public static final int REQUEST_TAKE_PHOTO_CROP_CODE = 1003; //調用系統圖庫的Code public static final int REQUEST_TAKE_PICTRUE_CODE = 1002; //調用系統圖庫裁剪Code public static final int REQUEST_TAKE_PICTRUE_CROP_CODE = 1004; //裁剪的Code public static final int REQUEST_TAKE_CROP_CODE = 1005; //截取圖片的高度 public static final int REQUEST_HEIGHT = 400; //截取圖片的寬度 public static final int REQUEST_WIDTH = 400; //用來存儲照片的URL private Uri photoURL; //調用照片的Activity private Activity activity; //回調函數 private CameraResult cameraResult; public CameraCore(CameraResult cameraResult, Activity activity) { this.cameraResult = cameraResult; this.activity = activity; } //調用系統照相機,對Intent參數進行封裝 protected Intent startTakePhoto(Uri photoURL) { this.photoURL = photoURL; Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設置Action為拍照 intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURL);//將拍取的照片保存到指定URI return intent; } //調用系統圖庫,對Intent參數進行封裝 protected Intent startTakePicture(Uri photoURL) { this.photoURL = photoURL; Intent intent = new Intent(); intent.setAction(Intent.ACTION_PICK); intent.setType("image/*");//從所有圖片中進行選擇 return intent; } //調用系統裁剪圖片,對Intent參數進行封裝 protected Intent takeCropPicture(Uri photoURL, int with, int height) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(photoURL, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", with); intent.putExtra("outputY", height); intent.putExtra("scale", true); intent.putExtra("scaleUpIfNeeded", true);//黑邊 intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURL); intent.putExtra("return-data", false); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection return intent; } //拍照 public void getPhoto2Camera(Uri uri) { activity.startActivityForResult(startTakePhoto(uri), REQUEST_TAKE_PHOTO_CODE); } //拍照后截屏 public void getPhoto2CameraCrop(Uri uri) { Intent intent = startTakePhoto(uri); // intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//將拍取的照片保存到指定URI activity.startActivityForResult(intent, REQUEST_TAKE_PHOTO_CROP_CODE); } //獲取系統相冊 public void getPhoto2Album(Uri uri) { activity.startActivityForResult(startTakePicture(uri), REQUEST_TAKE_PICTRUE_CODE); } //獲取系統相冊后裁剪 public void getPhoto2AlbumCrop(Uri uri) { activity.startActivityForResult(startTakePicture(uri), REQUEST_TAKE_PICTRUE_CROP_CODE); } //處理onActivityResult public void onResult(int requestCode, int resultCode, Intent intent) { if (resultCode == Activity.RESULT_OK) { switch (requestCode) { //選擇系統圖庫 case REQUEST_TAKE_PICTRUE_CODE: //獲取系統返回的照片的Uri photoURL = intent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; //從系統表中查詢指定Uri對應的照片 Cursor cursor = activity.getContentResolver().query(photoURL, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); //獲取照片路徑 cursor.close(); if (!TextUtils.isEmpty(picturePath)) { cameraResult.onSuccess(picturePath); } else { cameraResult.onFail("文件沒找到"); } break; //選擇系統圖庫.裁剪 case REQUEST_TAKE_PICTRUE_CROP_CODE: photoURL = intent.getData(); activity.startActivityForResult(takeCropPicture(photoURL, REQUEST_HEIGHT, REQUEST_WIDTH), REQUEST_TAKE_CROP_CODE); break; //調用相機 case REQUEST_TAKE_PHOTO_CODE: cameraResult.onSuccess(photoURL.getPath()); break; //調用相機,裁剪 case REQUEST_TAKE_PHOTO_CROP_CODE: activity.startActivityForResult(takeCropPicture(Uri.fromFile(new File(photoURL.getPath())), REQUEST_HEIGHT, REQUEST_WIDTH), REQUEST_TAKE_CROP_CODE); break; //裁剪之后的回調 case REQUEST_TAKE_CROP_CODE: String path = getPic2Uri(photoURL, activity); cameraResult.onSuccess(path); break; default: break; } } } public static String getPic2Uri(Uri contentUri, Context context) { try { Activity ac = (Activity) context; String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = ac.managedQuery(contentUri, proj, null, null, null); int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } catch (Exception e) { return contentUri.getPath(); } } }

代碼中注釋已經非常清楚了,那么說說我為什么會這樣來寫吧:

1.這樣的寫法,在UI層,只需要有CameraProxy的實例就可以調用拍照,選擇系統圖冊,和對圖片進行裁剪的方法。然后獲取到對應的圖片,UI層大概只需要10行代碼。2.這樣的寫法,可以在以后的項目中,直接導入libray,然后可以在這個的基礎上進行擴展,改變的話只是CameraCore里面的代碼,集成非常方便。

有時候,覺得自己像一只蝸牛一樣,拖著沉重的貝殼,努力的爬阿爬,然后回頭看看,卻什么都沒有改變。

編輯推薦
  • 相關內容
    河北排列7玩法