Android編程實現(xiàn)圖片拍照剪裁的方法
本文實例講述了Android實現(xiàn)圖片拍照剪裁的方法。分享給大家供大家參考,具體如下:
調(diào)用系統(tǒng)的裁剪工具對相冊或者拍照的圖片進行裁剪。
startActivityforResult用的很恰當(dāng),一些系統(tǒng)action需要注意。
package com.photosizing; import java.io.ByteArrayOutputStream; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class testActivity extends Activity { public static final int NONE = 0; public static final int PHOTOHRAPH = 1;// 拍照 public static final int PHOTOZOOM = 2; // 縮放 public static final int PHOTORESOULT = 3;// 結(jié)果 public static final String IMAGE_UNSPECIFIED = "image/*"; ImageView imageView = null; Button button0 = null; Button button1 = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView) findViewById(R.id.imageID); button0 = (Button) findViewById(R.id.btn_01); button1 = (Button) findViewById(R.id.btn_02); button0.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); startActivityForResult(intent, PHOTOZOOM); } }); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File( Environment.getExternalStorageDirectory(), "temp.jpg"))); startActivityForResult(intent, PHOTOHRAPH); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == NONE) return; if (data == null) return; // 拍照 if (requestCode == PHOTOHRAPH) { // 設(shè)置文件保存路徑這里放在跟目錄下 File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg"); startPhotoZoom(Uri.fromFile(picture)); } // 讀取相冊縮放圖片 if (requestCode == PHOTOZOOM) { startPhotoZoom(data.getData()); } // 處理結(jié)果 if (requestCode == PHOTORESOULT) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 75, stream); // (0 - 100)壓縮文件 imageView.setImageBitmap(photo); } } super.onActivityResult(requestCode, resultCode, data); } public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, IMAGE_UNSPECIFIED); intent.putExtra("crop", "true"); // aspectX aspectY 是寬高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪圖片寬高 intent.putExtra("outputX", 64); intent.putExtra("outputY", 64); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTORESOULT); } }
XML文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ImageView android:id="@+id/imageID" android:adjustViewBounds="true" android:maxWidth="50dip" android:maxHeight="50dip" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_01" android:layout_height="50dip" android:text="相冊" android:layout_width="150dip"/> <Button android:id="@+id/btn_02" android:layout_height="50dip" android:text="拍照" android:layout_width="150dip"/> </LinearLayout>
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android啟動相機拍照并返回圖片
- Android實現(xiàn)拍照、選擇圖片并裁剪圖片功能
- Android仿微信發(fā)表說說實現(xiàn)拍照、多圖上傳功能
- android 拍照和上傳的實現(xiàn)代碼
- Android拍照得到全尺寸圖片并進行壓縮
- android系統(tǒng)在靜音模式下關(guān)閉camera拍照聲音的方法
- Android手機拍照或選取圖庫圖片作為頭像
- Android中使用Camera類編寫手機拍照App的實例教程
- Android拍照或從圖庫選擇圖片并裁剪
- Android拍照裁剪圖片
- Android仿微信選擇圖片和拍照功能
- Android編程調(diào)用系統(tǒng)自帶的拍照功能并返回JPG文件示例【附demo源碼下載】
相關(guān)文章
Android應(yīng)用創(chuàng)建桌面快捷方式代碼
這篇文章主要為大家詳細(xì)介紹了Android應(yīng)用創(chuàng)建桌面快捷方式代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09android如何添加桌面圖標(biāo)和卸載程序后自動刪除圖標(biāo)
android如何添加桌面圖標(biāo)和卸載程序后自動刪除桌面圖標(biāo),這是一個應(yīng)用的安裝與卸載過程對桌面圖標(biāo)的操作,下面與大家分享下具體是如何實現(xiàn)的,感興趣的朋友可以參考下哈2013-06-06android 中使用TableLayout實現(xiàn)表單布局效果示例
本篇文章主要介紹了android 中使用TableLayout實現(xiàn)表單布局效果示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06Android開發(fā)之自定義view實現(xiàn)通訊錄列表A~Z字母提示效果【附demo源碼下載】
這篇文章主要介紹了Android開發(fā)之自定義view實現(xiàn)通訊錄列表A~Z字母提示效果,結(jié)合完整實例形式分析了Android獲取通訊錄列表及采用自定義view排列顯示的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07Android自定義TextView實現(xiàn)drawableLeft內(nèi)容居中
這篇文章主要介紹了Android自定義TextView實現(xiàn)drawableLeft內(nèi)容居中的相關(guān)資料,需要的朋友可以參考下2017-06-06Android App中實現(xiàn)簡單的刮刮卡抽獎效果的實例詳解
這篇文章主要介紹了Android App中實現(xiàn)簡單的刮刮卡抽獎效果的實例詳解,文中主要借助Bitmap的canvas.drawPath的api來實現(xiàn),需要的朋友可以參考下2016-03-03用于cocos2d-x引擎(ndk)中為android項目生成編譯文件列表
在android的ndk項目中,添加很多源文件之后總要手動編寫makefile來添加所有的源文件, 很麻煩,所以寫了一個自動生成編譯源文件列表的小工具2014-05-05android使用ExpandableListView控件實現(xiàn)小說目錄效果的例子
這篇文章主要介紹了android使用ExpandableListView控件實現(xiàn)小說目錄效果的例子,還可以實現(xiàn)二級列表展示效果,需要的朋友可以參考下2014-07-07