拍照+相册选择图片+剪裁图片完整实现
2014-05-28 17:24
309 查看
今天做项目的时候要用到图片选择,要实现拍照获取图片和从相册获取图片,并且要求在获取完之后可以裁剪,试了很多方法之后终于搞定了
我把这个功能模块摘出来一个小demo
上代码
我把这个功能模块摘出来一个小demo
上代码
package com.example.sddemo; import java.io.File; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class CramaActivity extends Activity { private Button creama=null; private ImageView img=null; private TextView text=null; private File tempFile = new File(Environment.getExternalStorageDirectory(), getPhotoFileName()); private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照 private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 private static final int PHOTO_REQUEST_CUT = 3;// 结果 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_crama); init(); } private void init() { // TODO Auto-generated method stub creama=(Button) findViewById(R.id.btn_creama); img=(ImageView) findViewById(R.id.img_creama); creama.setOnClickListener(listener); text=(TextView) findViewById(R.id.text); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case PHOTO_REQUEST_TAKEPHOTO:// 当选择拍照时调用 startPhotoZoom(Uri.fromFile(tempFile)); break; case PHOTO_REQUEST_GALLERY:// 当选择从本地获取图片时 // 做非空判断,当我们觉得不满意想重新剪裁的时候便不会报异常,下同 if (data != null) startPhotoZoom(data.getData()); break; case PHOTO_REQUEST_CUT:// 返回的结果 if (data != null) // setPicToView(data); sentPicToNext(data); break; } super.onActivityResult(requestCode, resultCode, data); } private OnClickListener listener=new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 指定调用相机拍照后照片的储存路径 cameraintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile)); startActivityForResult(cameraintent, PHOTO_REQUEST_TAKEPHOTO); }}; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_crama, menu); return true; } private void startPhotoZoom(Uri uri) { 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", 300); intent.putExtra("outputY", 300); intent.putExtra("return-data", true); intent.putExtra("noFaceDetection", true); startActivityForResult(intent, PHOTO_REQUEST_CUT); } // 将进行剪裁后的图片传递到下一个界面上 private void sentPicToNext(Intent picdata) { Bundle bundle = picdata.getExtras(); if (bundle != null) { Bitmap photo = bundle.getParcelable("data"); if (photo==null) { img.setImageResource(R.drawable.get_user_photo); }else { img.setImageBitmap(photo); // 设置文本内容为 图片绝对路径和名字 text.setText(tempFile.getAbsolutePath()); } ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] photodata = baos.toByteArray(); System.out.println(photodata.toString()); // Intent intent = new Intent(); // intent.setClass(RegisterActivity.this, ShowActivity.class); // intent.putExtra("photo", photodata); // startActivity(intent); // finish(); } catch (Exception e) { e.getStackTrace(); } finally { if (baos != null) { try { baos.close(); } catch (Exception e) { e.printStackTrace(); } } } } } // 使用系统当前日期加以调整作为照片的名称 private String getPhotoFileName() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat( "'IMG'_yyyyMMdd_HHmmss"); return dateFormat.format(date) + ".jpg"; } }
<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=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="38dp" android:layout_marginTop="46dp" android:text="Button" /> <ImageView android:id="@+id/imageView" android:layout_width="300dp" android:layout_height="300dp" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" /> </RelativeLayout>这就解决了
相关文章推荐
- [Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现
- Android中拍照和从相册选择图片,并对选择的图片进行剪裁的例子
- android完美实现 拍照 选择图片 剪裁等代码分享
- Android中拍照和从相册选择图片,并剪裁(二)
- android完美实现 拍照 选择图片 剪裁等代码分享
- Android实现拍照,以及从相册选择图片裁剪功能同时保存在本地
- 图片拍照或相册选择的实现以及裁剪功能
- android 拍照和相册选择照片之后。剪裁图片。
- Android实现拍照、选择相册图片并裁剪功能
- UIImagePickerControllerDelegate 实现选择相册图片和拍照得到图片
- Android中拍照和从相册选择图片,并剪裁(二)
- Android 相册选择照片或拍照获取图片系统6.0及7.0实现
- 使用南尘的ImagePicker实现仿微信的相册图片选择以及拍照上传
- Android实现选择相册图片和拍照,并手动截图返回图片路径和Bitmap对象
- Android 调用相册 拍照 实现系统控件缩放 切割图片
- Android中从SD卡中/拍照选择图片并进行剪裁的方法
- Android 调用相册 拍照 实现系统控件缩放 切割图片
- Android中从SD卡中/拍照选择图片并进行剪裁的方法
- Android调用相机实现拍照并裁剪图片,调用手机中的相册图片并裁剪图片
- 图片相关--相册选择、拍照、缩放、裁减