亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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");//設(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.  從相機拍取照片進行裁剪

控制相機拍照并將照片保存到指定位置:

/**
?* 從相機拍取照片進行裁剪
?*/
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;
?
/**
?* 從相冊選擇照片進行裁剪,從相機拍取照片進行裁剪
?* @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://從相冊選擇照片進行裁剪
?? ??? ??? ?cropFromGallery();
?? ??? ??? ?break;
?? ??? ?case R.id.btnCropFromTake://從相機拍取照片進行裁剪
?? ??? ??? ?cropFromTake();
?? ??? ??? ?break;
?
?? ??? ?default:
?? ??? ??? ?break;
?? ??? ?}
?? ?}
?? ?/**
?? ? * 從相機拍取照片進行裁剪
?? ? */
?? ?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);
?? ?}
?? ?/**
?? ? * 從相冊選擇照片進行裁剪
?? ? */
?? ?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");//設(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)文章

  • 基于界面適配華為手機的虛擬按鍵的解決方法

    基于界面適配華為手機的虛擬按鍵的解決方法

    下面小編就為大家分享一篇基于界面適配華為手機的虛擬按鍵的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Android系統(tǒng)實現(xiàn)DroidPlugin插件機制

    Android系統(tǒng)實現(xiàn)DroidPlugin插件機制

    這篇文章主要為大家詳細介紹了Android系統(tǒng)上實現(xiàn)DroidPlugin插件機制,可以在無需安裝、修改的情況下運行APK文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android圖片描邊效果的實現(xiàn)

    Android圖片描邊效果的實現(xiàn)

    這篇文章主要給大家介紹了Android圖片描邊效果的實現(xiàn),在添加圖片描邊的時候我們用到的是圖片蒙版技術(shù),文中通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下
    2023-12-12
  • Android實現(xiàn)圓角ListView效果

    Android實現(xiàn)圓角ListView效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)圓角ListView效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Android Studio 下載視頻到本地

    Android Studio 下載視頻到本地

    這篇文章主要介紹了Android Studio 下載視頻到本地,利用GreenDao實現(xiàn)多線程斷點續(xù)傳,這樣的話,下次用戶再次下載時,將繼續(xù)上次數(shù)據(jù)庫的接著下載,這樣用戶體驗就會很好,也大大節(jié)省了成本.具體實現(xiàn)代碼大家參考下本文
    2018-03-03
  • Android相冊效果(使用C#和Java分別實現(xiàn))

    Android相冊效果(使用C#和Java分別實現(xiàn))

    這篇文章主要介紹了Android相冊效果(使用C#和Java分別實現(xiàn)),原來C#也可以開發(fā)APP,小編第一次見了~感覺不錯,因為小編暫時不喜歡Java,所以,需要的朋友可以參考下
    2015-06-06
  • Android自定義webView頭部進度加載效果

    Android自定義webView頭部進度加載效果

    這篇文章主要介紹了Android自定義webView頭部進度加載效果,小編畫一條進度線,然后加載webview上面,具體實現(xiàn)代碼大家參考下本文
    2017-11-11
  • 詳解Android中解析XML的方法

    詳解Android中解析XML的方法

    XML在各種開發(fā)中都廣泛應(yīng)用,Android也不例外。這篇文章主要介紹了詳解Android中解析XML的方法,有需要的可以了解一下。
    2016-11-11
  • Android統(tǒng)一依賴管理的三種方式總結(jié)

    Android統(tǒng)一依賴管理的三種方式總結(jié)

    為了項目的管理,依賴包的紡一管理是必要的,下面這篇文章主要給大家介紹了關(guān)于Android統(tǒng)一依賴管理的三種方式,文中通過實例代碼和圖文介紹的非常詳細,需要的朋友可以參考下
    2022-01-01
  • Android中巧妙的實現(xiàn)緩存詳解

    Android中巧妙的實現(xiàn)緩存詳解

    采用緩存,可以進一步大大緩解數(shù)據(jù)交互的壓力,有的時候為了快速查詢會被多次調(diào)用的數(shù)據(jù),或者構(gòu)建比較廢時的實例,我們一般使用緩存的方法。無論大型或小型應(yīng)用,靈活的緩存可以說不僅大大減輕了服務(wù)器的壓力,而且因為更快速的用戶體驗而方便了用戶。下面來一起看看吧。
    2016-11-11

最新評論