android 调用系统照相机拍照后剪裁
2015-10-09 09:06
676 查看
private static final int PHOTO_REQUEST_CAMERA = 1;// 拍照 private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 private static final int PHOTO_REQUEST_CUT = 3;// 结果 /* 头像名称 */ private static String PHOTO_FILE_NAME = "temp_photo.jpg";
private Bitmap bitmap; private File tempFile; private String fileName = "temp.jpg";
1.点击事件
//头像 @Click(R.id.avatar_rl) public void myinfo_iv(){ Dialog dialog = new AlertDialog.Builder(this).setIcon( R.drawable.ic_launcher).setTitle("上传头像").setPositiveButton("相机拍照", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent( "android.media.action.IMAGE_CAPTURE"); // 判断存储卡是否可以用,可用进行存储 if (Utils.hasSdcard()) { SimpleDateFormat sDateFormat = new SimpleDateFormat( "yyyyMMdd_HHmmss"); PHOTO_FILE_NAME = sDateFormat .format(new java.util.Date()) + ".jpg"; intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri .fromFile(new File(Environment .getExternalStorageDirectory(), PHOTO_FILE_NAME))); } startActivityForResult(intent, PHOTO_REQUEST_CAMERA); } }).setNegativeButton("手机相册", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, PHOTO_REQUEST_GALLERY); } }).create(); dialog.show(); }
2.返回事件
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PHOTO_REQUEST_GALLERY) { //相册 if (data != null) { // 得到图片的全路径 Uri uri = data.getData(); String DCIMPath = ""; Cursor cr = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.Media.DATA }, null, null, MediaStore.Images.Media.DATE_MODIFIED + " desc"); if (cr.moveToNext()) { DCIMPath = cr.getString(cr.getColumnIndex(MediaStore.Images.Media.DATA)); } 4000 cr.close(); if(uri!=null){ fileName =getFileName(getRealPathFromURI(data.getData())); crop(uri); }else if(!TextUtils.isEmpty(DCIMPath)){ fileName =getFileName(DCIMPath); crop(Uri.fromFile(new File(DCIMPath))); }else{ Toast.makeText(MyinfoActivity.this, "未能获取到图片", Toast.LENGTH_SHORT).show(); } } } else if (requestCode == PHOTO_REQUEST_CAMERA && resultCode != 0) {//拍照 if (Utils.hasSdcard()) { tempFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME); crop(Uri.fromFile(tempFile)); } else { Toast.makeText(MyinfoActivity.this, "未找到存储卡,无法存储照片!", Toast.LENGTH_SHORT).show(); } } else if (requestCode == PHOTO_REQUEST_CUT) {//结果 if (data != null) { try { bitmap = data.getParcelableExtra("data"); ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); byte[] buffer = out.toByteArray(); byte[] encode = Base64.encode(buffer, Base64.DEFAULT); String photo = new String(encode); ACache mCache = ACache.get(this); String token = mCache.getAsString("token"); WaitDialogUtil.showDialog(this); UpdatePicModel.updatepic(photo, token); /* String userId = spUtil.getUserid(); String verifyCode = spUtil.getkey(); mDialog = DialogUtil.getLoginDialog(this); mDialog.show(); RequestParams params = new RequestParams(); params.put("ImagefileName", fileName); params.put("filestream", photo); params.put("userid", userId); params.put("verifyCode", verifyCode); String url = URLManage.FileUploadImage();*/ /* HttpUtil.post(url, params, new JsonHttpResponseHandler() { @Override public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable e) { super.onFailure(statusCode, headers, responseBody, e); if (mDialog != null) mDialog.dismiss(); if (statusCode == 0) { T.showShort(getApplicationContext(), R.string.http_failure); } else { T.showShort(getApplicationContext(), R.string.server_failure); } } @Override public void onSuccess(JSONObject json) { // 返回的是JSONObject,会调用这里 L.i("json", json.toString()); if (mDialog != null) mDialog.dismiss(); try { int resultcode = json.getInt("resultcode"); if (resultcode == 0) { imageLoader.displayImage( json.optString("data"), img_head, ImageUtil.getMyInfoHeadOptions()); spUtil.setHeadImg(json .optString("data")); T.showShort(getApplicationContext(), "头像上传成功"); } else { String msg = json.getString("msg"); T.showShort(getApplicationContext(), msg); } } catch (Exception e) { L.e(e.toString()); } }; });*/ } catch (Exception e) { e.printStackTrace(); } } } }
4.方法
/** * 从URI获取路径 * * @param contentUri * @return */ public String getRealPathFromURI(Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(contentUri, proj, null, null, null); int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } /** * 获取文件名+扩展名 * * @param apath * @return */ public String getFileName(String apath) { int start = apath.lastIndexOf("/"); int end = apath.length(); // int end = apath.lastIndexOf("."); if (start != -1 && end != -1) { return apath.substring(start + 1, end); } else { return "temp.jpg"; } } /** * 剪切图片 * * @function: * @author:Jerry * @date:2013-12-30 * @param uri */ private void crop(Uri uri) { // 裁剪图片意图 Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); // 裁剪框的比例,1:1 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // 裁剪后输出图片的尺寸大小 intent.putExtra("outputX", 250); intent.putExtra("outputY", 250); // 图片格式 intent.putExtra("outputFormat", "JPEG"); intent.putExtra("noFaceDetection", true);// 取消人脸识别 intent.putExtra("return-data", true);// true:不返回uri,false:返回uri startActivityForResult(intent, PHOTO_REQUEST_CUT); }
5.上传服务器成功以后的处理
//更换头像 EventCenter.bindContainerAndHandler(this, new BmSimpleEventHandler() { public void onEvent(UpdatePicEvent event) { myinfo_avatar.setImageBitmap(bitmap); WaitDialogUtil.dissMiss(); ToastUtil.show(getApplicationContext(),"头像修改成功"); } }).tryToRegisterIfNot();
相关文章推荐
- C#实现将网页保存成图片的网页拍照功能
- android 拍照和上传的实现代码
- 使用ImageMagick进行图片缩放、合成与裁剪(js+python)
- PHP 使用 Imagick 裁切/生成缩略图/添加水印自动检测和处理 GIF
- jQuery插件jcrop+Fileapi完美实现图片上传+裁剪+预览的代码分享
- Android调用手机拍照功能的方法
- ASP.NET简单好用功能齐全图片上传工具类(水印、缩略图、裁剪等)
- 基于JQuery实现的图片自动进行缩放和裁剪处理
- MacOS 安装 PHP的图片裁剪扩展Tclip
- php视频拍照上传头像功能实现代码分享
- 开源中国 OsChina Android 客户端源码分析(6)拍照、图库、裁剪
- android 实现圆形imageView,Circle imageView.
- Adnroid Studio Gradle 多渠道打包
- vtk中实现裁剪
- android 调用系统相机拍照,并保存到SD卡
- 3D坐标系、矩阵变换、视景体与裁剪
- AndroidStudio创建TV-APP,出现Error:(28, 13) Failed to resolve: com.squareup.picasso:picasso:2.5.0 的解决
- 教你如何不翻墙就可以快速更新AndroidSDK Manager中的内容
- 安卓拍照和选择照片上传功能代码
- Android Studio汉化版安装说明