国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現拍照、選擇相冊圖片并裁剪功能

2019-12-12 04:26:49
字體:
來源:轉載
供稿:網友

通過拍照或相冊中獲取圖片,并進行裁剪操作,然后把圖片顯示到ImageView上。
 當然也可以上傳到服務器(項目中絕大部分情況是上傳到服務器),參考網上資料及結合項目實際情況,
 測試了多款手機暫時沒有發現嚴重問題。代碼有注釋,直接貼代碼:

public class UploadPicActivity extends Activity implements View.OnClickListener { private Button take_photo_btn; private Button select_photo_btn; private ImageView photo_iv; //使用照相機拍照獲取圖片 public static final int TAKE_PHOTO_CODE = 1; //使用相冊中的圖片 public static final int SELECT_PIC_CODE = 2; //圖片裁剪 private static final int PHOTO_CROP_CODE = 3; //定義圖片的Uri private Uri photoUri; //圖片文件路徑 private String picPath; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_upload_pic);  initViews(); } private void initViews() {  this.take_photo_btn = (Button) findViewById(R.id.take_photo_btn);  this.take_photo_btn.setOnClickListener(this);  this.select_photo_btn = (Button) findViewById(R.id.select_photo_btn);  this.select_photo_btn.setOnClickListener(this);  this.photo_iv = (ImageView) findViewById(R.id.photo_iv); } @Override public void onClick(View view) {  switch (view.getId()) {   //拍照   case R.id.take_photo_btn:    picTyTakePhoto();    break;   //選擇圖庫   case R.id.select_photo_btn:    pickPhoto();    break;  } } /**  * 拍照獲取圖片  */ private void picTyTakePhoto() {  //判斷SD卡是否存在  String SDState = Environment.getExternalStorageState();  if (SDState.equals(Environment.MEDIA_MOUNTED)) {   Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//"android.media.action.IMAGE_CAPTURE"/*** * 使用照相機拍照,拍照后的圖片會存放在相冊中。使用這種方式好處就是:獲取的圖片是拍照后的原圖, * 如果不實用ContentValues存放照片路徑的話,拍照后獲取的圖片為縮略圖有可能不清晰 */   ContentValues values = new ContentValues();   photoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);   intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);   startActivityForResult(intent, TAKE_PHOTO_CODE);  } else {   Toast.makeText(this, "內存卡不存在", Toast.LENGTH_LONG).show();  } } /***  * 從相冊中取圖片  */ private void pickPhoto() {  Intent intent = new Intent(Intent.ACTION_PICK, null);  intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,    "image/*");  startActivityForResult(intent, SELECT_PIC_CODE); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {  super.onActivityResult(requestCode, resultCode, data);  if (resultCode == Activity.RESULT_OK) {   //從相冊取圖片,有些手機有異常情況,請注意   if (requestCode == SELECT_PIC_CODE) {    if (null != data && null != data.getData()) {     photoUri = data.getData();     picPath = uriToFilePath(photoUri);     startPhotoZoom(photoUri, PHOTO_CROP_CODE);    } else {     Toast.makeText(this, "圖片選擇失敗", Toast.LENGTH_LONG).show();    }   } else if (requestCode == TAKE_PHOTO_CODE) {    String[] pojo = {MediaStore.Images.Media.DATA};    Cursor cursor = managedQuery(photoUri, pojo, null, null, null);    if (cursor != null) {     int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);     cursor.moveToFirst();     picPath = cursor.getString(columnIndex);     if (Build.VERSION.SDK_INT < 14) {      cursor.close();     }    }    if (picPath != null) {     photoUri = Uri.fromFile(new File(picPath));     startPhotoZoom(photoUri, PHOTO_CROP_CODE);    } else {     Toast.makeText(this, "圖片選擇失敗", Toast.LENGTH_LONG).show();    }   } else if (requestCode == PHOTO_CROP_CODE) {    if (photoUri != null) {     Bitmap bitmap = BitmapFactory.decodeFile(picPath);     if (bitmap != null) {      //這里可以把圖片進行上傳到服務器操作      photo_iv.setImageBitmap(bitmap);     }    }   }  } } /**  * @param  * @description 裁剪圖片  * @author ldm  * @time 2016/11/30 15:19  */ private void startPhotoZoom(Uri uri, int REQUE_CODE_CROP) {  Intent intent = new Intent("com.android.camera.action.CROP");  intent.setDataAndType(uri, "image/*");  // crop=true是設置在開啟的Intent中設置顯示的VIEW可裁剪  intent.putExtra("crop", "true");  // 去黑邊  intent.putExtra("scale", true);  intent.putExtra("scaleUpIfNeeded", true);  // aspectX aspectY 是寬高的比例,根據自己情況修改  intent.putExtra("aspectX", 3);  intent.putExtra("aspectY", 2);  // outputX outputY 是裁剪圖片寬高像素  intent.putExtra("outputX", 600);  intent.putExtra("outputY", 400);  intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());  //取消人臉識別功能  intent.putExtra("noFaceDetection", true);  //設置返回的uri  intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);  //設置為不返回數據  intent.putExtra("return-data", false);  startActivityForResult(intent, REQUE_CODE_CROP); } /**  * @param  * @description 把Uri轉換為文件路徑  * @author ldm  * @time 2016/11/30 15:22  */ private String uriToFilePath(Uri uri) {  //獲取圖片數據  String[] proj = {MediaStore.Images.Media.DATA};  //查詢  Cursor cursor = managedQuery(uri, proj, null, null, null);  //獲得用戶選擇的圖片的索引值  int image_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);  cursor.moveToFirst();  //返回圖片路徑  return cursor.getString(image_index); }}

布局文件長這樣:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="16dp"> <Button  android:id="@+id/take_photo_btn"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_marginTop="20dp"  android:gravity="center"  android:text="拍照"  android:textSize="16sp"/> <Button  android:id="@+id/select_photo_btn"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_marginTop="20dp"  android:gravity="center"  android:text="選擇圖片"  android:textSize="16sp"/> <ImageView  android:id="@+id/photo_iv"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_gravity="center_horizontal"  android:layout_marginTop="20dp"/></LinearLayout>

最后不要忘記在AndroidManifest.xml中添加UploadPicActivity及權限:

<uses-permission android:name="android.permission.CAMERA"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission  android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青海省| 鱼台县| 五大连池市| 托克托县| 广昌县| 鱼台县| 古蔺县| 德令哈市| 文安县| 抚松县| 朝阳区| 岱山县| 井冈山市| 桓台县| 乐至县| 衢州市| 阳谷县| 太原市| 米泉市| 全南县| 尼勒克县| 富民县| 石楼县| 阳朔县| 天气| 德化县| 荃湾区| 始兴县| 莎车县| 盐山县| 寿阳县| 宿松县| 射阳县| 宝应县| 孟连| 名山县| 乡宁县| 丹江口市| 洮南市| 邵武市| 沁水县|