天天看點

Android 拍照圖檔選取與圖檔剪裁

  最近從以前的項目中扒下來一個常用的子產品,在這裡有必要記錄一下的,就是android上擷取圖檔以及裁剪圖檔,怎麼樣?這個功能是不是很常用啊,你随便打開一個App,隻要它有注冊功能都會有設定人物頭像的功能,尤其在内容型的app中更為常見,那麼這些功能是怎麼實作的呢?今天,在這裡就記錄一下好了,防止以後的項目中也會用到,就直接拿來用好了。

1.通過拍照或者圖冊擷取圖檔(不需要剪裁)

        這種擷取圖檔的方式就比較次了,因為不設定圖檔的剪裁功能,有可能因為圖檔過大,導緻OOM,但是這種方式也是有必要講一下的,其擷取圖檔的方式有兩種,一是調用系統相機實時拍攝一張圖檔,二十打開裝置上已有的圖庫,在圖庫中選擇一張照片。這兩種方式實作方法都是一個道理,無非就是通過Intent調用系統的東西。下面是源碼,首先是圖檔選擇方式的Activity,這個Activity被設定成了Dialog模式,需要進行設定一下。

布局檔案/res/layout/activity_select_photo.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal" >

    <LinearLayout
        android:id="@+id/dialog_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/select_photo_up_bg"
            android:orientation="vertical"
            android:paddingBottom="5dp"
            android:paddingTop="5dp" >

            <Button
                android:id="@+id/btn_take_photo"
                android:layout_width="fill_parent"
                android:layout_height="35dp"
                android:background="@drawable/select_photo_bg"
                android:text="拍照選取"
                android:textStyle="bold" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="0.5px"
                android:background="#828282" />

            <Button
                android:id="@+id/btn_pick_photo"
                android:layout_width="fill_parent"
                android:layout_height="35dp"
                android:layout_marginTop="0dip"
                android:background="@drawable/select_photo_bg"
                android:text="相冊選取"
                android:textStyle="bold" />
        </LinearLayout>

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="fill_parent"
            android:layout_height="35dp"
            android:layout_marginTop="20dip"
            android:background="@drawable/select_photo_bg"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:text="取消"
            android:textColor="#ffff0000"
            android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>      

接着是擷取圖檔Activity裡的代碼SelectPhotoActivity:

