Android實(shí)現(xiàn)裁剪照片功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)裁剪照片功能的具體代碼,供大家參考,具體內(nèi)容如下
1. 從相冊(cè)選擇照片進(jìn)行裁剪
從相冊(cè)選擇照片并裁剪:
/** ?* 從相冊(cè)選擇照片進(jìn)行裁剪 ?*/ 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/*");//從所有圖片中進(jìn)行選擇 ? ? intent.putExtra("crop", "true");//設(shè)置為裁切 ? ? 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);//將裁切的結(jié)果輸出到指定的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對(duì)象的圖片加載到內(nèi)存 ? ? ? ? ? ? imgShow.setImageBitmap(bitmap); ? ? ? ? } catch (FileNotFoundException e) { ? ? ? ? ? ? // TODO Auto-generated catch block ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } ? ? break;
程序運(yùn)行效果圖:
2. 從相機(jī)拍取照片進(jìn)行裁剪
控制相機(jī)拍照并將照片保存到指定位置:
/** ?* 從相機(jī)拍取照片進(jìn)行裁剪 ?*/ private void cropFromTake() { ? ? // TODO Auto-generated method stub ? ? Intent intent=new Intent(); ? ? intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設(shè)置Action為拍照 ? ? intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//將拍取的照片保存到指定URI ? ? startActivityForResult(intent, TAKE_PIC); }
裁剪已經(jīng)排好的照片并顯示在ImageView上:
case TAKE_PIC: ? ? if (resultCode==RESULT_OK) { ? ? ? ? cropImageUri(imageUri, 600, 600, CROP_PIC); ? ? } break;
/** ?* 裁剪指定uri對(duì)應(yīng)的照片 ?* @param imageUri:uri對(duì)應(yīng)的照片 ?* @param outputX:裁剪寬 ?* @param outputY:裁剪高 ?* @param requestCode:請(qǐng)求碼 ?*/ 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); }
程序運(yùn)行效果圖:
3.完整項(xiàng)目代碼:
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; ? /** ?* 從相冊(cè)選擇照片進(jìn)行裁剪,從相機(jī)拍取照片進(jìn)行裁剪 ?* @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; ?? ?@Override ?? ?protected void onCreate(Bundle savedInstanceState) { ?? ??? ?super.onCreate(savedInstanceState); ?? ??? ?setContentView(R.layout.activity_main); ?? ??? ?//初始化imageUri ?? ??? ?imageUri=Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "test.jpg")); ?? ??? ?imgShow=(ImageView)findViewById(R.id.imgShow); ?? ?} ?? ?@Override ?? ?protected void onActivityResult(int requestCode, int resultCode, Intent data) { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?switch (requestCode) { ?? ??? ?case SELECT_PIC: ?? ??? ??? ?if (resultCode==RESULT_OK) { ?? ??? ??? ??? ?try { ?? ??? ??? ??? ??? ?Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver(). ?? ??? ??? ??? ??? ??? ??? ?openInputStream(imageUri));//將imageUri對(duì)象的圖片加載到內(nèi)存 ?? ??? ??? ??? ??? ?imgShow.setImageBitmap(bitmap); ?? ??? ??? ??? ?} catch (FileNotFoundException e) { ?? ??? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ??? ?e.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對(duì)象的圖片加載到內(nèi)存 ?? ??? ??? ??? ??? ?imgShow.setImageBitmap(bitmap); ?? ??? ??? ??? ?} catch (FileNotFoundException e) { ?? ??? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?default: ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?super.onActivityResult(requestCode, resultCode, data); ?? ?} ?? ?/** ?? ? * 裁剪指定uri對(duì)應(yīng)的照片 ?? ? * @param imageUri:uri對(duì)應(yīng)的照片 ?? ? * @param outputX:裁剪寬 ?? ? * @param outputY:裁剪高 ?? ? * @param requestCode:請(qǐng)求碼 ?? ? */ ?? ?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://從相冊(cè)選擇照片進(jìn)行裁剪 ?? ??? ??? ?cropFromGallery(); ?? ??? ??? ?break; ?? ??? ?case R.id.btnCropFromTake://從相機(jī)拍取照片進(jìn)行裁剪 ?? ??? ??? ?cropFromTake(); ?? ??? ??? ?break; ? ?? ??? ?default: ?? ??? ??? ?break; ?? ??? ?} ?? ?} ?? ?/** ?? ? * 從相機(jī)拍取照片進(jìn)行裁剪 ?? ? */ ?? ?private void cropFromTake() { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?Intent intent=new Intent(); ?? ??? ?intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設(shè)置Action為拍照 ?? ??? ?intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//將拍取的照片保存到指定URI ?? ??? ?startActivityForResult(intent, TAKE_PIC); ?? ?} ?? ?/** ?? ? * 從相冊(cè)選擇照片進(jìn)行裁剪 ?? ? */ ?? ?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/*");//從所有圖片中進(jìn)行選擇 ?? ??? ?intent.putExtra("crop", "true");//設(shè)置為裁切 ?? ??? ?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);//將裁切的結(jié)果輸出到指定的Uri ?? ??? ?intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//裁切成的圖片的格式 ?? ??? ?intent.putExtra("noFaceDetection", true); // no face detection ?? ??? ?startActivityForResult(intent, SELECT_PIC); ? ? ?? ?} }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)拍照、選擇圖片并裁剪圖片功能
- Android裁剪圖片為圓形圖片的實(shí)現(xiàn)原理與代碼
- 解決Android從相冊(cè)中獲取圖片出錯(cuò)圖片卻無法裁剪問題的方法
- Android實(shí)現(xiàn)從本地圖庫/相機(jī)拍照后裁剪圖片并設(shè)置頭像
- Android 7.0中拍照和圖片裁剪適配的問題詳解
- Android自定義View實(shí)現(xiàn)照片裁剪框與照片裁剪功能
- Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理)
- Android編程實(shí)現(xiàn)調(diào)用系統(tǒng)圖庫與裁剪圖片功能
- Android拍照或從圖庫選擇圖片并裁剪
- Android調(diào)用系統(tǒng)拍照裁剪圖片模糊的解決方法
相關(guān)文章
Android系統(tǒng)實(shí)現(xiàn)DroidPlugin插件機(jī)制
這篇文章主要為大家詳細(xì)介紹了Android系統(tǒng)上實(shí)現(xiàn)DroidPlugin插件機(jī)制,可以在無需安裝、修改的情況下運(yùn)行APK文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android實(shí)現(xiàn)圓角ListView效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圓角ListView效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10Android相冊(cè)效果(使用C#和Java分別實(shí)現(xiàn))
這篇文章主要介紹了Android相冊(cè)效果(使用C#和Java分別實(shí)現(xiàn)),原來C#也可以開發(fā)APP,小編第一次見了~感覺不錯(cuò),因?yàn)樾【帟簳r(shí)不喜歡Java,所以,需要的朋友可以參考下2015-06-06Android自定義webView頭部進(jìn)度加載效果
這篇文章主要介紹了Android自定義webView頭部進(jìn)度加載效果,小編畫一條進(jìn)度線,然后加載webview上面,具體實(shí)現(xiàn)代碼大家參考下本文2017-11-11Android統(tǒng)一依賴管理的三種方式總結(jié)
為了項(xiàng)目的管理,依賴包的紡一管理是必要的,下面這篇文章主要給大家介紹了關(guān)于Android統(tǒng)一依賴管理的三種方式,文中通過實(shí)例代碼和圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01