Android開發之裁剪照片

來源:互聯網
上載者:User

標籤:android從相簿選擇照片進行裁剪   android從相機拍取照片進行裁剪   控制相機拍照並將照片儲存到指定位置   

請尊重他人的勞動成果,轉載請註明出處:Android開發之裁剪照片 


1.   從相簿選擇照片進行裁剪

從相簿選擇照片並裁剪:

/** * 從相簿選擇照片進行裁剪 */private void cropFromGallery() {    // TODO Auto-generated method stub        Intent intent=new Intent();    intent.setAction(Intent.ACTION_PICK);//Pick an item from the data    intent.setType("image/*");//從所有圖片中進行選擇    intent.putExtra("crop", "true");//設定為裁切    intent.putExtra("aspectX", 1);//裁切的寬比例    intent.putExtra("aspectY", 1);//裁切的高比例    intent.putExtra("outputX", 600);//裁切的寬度    intent.putExtra("outputY", 600);//裁切的高度    intent.putExtra("scale", true);//支援縮放    intent.putExtra("return-data", false);    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//將裁切的結果輸出到指定的Uri    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//裁切成的圖片的格式    intent.putExtra("noFaceDetection", true); // no face detection    startActivityForResult(intent, SELECT_PIC);   }


將裁減好的照片顯示在顯示在ImagaView上:

case SELECT_PIC:    if (resultCode==RESULT_OK) {        try {            Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().                    openInputStream(imageUri));//將imageUri對象的圖片載入到記憶體            imgShow.setImageBitmap(bitmap);        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    break;

程式運行:



2.   從相機拍取照片進行裁剪

控制相機拍照並將照片儲存到指定位置:

/** * 從相機拍取照片進行裁剪 */private void cropFromTake() {    // TODO Auto-generated method stub    Intent intent=new Intent();    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設定Action為拍照    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//將拍取的照片儲存到指定URI    startActivityForResult(intent, TAKE_PIC);}

裁剪已經排好的照片並顯示在ImageView上:

case TAKE_PIC:    if (resultCode==RESULT_OK) {        cropImageUri(imageUri, 600, 600, CROP_PIC);    }    break;


/** * 裁剪指定uri對應的照片 * @param imageUri:uri對應的照片 * @param outputX:裁剪寬 * @param outputY:裁剪高 * @param requestCode:請求碼 */private void cropImageUri(Uri imageUri, int outputX, int outputY, int requestCode){    Intent intent = new Intent("com.android.camera.action.CROP");    intent.setDataAndType(imageUri, "image/*");    intent.putExtra("crop", "true");    intent.putExtra("aspectX", 1);    intent.putExtra("aspectY", 1);    intent.putExtra("outputX", outputX);    intent.putExtra("outputY", outputY);    intent.putExtra("scale", true);    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);    intent.putExtra("return-data", false);    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());    intent.putExtra("noFaceDetection", true); // no face detection    startActivityForResult(intent, requestCode);}

程式運行:



3.完整項目代碼:
package com.jph.cp;import java.io.File;import java.io.FileNotFoundException;import android.support.v7.app.ActionBarActivity;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.view.View;import android.widget.ImageView;/** * 從相簿選擇照片進行裁剪,從相機拍取照片進行裁剪 * @author JPH * Date:2014.10.09 */public class MainActivity extends ActionBarActivity {private final static int SELECT_PIC=0x123; private final static int TAKE_PIC=0x124; private final static int CROP_PIC=0x125; private Uri imageUri;private ImageView imgShow;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//初始化imageUriimageUri=Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "test.jpg"));imgShow=(ImageView)findViewById(R.id.imgShow);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubswitch (requestCode) {case SELECT_PIC:if (resultCode==RESULT_OK) {try {Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));//將imageUri對象的圖片載入到記憶體imgShow.setImageBitmap(bitmap);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}break;case TAKE_PIC:if (resultCode==RESULT_OK) {cropImageUri(imageUri, 600, 600, CROP_PIC);}break;case CROP_PIC:if (resultCode==RESULT_OK) {try {Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));//將imageUri對象的圖片載入到記憶體imgShow.setImageBitmap(bitmap);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}break;default:break;}super.onActivityResult(requestCode, resultCode, data);}/** * 裁剪指定uri對應的照片 * @param imageUri:uri對應的照片 * @param outputX:裁剪寬 * @param outputY:裁剪高 * @param requestCode:請求碼 */private void cropImageUri(Uri imageUri, int outputX, int outputY, int requestCode){    Intent intent = new Intent("com.android.camera.action.CROP");    intent.setDataAndType(imageUri, "image/*");    intent.putExtra("crop", "true");    intent.putExtra("aspectX", 1);    intent.putExtra("aspectY", 1);    intent.putExtra("outputX", outputX);    intent.putExtra("outputY", outputY);    intent.putExtra("scale", true);    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);    intent.putExtra("return-data", false);    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());    intent.putExtra("noFaceDetection", true); // no face detection    startActivityForResult(intent, requestCode);}public void cropPic(View view) {switch (view.getId()) {case R.id.btnCropFromGallery://從相簿選擇照片進行裁剪cropFromGallery();break;case R.id.btnCropFromTake://從相機拍取照片進行裁剪cropFromTake();break;default:break;}}/** * 從相機拍取照片進行裁剪 */private void cropFromTake() {// TODO Auto-generated method stubIntent intent=new Intent();intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設定Action為拍照intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//將拍取的照片儲存到指定URIstartActivityForResult(intent, TAKE_PIC);}/** * 從相簿選擇照片進行裁剪 */private void cropFromGallery() {// TODO Auto-generated method stubIntent intent=new Intent();intent.setAction(Intent.ACTION_PICK);//Pick an item from the dataintent.setType("image/*");//從所有圖片中進行選擇intent.putExtra("crop", "true");//設定為裁切intent.putExtra("aspectX", 1);//裁切的寬比例intent.putExtra("aspectY", 1);//裁切的高比例intent.putExtra("outputX", 600);//裁切的寬度intent.putExtra("outputY", 600);//裁切的高度intent.putExtra("scale", true);//支援縮放intent.putExtra("return-data", false);intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//將裁切的結果輸出到指定的Uriintent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//裁切成的圖片的格式intent.putExtra("noFaceDetection", true); // no face detectionstartActivityForResult(intent, SELECT_PIC);    }}


Android開發之裁剪照片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.