Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android拍照、相冊 獲取圖片後,裁剪圖片

Android拍照、相冊 獲取圖片後,裁剪圖片

編輯:關於Android編程

最近在做的B2B的項目,圖片大部分來源於用戶自己上傳;
由於android尺寸的不一,用戶相機,相冊的圖片也是奇形怪狀;
所以在上傳之前對圖片做一次裁剪是很有必要的!

下面是按比例裁剪圖片的demo

資源文件activity_main.xml

<code class=" hljs xml"><relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.croppicturedemo.MainActivity">
    <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
         </linearlayout></relativelayout></code><button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="選擇圖片"><code class=" hljs xml">
        <imageview android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="match_parent">

    

</imageview></code></button>

MainActivity

public class MainActivity extends Activity implements OnClickListener {
        private ImageView imageview;
        private Button btn;
        private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
        private static final int PHOTO_REQUEST_GALLERY = 2;// 從相冊中選擇
        private static final int PHOTO_REQUEST_CUT = 3;// 結果
        // 創建一個以當前時間為名稱的文件
        File tempFile = new File(Environment.getExternalStorageDirectory(),getPhotoFileName());

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            init();
        }

        //初始化控件
        private void init() {
            imageview = (ImageView) findViewById(R.id.imageview);
            btn = (Button) findViewById(R.id.btn);

            //為ImageButton和Button添加監聽事件
            imageview.setOnClickListener(this);
            btn.setOnClickListener(this);
        }

        //點擊事件
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.imageview:
                showDialog();
                break;

            case R.id.btn:
                showDialog();
                break;
            }
        }


        //提示對話框方法
        private void showDialog() {
            new AlertDialog.Builder(this)
                    .setTitle("頭像設置")
                    .setPositiveButton("拍照", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                            // 調用系統的拍照功能
                            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            // 指定調用相機拍照後照片的儲存路徑
                            intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(tempFile));
                            startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
                        }
                    })
                    .setNegativeButton("相冊", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                            Intent intent = new Intent(Intent.ACTION_PICK, null);
                            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
                            startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
                        }
                    }).show();
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub

            switch (requestCode) {
            case PHOTO_REQUEST_TAKEPHOTO:
                startPhotoZoom(Uri.fromFile(tempFile), 150);
                break;

            case PHOTO_REQUEST_GALLERY:
                if (data != null)
                    startPhotoZoom(data.getData(), 150);
                break;

            case PHOTO_REQUEST_CUT:
                if (data != null) 
                    setPicToView(data);
                break;
            }
            super.onActivityResult(requestCode, resultCode, data);

        }

        private void startPhotoZoom(Uri uri, int size) {
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(uri, "image/*");
            // crop為true是設置在開啟的intent中設置顯示的view可以剪裁
            intent.putExtra("crop", "true");

            // aspectX aspectY 是寬高的比例
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);

            // outputX,outputY 是剪裁圖片的寬高
            intent.putExtra("outputX", size);
            intent.putExtra("outputY", size);
            intent.putExtra("scale", true);  
            intent.putExtra("return-data", true);

            startActivityForResult(intent, PHOTO_REQUEST_CUT);
        }

        //將進行剪裁後的圖片顯示到UI界面上
        private void setPicToView(Intent picdata) {
            Bundle bundle = picdata.getExtras();
            if (bundle != null) {
                Bitmap photo = bundle.getParcelable("data");
                Drawable drawable = new BitmapDrawable(photo);
                imageview.setBackgroundDrawable(drawable);
            }
        }

        // 使用系統當前日期加以調整作為照片的名稱
        private String getPhotoFileName() {
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");
            return dateFormat.format(date) + ".jpg";
        }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved