編輯:關於 Android 開發
本文示範如何在 Android 中透過拍照或從相冊選圖,再進行裁剪,最後取得裁剪後的圖片。核心涉及 ACTION_IMAGE_CAPTURE、ACTION_PICK 與自定義的 CROP 意圖。
<LinearLayout xmlns:android="https://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:text="Take Photo" /> <Button android:id="@+id/get_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="get Photo" /> <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> </LinearLayout>
package com.example.choosepictest; import java.io.File; import java.io.IOException; 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 MainActivity extends Activity implements OnClickListener { public static final int TAKE_PHOTO = 1; public static final int CROP_PHOTO = 2; public static final int GET_PHOTO = 3; private Button takePhoto; private Button getPhoto; private ImageView picture; private Uri headImgUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); takePhoto = (Button) findViewById(R.id.take_photo); getPhoto = (Button) findViewById(R.id.get_photo); picture = (ImageView) findViewById(R.id.picture); takePhoto.setOnClickListener(this); getPhoto.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.take_photo: takePhoto(); break; case R.id.get_photo: getPhoto(); break; default: break; } } // 拍照 private void takePhoto() { File appDir = new File(Environment.getExternalStorageDirectory(), "/etoury/picCache"); if (!appDir.exists()) { appDir.mkdir(); } String fileName = "user_head" + ".jpg"; File outputImage = new File(appDir, fileName); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } headImgUri = Uri.fromFile(outputImage); Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, headImgUri); startActivityForResult(intent, TAKE_PHOTO); } // 定向到圖片庫 private void getPhoto() { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, GET_PHOTO); } /** * 裁剪 */ private void crop(Uri uri) { // 裁剪圖片意圖 Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 下面這個crop=true是設置在開啟的Intent中設置顯示的VIEW可裁剪 intent.putExtra("crop", "true"); intent.putExtra("scale", true);// 去黑邊 // 裁剪框的比例,1:1 intent.putExtra("aspectX", 1);// 輸出是X方向的比例 intent.putExtra("aspectY", 1); // 裁剪後輸出圖片的尺寸大小,不能太大500程序崩潰 intent.putExtra("outputX", 256); intent.putExtra("outputY", 256); // 圖片格式 /* intent.putExtra("outputFormat", "JPEG"); */ intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); // intent.putExtra("noFaceDetection", true);// 取消人臉識別 intent.putExtra("return-data", true);// true:返回uri,false:不返回uri // 同一個地址下 裁剪的圖片覆蓋拍照的圖片 intent.putExtra(MediaStore.EXTRA_OUTPUT, headImgUri); startActivityForResult(intent, CROP_PHOTO); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case GET_PHOTO: if (resultCode == RESULT_OK) { crop(data.getData()); } break; case TAKE_PHOTO: if (resultCode == RESULT_OK) { crop(headImgUri); } break; case CROP_PHOTO: if (resultCode == RESULT_OK) { Bitmap cropbitmap = data.getParcelableExtra("data"); picture.setImageBitmap(cropbitmap); } break; default: break; } } }
總結:
1. 拍照會返回一張圖片,可以是全尺寸的圖片。
2. 拍照返回圖片的地址,其實就是某個目錄下的一個檔案。
3. 裁剪後圖片的地址,會覆蓋掉全尺寸圖片的地址。
4. 相冊 intent 返回的是一個 uri,而非 string。
5. 裁剪後的圖片,不能覆蓋相冊返回的 uri(這一點務必注意)。
Android 圖片刮開效果實作:用 setPixel 做像素級擦除
本文實作一個「刮刮樂」風格的互動效果:畫面上疊著兩張圖,用手指滑過上層圖片時,滑過的區域會變成透明,逐漸顯露出底下那張圖。這類效果常用於優惠券刮刮區、線索揭露小遊戲,或任
Android 網路開發:實作一個網頁原始碼查看器(HttpURLConnection + Handler)
Android 網路開發:實作一個網頁原始碼查看器(HttpURLConnection + Handler) 本文示範如何打造一個簡易的網頁原始碼查看器:使用者輸入網址後
Android 開發環境配置:Eclipse 關聯源碼與源碼/JAR 查看工具
想更深入理解 Android 底層運作,關聯系統源碼、選對查看工具是必備技能。本文介紹三項實用配置。 1、Eclipse 關聯源碼 步驟一:自行下載對應版本的源碼,本文以
使用 adb pull / push 在手機與電腦間傳輸檔案與資料庫
使用 adb pull / push 在手機與電腦間傳輸檔案與資料庫 先來談談 adb 命令的環境設定。如果你遇到「adb 不是內部或外部命令」的提示,通常是因為尚未設定