public class SelectPhotoActivity extends Activity implements OnClickListener {
    /** 使用照相機拍照擷取圖檔 */
    public static final int SELECT_PIC_BY_TACK_PHOTO = 1;
    /** 使用相冊中的圖檔 */
    public static final int SELECT_PIC_BY_PICK_PHOTO = 2;
    /** 開啟相機 */
    private Button btn_take_photo;
    /** 開啟圖冊 */
    private Button btn_pick_photo;
    /** 取消 */
    private Button btn_cancel;
    /** 擷取到的圖檔路徑 */
    private String picPath;
    private Intent lastIntent;
    private Uri photoUri;
    /** 從Intent擷取圖檔路徑的KEY */
    public static final String KEY_PHOTO_PATH = "photo_path";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_photo);
        btn_take_photo = (Button) findViewById(R.id.btn_take_photo);
        btn_pick_photo = (Button) findViewById(R.id.btn_pick_photo);
        btn_cancel = (Button) findViewById(R.id.btn_cancel);

        lastIntent = getIntent();

        btn_take_photo.setOnClickListener(this);
        btn_pick_photo.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_take_photo : // 開啟相機
                takePhoto();
                break;
            case R.id.btn_pick_photo : // 開啟圖冊
                pickPhoto();
                break;
            case R.id.btn_cancel : // 取消操作
                this.finish();
                break;
            default :
                break;
        }
    }

    /**
     * 拍照擷取圖檔
     */
    private void takePhoto() {
        // 執行拍照前,應該先判斷SD卡是否存在
        String SDState = Environment.getExternalStorageState();
        if (SDState.equals(Environment.MEDIA_MOUNTED)) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"
            /***
             * 需要說明一下,以下操作使用照相機拍照,拍照後的圖檔會存放在相冊中的 這裡使用的這種方式有一個好處就是擷取的圖檔是拍照後的原圖
             * 如果不實用ContentValues存放照片路徑的話,拍照後擷取的圖檔為縮略圖不清晰
             */
            ContentValues values = new ContentValues();
            photoUri = this.getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
            startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
        } else {
            Toast.makeText(getApplicationContext(), "記憶體卡不存在",
                    Toast.LENGTH_SHORT).show();
        }
    }

    /***
     * 從相冊中取圖檔
     */
    private void pickPhoto() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        finish();
        return super.onTouchEvent(event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            doPhoto(requestCode, data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 選擇圖檔後,擷取圖檔的路徑
     * 
     * @param requestCode
     * @param data
     */
    private void doPhoto(int requestCode, Intent data) {
        if (requestCode == SELECT_PIC_BY_PICK_PHOTO) {// 從相冊取圖檔,有些手機有異常情況,請注意
            if (data == null) {
                Toast.makeText(getApplicationContext(), "選擇圖檔檔案出錯",
                        Toast.LENGTH_SHORT).show();
                return;
            }
            photoUri = data.getData();
            if (photoUri == null) {
                Toast.makeText(getApplicationContext(), "選擇圖檔檔案出錯",
                        Toast.LENGTH_SHORT).show();
                return;
            }
        }
        String[] pojo = {MediaStore.Images.Media.DATA};
        Cursor cursor = managedQuery(photoUri, pojo, null, null, null);
        if (cursor != null) {
            int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
            cursor.moveToFirst();
            picPath = cursor.getString(columnIndex);
            cursor.close();
        }
        if (picPath != null
                && (picPath.endsWith(".png") || picPath.endsWith(".PNG")
                        || picPath.endsWith(".jpg") || picPath.endsWith(".JPG"))) {
            lastIntent.putExtra(KEY_PHOTO_PATH, picPath);
            setResult(Activity.RESULT_OK, lastIntent);
            finish();
        } else {
            Toast.makeText(getApplicationContext(), "選擇圖檔檔案不正确",
                    Toast.LENGTH_SHORT).show();
        }
    }

}      

因為這Activity是要設定成Dialog模式的,是以需要在清單檔案中設定一下style,/res/values/styles.xml裡添加如下:

<!-- 選取照片的Activity的樣式風格,采取對話框的風格 -->
    <style name="AnimBottom" parent="@android:style/Animation">
        <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
        <item name="android:windowExitAnimation">@anim/push_bottom_out</item>
    </style>

    <style name="DialogStyleBottom" parent="android:Theme.Dialog">
        <item name="android:windowAnimationStyle">@style/AnimBottom</item>
        <item name="android:windowFrame">@null</item>
        <!-- 邊框 -->
        <item name="android:windowIsFloating">false</item>
        <!-- 是否浮現在activity之上 -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- 半透明 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 無标題 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- 背景透明 -->
        <item name="android:backgroundDimEnabled">true</item>
        <!-- 模糊 -->
    </style>      

在Activity的節點下,設定這個style:

<activity
            android:name="com.example.croppictrue.SelectPhotoActivity"
            android:screenOrientation="portrait"
            android:theme="@style/DialogStyleBottom" >
        </activity>      

添權重限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />        

運作效果如下:

Android 拍照圖檔選取與圖檔剪裁
Android 拍照圖檔選取與圖檔剪裁

2.通過拍照或者圖冊擷取圖檔(需要剪裁)

      上面第一種方式擷取圖檔是沒有經過剪裁的,但是大多項目需求是需要剪裁圖檔後再使用,例如修改使用者頭像等等功能。那麼,下面,就奉上剪裁圖檔的代碼吧:

public class CropPictureActivity extends Activity {

    /** ImageView對象 */
    private ImageView iv_photo;
    private String[] items = new String[]{"選擇本地圖檔", "拍照"};
    /** 頭像名稱 */
    private static final String IMAGE_FILE_NAME = "image.jpg";

    /** 請求碼 */
    private static final int IMAGE_REQUEST_CODE = 0;
    private static final int CAMERA_REQUEST_CODE = 1;
    private static final int RESULT_REQUEST_CODE = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop);
        iv_photo = (ImageView) findViewById(R.id.iv_photo);
        iv_photo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
    }

    /**
     * 顯示選擇對話框
     */
    private void showDialog() {

        new AlertDialog.Builder(this)
                .setTitle("設定頭像")
                .setItems(items, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0 :
                                Intent intentFromGallery = new Intent();
                                intentFromGallery.setType("image/*"); // 設定檔案類型
                                intentFromGallery
                                        .setAction(Intent.ACTION_GET_CONTENT);
                                startActivityForResult(intentFromGallery,
                                        IMAGE_REQUEST_CODE);
                                break;
                            case 1 :
                                Intent intentFromCapture = new Intent(
                                        MediaStore.ACTION_IMAGE_CAPTURE);
                                // 判斷存儲卡是否可以用,可用進行存儲
                                String state = Environment
                                        .getExternalStorageState();
                                if (state.equals(Environment.MEDIA_MOUNTED)) {
                                    File path = Environment
                                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                                    File file = new File(path, IMAGE_FILE_NAME);
                                    intentFromCapture.putExtra(
                                            MediaStore.EXTRA_OUTPUT,
                                            Uri.fromFile(file));
                                }

                                startActivityForResult(intentFromCapture,
                                        CAMERA_REQUEST_CODE);
                                break;
                        }
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 結果碼不等于取消時候
        if (resultCode != RESULT_CANCELED) {
            switch (requestCode) {
                case IMAGE_REQUEST_CODE :
                    startPhotoZoom(data.getData());
                    break;
                case CAMERA_REQUEST_CODE :
                    // 判斷存儲卡是否可以用,可用進行存儲
                    String state = Environment.getExternalStorageState();
                    if (state.equals(Environment.MEDIA_MOUNTED)) {
                        File path = Environment
                                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                        File tempFile = new File(path, IMAGE_FILE_NAME);
                        startPhotoZoom(Uri.fromFile(tempFile));
                    } else {
                        Toast.makeText(getApplicationContext(),
                                "未找到存儲卡,無法存儲照片!", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case RESULT_REQUEST_CODE : // 圖檔縮放完成後
                    if (data != null) {
                        getImageToView(data);
                    }
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 裁剪圖檔方法實作
     * 
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 設定裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是寬高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪圖檔寬高
        intent.putExtra("outputX", 340);
        intent.putExtra("outputY", 340);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, RESULT_REQUEST_CODE);
    }

    /**
     * 儲存裁剪之後的圖檔資料
     * 
     * @param picdata
     */
    private void getImageToView(Intent data) {
        Bundle extras = data.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            Drawable drawable = new BitmapDrawable(this.getResources(), photo);
            iv_photo.setImageDrawable(drawable);
        }
    }
}      

效果圖:

Android 拍照圖檔選取與圖檔剪裁
Android 拍照圖檔選取與圖檔剪裁

        在這個Activity裡為了簡便處理,我沒有在選擇圖檔時候start一個Dialog風格的Activity了,就直接一個普通的對話框提示使用者選擇,效果也許。其實實作的原理都比較簡單,實作圖檔的剪裁就是發一個Intent請求,調用裝置上所有具有剪裁圖檔功能的app去剪裁圖檔,我的裝置上除了android系統自帶的圖庫以外,還裝有“快圖浏覽”這個app,這個app也自帶一個圖檔剪裁的功能,所有當選擇好圖檔後,會出現一個選擇提示,使用者可以根據提示選擇到底使用哪個app提供的剪裁功能區剪裁圖檔。

        以上代碼均在模拟器上測試過,由于模拟器對相機支援的不好,是以就沒有示範打開相機拍攝圖檔了,有興趣的朋友可以先請下載下傳這個Demo的源碼,運作在手機上試試看效果如何,如若疏漏之後,歡迎大家批評指正!

源碼請在這裡下載下傳