最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現(xiàn)裁剪照片功能

 更新時間:2022年03月29日 09:44:46   作者:CrazyCodeBoy  
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)裁剪照片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)裁剪照片功能的具體代碼,供大家參考,具體內(nèi)容如下

1.   從相冊選擇照片進(jìn)行裁剪

從相冊選擇照片并裁剪:

/**
?* 從相冊選擇照片進(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對象的圖片加載到內(nèi)存
? ? ? ? ? ? imgShow.setImageBitmap(bitmap);
? ? ? ? } catch (FileNotFoundException e) {
? ? ? ? ? ? // TODO Auto-generated catch block
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? break;

程序運行效果圖:

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對應(yīng)的照片
?* @param imageUri:uri對應(yīng)的照片
?* @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;
?
/**
?* 從相冊選擇照片進(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對象的圖片加載到內(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對象的圖片加載到內(nèi)存
?? ??? ??? ??? ??? ?imgShow.setImageBitmap(bitmap);
?? ??? ??? ??? ?} catch (FileNotFoundException e) {
?? ??? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?break;
?? ??? ?default:
?? ??? ??? ?break;
?? ??? ?}
?? ??? ?super.onActivityResult(requestCode, resultCode, data);
?? ?}
?? ?/**
?? ? * 裁剪指定uri對應(yīng)的照片
?? ? * @param imageUri:uri對應(yīng)的照片
?? ? * @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://從相冊選擇照片進(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);
?? ?}
?? ?/**
?? ? * 從相冊選擇照片進(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); ? ?
?? ?}
}

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

相關(guān)文章

最新評論

镇沅| 潞城市| 屯留县| 郯城县| 平果县| 怀柔区| 龙南县| 东山县| 广汉市| 海安县| 遂昌县| 从化市| 龙陵县| 海兴县| 和龙市| 卫辉市| 大洼县| 平谷区| 张掖市| 大竹县| 米林县| 屏东市| 亚东县| 高雄市| 乐东| 当雄县| 开阳县| 蒙山县| 峨边| 工布江达县| 秦安县| 柘城县| 长白| 台江县| 阜新市| 望谟县| 朝阳县| 年辖:市辖区| 翼城县| 太和县| 柳江县|