Android編程圖片操作類(lèi)定義與用法示例【拍照,相冊(cè)選圖及裁剪】
本文實(shí)例講述了Android編程圖片操作類(lèi)定義與用法。分享給大家供大家參考,具體如下:
主界面類(lèi):拍照及選擇相冊(cè)圖片
import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.ImageView; /** * Android中圖片操作(拍照,相冊(cè)圖片選擇及圖片裁剪) * 作者:ldm * 時(shí)間:20162016/7/11 09:09 */ public class ImageTestActivity extends Activity implements View.OnClickListener { //拍照 private Button take_photo; //從相冊(cè)中選擇圖片 private Button local_pic; //圖片展示 private ImageView upload_image; //定義操作常量 private final static int TAKE_PHOTO_REQUEST = 1; private final static int LOCAL_PICS_REQUEST = 2; private final static int UPLOAD_PIC_REQUEST = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_test); //初始化控件及監(jiān)聽(tīng)事件 initViews(); } private void initViews() { this.upload_image = (ImageView) findViewById(R.id.upload_image); this.take_photo = (Button) findViewById(R.id.take_photo); this.local_pic = (Button) findViewById(R.id.local_pics); this.take_photo.setOnClickListener(this); this.local_pic.setOnClickListener(this); } @Override public void onClick(View view) { if (view.getId() == R.id.take_photo) {//拍照 //調(diào)用系統(tǒng)拍照In Intent photoIn = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(photoIn, TAKE_PHOTO_REQUEST); } else if (view.getId() == R.id.local_pics) {//從相冊(cè)選擇 Intent picsIn = new Intent(Intent.ACTION_GET_CONTENT); picsIn.setType("image/*");//設(shè)置選擇的數(shù)據(jù)類(lèi)型為圖片類(lèi)型 startActivityForResult(picsIn, LOCAL_PICS_REQUEST); } } //拍照或選擇相冊(cè)后,數(shù)據(jù)在這里處理 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (null == data) { return; } switch (requestCode) { case TAKE_PHOTO_REQUEST: Bundle bundle = data.getExtras();//獲取到圖片數(shù)據(jù) if (null != bundle) { Bitmap bm = bundle.getParcelable("data"); //把圖片展示在ImView上 // upload_image.setImageBitmap(bm); //對(duì)圖片剪 Uri uri = ImageUtils.saveBitmapToSdCard(bm); startImageCrop(uri); } break; case LOCAL_PICS_REQUEST: Uri uri = data.getData();//從圖片的Uri是以cotent://格式開(kāi)頭的 //獲取到圖片 Bitmap bm = ImageUtils.uri2Bitmap(ImageTestActivity.this, uri); //把圖片展示在ImView上 // upload_image.setImageBitmap(bm); //把拍照的圖片保存到本地并轉(zhuǎn)換成文件格式的Uri Uri fileUri = ImageUtils.saveBitmapToSdCard(bm); //對(duì)圖片剪 startImageCrop(fileUri); break; case UPLOAD_PIC_REQUEST: //把裁剪后的圖片展示出來(lái) Bundle b = data.getExtras(); Bitmap bitmap = b.getParcelable("data"); //圖片展示出來(lái) upload_image.setImageBitmap(bitmap); break; } } /** * @param * @description 圖片裁剪 * @author ldm * @time 2016/7/11 10:07 */ private void startImageCrop(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*");//設(shè)置Uri及類(lèi)型 intent.putExtra("crop", "true");// intent.putExtra("aspectX", 2);//X方向上的比例 intent.putExtra("aspectY", 1);//Y方向上的比例 intent.putExtra("outputX", 200);//裁剪區(qū)的X方向?qū)? intent.putExtra("outputY", 100);//裁剪區(qū)的Y方向?qū)? intent.putExtra("scale", true);//是否保留比例 intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString()); intent.putExtra("return-data", true);//是否將數(shù)據(jù)保留在Bitmap中返回dataParcelable相應(yīng)的Bitmap數(shù)據(jù) startActivityForResult(intent, UPLOAD_PIC_REQUEST); } }
布局文件
<?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"> <Button android:id="@+id/take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="拍照上傳" /> <Button android:id="@+id/local_pics" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="本地圖庫(kù)上傳" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="圖片信息展示" android:layout_marginLeft="10dp" android:textSize="16sp"/> <ImageView android:id="@+id/upload_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical"/> </LinearLayout>
圖片操作工具類(lèi)
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.text.format.DateFormat; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Calendar; import java.util.Locale; /** * description:從圖片中獲取到的Uri是以content://開(kāi)頭的,從U中找到對(duì)應(yīng)圖片 * 作者:ldm * 間:20162016/7/11 09:47 */ public class ImageUtils { public static Bitmap uri2Bitmap(Context mContext, Uri uri) { InputStream in = null; try { in = mContext.getContentResolver().openInputStream(uri); //從輸入流中獲取到圖片 Bitmap bm = BitmapFactory.decodeStream(in); in.close(); return bm; } catch (Exception e) { e.printStackTrace(); } return null; } /** * @param * @description 保存圖片到手機(jī)SD卡, 并返回圖片對(duì)應(yīng)的文件i * @author ldm * @time 2016/7/11 9:55 */ public static Uri saveBitmapToSdCard(Bitmap bm) { //自定義圖片名稱(chēng) String name = DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".png"; //定義圖片存放的位置 File tempFile = new File("/sdcard/Image/"); if (!tempFile.exists()) { tempFile.mkdir(); } String fileName = "/sdcard/Image/" + name; File pic = new File(fileName); try { FileOutputStream os = new FileOutputStream(pic); //對(duì)圖片進(jìn)行壓縮 bm.compress(Bitmap.CompressFormat.PNG, 100, os); os.flush(); os.close(); return Uri.fromFile(pic); } catch (Exception e) { e.printStackTrace(); } return null; } }
最后不要忘記在AndroidManifest.xml中添加 相應(yīng)權(quán)限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
注:更多關(guān)于Android權(quán)限控制的說(shuō)明可點(diǎn)擊此處查看Android權(quán)限操作說(shuō)明
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android圖形與圖像處理技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android 選擇相冊(cè)照片并返回功能的實(shí)現(xiàn)代碼
- Android開(kāi)發(fā)從相冊(cè)中選取照片的示例代碼
- Android通過(guò)手機(jī)拍照或從本地相冊(cè)選取圖片設(shè)置頭像
- Android 實(shí)現(xiàn)IOS選擇拍照相冊(cè)底部彈出的實(shí)例
- android相冊(cè)選擇圖片的編碼實(shí)現(xiàn)代碼
- Android7.0實(shí)現(xiàn)拍照和相冊(cè)選取圖片功能
- Android實(shí)現(xiàn)拍照、選擇相冊(cè)圖片并裁剪功能
- Android工具類(lèi)ImgUtil選擇相機(jī)和系統(tǒng)相冊(cè)
- Android開(kāi)發(fā)實(shí)現(xiàn)從相冊(cè)中選擇照片功能詳解
相關(guān)文章
Android中recyclerView底部添加透明漸變效果
這篇文章主要給大家介紹了關(guān)于Android中recyclerView如何實(shí)現(xiàn)底部添加透明漸變效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2018-04-04android基于SwipeRefreshLayout實(shí)現(xiàn)類(lèi)QQ的側(cè)滑刪除
本篇文章主要介紹了android基于SwipeRefreshLayout實(shí)現(xiàn)類(lèi)QQ的側(cè)滑刪除,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10android鬧鈴簡(jiǎn)單實(shí)現(xiàn)
本文給大家分享的是一段簡(jiǎn)單的實(shí)現(xiàn)Android系統(tǒng)的鬧鈴的代碼,非常實(shí)用,想做Android開(kāi)發(fā)的小伙伴們可以參考下。2015-03-03Android使用Walle實(shí)現(xiàn)多渠道打包功能的實(shí)現(xiàn)示例
這篇文章主要介紹了Android使用Walle實(shí)現(xiàn)多渠道打包功能的實(shí)現(xiàn)示例,幫助大家更好的理解和學(xué)習(xí)使用Android開(kāi)發(fā),感興趣的朋友可以了解下2021-04-04Android實(shí)現(xiàn)帶列表的地圖POI周邊搜索功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶列表的地圖POI周邊搜索功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Android仿今日頭條多個(gè)fragment懶加載的實(shí)現(xiàn)
我們?cè)谧鰬?yīng)用開(kāi)發(fā)的時(shí)候,一個(gè)Activity里面可能會(huì)以viewpager(或其他容器)與多個(gè)Fragment來(lái)組合使用,下面這篇文章主要給大家介紹了關(guān)于利用Android仿今日頭條多個(gè)fragment懶加載的相關(guān)資料,需要的朋友可以參考下。2017-12-12Android開(kāi)發(fā) Activity和Fragment詳解
本文主要介紹Android開(kāi)發(fā) Activity和Fragment,這里對(duì)Activity和Fragment的知識(shí)做了詳細(xì)講解,并附簡(jiǎn)單代碼示例,有興趣的小伙伴可以參考下2016-08-08Kotlin作用域函數(shù)應(yīng)用詳細(xì)介紹
作用域函數(shù):是Kotlin標(biāo)準(zhǔn)庫(kù)中的內(nèi)聯(lián)函數(shù),作用在對(duì)象上時(shí),執(zhí)行給定的block代碼塊??梢栽赽lock代碼塊中通過(guò)it,this代表當(dāng)前對(duì)象,進(jìn)行代碼邏輯處理2022-08-08