Android 拍照+图片 裁剪 功能的实现_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Android 拍照+图片 裁剪 功能的实现

Android 拍照+图片 裁剪 功能的实现

 2014/5/28 19:06:16  兵库合力  博客园  我要评论(0)
  • 摘要:接到一个小需求,要求实现类似截取头像的图片裁剪的功能!时间要求很紧,所以就上看了几个小Demo。虽然原理不难,但是真正好用的没有几个。在一堆烂苹果里找到一个最不烂的。然后又自己修改了一下,达到了预期需求实现!部局部分:1<?xmlversion="1.0"encoding="utf-8"?>2<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"3android
  • 标签:android 功能 实现 图片

  接到一个小需求,要求实现类似截取头像的图片 裁剪 的功能!时间要求很紧,所以就上看了几个小Demo。虽然原理不难,但是真正好用的没有几个。在一堆烂苹果里找到一个最不烂的。然后又自己修改了一下,达到了预期需求实现!

 

   部局部分:

class="code_img_closed" src="/Upload/Images/2014052819/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('57f11a86-54af-48cc-b3f2-b08e002a4492',event)" src="/Upload/Images/2014052819/2B1B950FA3DF188F.gif" alt="" />
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <Button
 8         android:id="@+id/selectImageBtn"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:text="选择图片" />
12 
13     <ImageView
14         android:id="@+id/imageView"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content" />
17 
18 </LinearLayout>
View Code

  

  Activity部分:

  1 import android.app.Activity;
  2 import android.app.AlertDialog;
  3 import android.content.DialogInterface;
  4 import android.content.Intent;
  5 import android.graphics.Bitmap;
  6 import android.graphics.BitmapFactory;
  7 import android.net.Uri;
  8 import android.os.Bundle;
  9 import android.os.Environment;
 10 import android.provider.MediaStore;
 11 import android.view.View;
 12 import android.widget.Button;
 13 import android.widget.ImageView;
 14 
 15 public class DoPhotoActivity extends Activity implements
 16         View.OnClickListener {
 17 
 18     /** Called when the activity is first created. */
 19 
 20     private Button selectImageBtn;
 21 
 22     private ImageView imageView;
 23 
 24     private File sdcardTempFile;
 25 
 26     private AlertDialog dialog;
 27 
 28     private int crop = 128;
 29 
 30     private static final int PHOTO_PICKED_WITH_DATA = 3021;
 31 
 32     private static final int PHOTO_CAMERA_PICKED_WITH_DATA = 3022;
 33 
 34     private static final int CAMERA_WITH_DATA = 3023;
 35 
 36     @Override
 37     public void onCreate(Bundle savedInstanceState) {
 38 
 39         super.onCreate(savedInstanceState);
 40 
 41         setContentView(R.layout.do_photo);
 42 
 43         selectImageBtn = (Button) findViewById(R.id.selectImageBtn);
 44 
 45         imageView = (ImageView) findViewById(R.id.imageView);
 46 
 47         selectImageBtn.setOnClickListener(this);
 48 
 49         sdcardTempFile = new File(Environment.getExternalStorageDirectory()
 50                                 + "/MYIMAGE/" + String.valueOf(System.currentTimeMillis())
 51                                 + ".jpg");
 52     }
 53 
 54     public void ImageScale() {
 55 
 56         Intent intent = new Intent("android.intent.action.PICK");
 57         intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI,
 58                 "image/*");
 59         intent.putExtra("output", Uri.fromFile(sdcardTempFile));
 60         intent.putExtra("crop", "true");
 61         intent.putExtra("aspectX", 1);// 裁剪框比例
 62         intent.putExtra("aspectY", 1);
 63         intent.putExtra("outputX", crop);// 输出图片大小
 64         intent.putExtra("outputY", crop);
 65         startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
 66     }
 67 
 68     public void photo() {
 69 
 70         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 71         startActivityForResult(intent, CAMERA_WITH_DATA);
 72     }
 73 
 74     protected void doCropPhoto(Bitmap data) {
 75 
 76         Intent intent = getCropImageIntent(data);
 77         startActivityForResult(intent, PHOTO_CAMERA_PICKED_WITH_DATA);
 78     }
 79 
 80     public Intent getCropImageIntent(Bitmap data) {
 81         Intent intent = new Intent("com.android.camera.action.CROP");
 82         intent.setType("image/*");
 83         intent.putExtra("data", data);
 84         intent.putExtra("crop", "true");
 85         intent.putExtra("aspectX", 1);
 86         intent.putExtra("aspectY", 1);
 87         intent.putExtra("outputX", crop);
 88         intent.putExtra("outputY", crop);
 89         intent.putExtra("return-data", true);
 90         return intent;
 91     }
 92 
 93     @Override
 94     public void onClick(View v) {
 95 
 96         if (v == selectImageBtn) {
 97 
 98             if (dialog == null) {
 99 
100                 dialog = new AlertDialog.Builder(this).setItems(
101                         new String[] { "相机", "相册" },
102                         new DialogInterface.OnClickListener() {
103 
104                             @Override
105                             public void onClick(DialogInterface dialog,
106                                     int which) {
107 
108                                 if (which == 0) {
109 
110                                     photo();
111 
112                                 } else {
113                                     ImageScale();
114                                 }
115 
116                             }
117 
118                         }).create();
119 
120             }
121 
122             if (!dialog.isShowing()) {
123 
124                 dialog.show();
125 
126             }
127 
128         }
129 
130     }
131 
132     @Override
133     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
134 
135         if (resultCode == RESULT_OK) {
136 
137             switch (requestCode) {
138 
139             case CAMERA_WITH_DATA:
140 
141                 Bitmap camera_photo = data.getParcelableExtra("data");
142                 doCropPhoto(camera_photo);
143                 
144 
145             case PHOTO_PICKED_WITH_DATA:
146 
147                 Bitmap picked_photo = BitmapFactory.decodeFile(sdcardTempFile.getAbsolutePath());
148                 imageView.setImageBitmap(picked_photo);
149                 break;
150 
151             case PHOTO_CAMERA_PICKED_WITH_DATA:
152 
153                 Bitmap camera_picked_photo = data.getParcelableExtra("data");
154                 imageView.setImageBitmap(camera_picked_photo);
155                 break;
156 
157             }
158 
159         }
160 
161     }
162 }
View Code

 

 

发表评论
用户名: 匿名