欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)拍照、選擇相冊(cè)圖片并裁剪功能

 更新時(shí)間:2016年12月02日 14:55:37   作者:遲做總比不做強(qiáng)  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)拍照、選擇相冊(cè)圖片并裁剪功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

通過(guò)拍照或相冊(cè)中獲取圖片,并進(jìn)行裁剪操作,然后把圖片顯示到ImageView上。
 當(dāng)然也可以上傳到服務(wù)器(項(xiàng)目中絕大部分情況是上傳到服務(wù)器),參考網(wǎng)上資料及結(jié)合項(xiàng)目實(shí)際情況,
 測(cè)試了多款手機(jī)暫時(shí)沒(méi)有發(fā)現(xiàn)嚴(yán)重問(wèn)題。代碼有注釋,直接貼代碼:

public class UploadPicActivity extends Activity implements View.OnClickListener {
 private Button take_photo_btn;
 private Button select_photo_btn;
 private ImageView photo_iv;
 //使用照相機(jī)拍照獲取圖片
 public static final int TAKE_PHOTO_CODE = 1;
 //使用相冊(cè)中的圖片
 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;
   //選擇圖庫(kù)
   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"
/***
 * 使用照相機(jī)拍照,拍照后的圖片會(huì)存放在相冊(cè)中。使用這種方式好處就是:獲取的圖片是拍照后的原圖,
 * 如果不實(shí)用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, "內(nèi)存卡不存在", Toast.LENGTH_LONG).show();
  }
 }

 /***
  * 從相冊(cè)中取圖片
  */
 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) {
   //從相冊(cè)取圖片,有些手機(jī)有異常情況,請(qǐng)注意
   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) {
      //這里可以把圖片進(jìn)行上傳到服務(wù)器操作
      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是設(shè)置在開(kāi)啟的Intent中設(shè)置顯示的VIEW可裁剪
  intent.putExtra("crop", "true");
  // 去黑邊
  intent.putExtra("scale", true);
  intent.putExtra("scaleUpIfNeeded", true);
  // aspectX aspectY 是寬高的比例,根據(jù)自己情況修改
  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());
  //取消人臉識(shí)別功能
  intent.putExtra("noFaceDetection", true);
  //設(shè)置返回的uri
  intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
  //設(shè)置為不返回?cái)?shù)據(jù)
  intent.putExtra("return-data", false);
  startActivityForResult(intent, REQUE_CODE_CROP);
 }

 /**
  * @param
  * @description 把Uri轉(zhuǎn)換為文件路徑
  * @author ldm
  * @time 2016/11/30 15:22
  */
 private String uriToFilePath(Uri uri) {
  //獲取圖片數(shù)據(jù)
  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);
 }
}

布局文件長(zhǎng)這樣:

<?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及權(quán)限:

<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"/>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論