/** * 拍照获取图片 */ private void takePhoto(int type) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //将拍照的原图保存到File(Config.HEADIMAGE + Config.HEADTESTIMAGENAME) //如果不指定保存路径 系统会给一个压缩后的照片而不是原图。裁剪时会缩小或失真 Uri imageUri = Uri.fromFile(new File(Config.HEADIMAGE + Config.HEADTESTIMAGENAME)); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, type); } /*** * 从相册中取图片 */ private void pickPhoto(int type) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, type); //if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.KITKAT){ // startActivityForResult(intent, type); //}else{ // startActivityForResult(intent, type); //} } /**图片裁剪**/ public void cropImage(Uri uri, int outputX, int outputY, int requestCode) { // 裁剪图片意图 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", outputX); intent.putExtra("outputY", outputY); // 图片格式 intent.putExtra("outputFormat", "JPEG"); intent.putExtra("noFaceDetection", true); intent.putExtra("return-data", true); startActivityForResult(intent, requestCode); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { Bitmap bm = null; ContentResolver resolver = getContentResolver(); switch (requestCode) { case (Constant.POSITIVE_LOCAL_CODE)://本地图库 String path = getPath(context, data.getData()); File file = new File(path); cropImage(Uri.fromFile(file), 200, 200, 301); break; case 301://处理本地图库裁剪后的图片 Bitmap photo = null; Uri photoUri = data.getData(); if (photoUri != null) { photo = BitmapFactory.decodeFile(photoUri.getPath()); } if (photo == null) { Bundle extra = data.getExtras(); if (extra != null) { photo = (Bitmap) extra.get("data"); } } if (photo != null) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); // 图片压缩 40% photo.compress(Bitmap.CompressFormat.JPEG, 60, stream); } if (!Utils.SDstate()) { Toast.makeText( context, context.getString(R.string.identity_authentication_SDState), Toast.LENGTH_SHORT).show(); return; } // 显得到bitmap图片 存成圆形 FileUtils.saveImageInSD(ImageUtil.toRoundBitmap(photo), Config.HEADIMAGE, Config.HEADIMAGENAME); imageupdate = 2; headImage.setImageBitmap(BitmapFactory.decodeFile(Config.HEADIMAGE + Config.HEADIMAGENAME)); personHeadText.setText(context .getString(R.string.personal_data_modify_head)); break; case (Constant.POSITIVE_PHOTO_CODE)://拍照 if (!Utils.SDstate()) { Toast.makeText( context, context.getString(R.string.identity_authentication_SDState), Toast.LENGTH_SHORT).show(); return; } File file1 = new File(Config.HEADIMAGE + Config.HEADTESTIMAGENAME); cropImage(Uri.fromFile(file1), 200, 200, 301); break; } } else { Toast.makeText(context, context.getString(R.string.personal_data_choice_head), Toast.LENGTH_SHORT).show(); } } @SuppressLint("NewApi") /**区别android4.4及以上版本的uri的解析方式*/ public static String getPath(final Context context, final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } // TODO handle non-primary volumes } // DownloadsProvider else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { // Return the remote address if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); return getDataColumn(context, uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); } } finally { if (cursor != null) cursor.close(); } return null; } public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is Google Photos. */ public static boolean isGooglePhotosUri(Uri uri) { return "com.google.android.apps.photos.content".equals(uri.getAuthority()); } /**选择图片的pop*/ public void getImage(final int type) { LayoutInflater inflater = (LayoutInflater) this .getSystemService(LAYOUT_INFLATER_SERVICE); View pop = inflater.inflate(R.layout.image_pop, null); TextView male = (TextView) pop.findViewById(R.id.radio_male); TextView female = (TextView) pop.findViewById(R.id.radio_female); TextView cancel = (TextView) pop.findViewById(R.id.radio_cancel); // male.setText("拍照"); // female.setText("选择照片"); this.genderPop = new PopupWindow(pop, RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); this.genderPop.setBackgroundDrawable(new BitmapDrawable()); this.genderPop.setFocusable(true); this.genderPop.showAtLocation(personalLayout, Gravity.BOTTOM, 0, 0); male.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { takePhoto(Constant.POSITIVE_PHOTO_CODE); PersonalDataActivity.this.genderPop.dismiss(); } }); female.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pickPhoto(Constant.POSITIVE_LOCAL_CODE); PersonalDataActivity.this.genderPop.dismiss(); } }); cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PersonalDataActivity.this.genderPop.dismiss(); } }); } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { // 上传头像 case Constant.TO_UPLOAD_FILE: toUploadFile(DBUtil.getUser(context).getToken(), Constant.IMG_TYPE_HEAD, Config.HEADIMAGE+Config.HEADIMAGENAME); break; default: break; } super.handleMessage(msg); } }; IMAGEUTIL .存圆形图 */ public static Bitmap toRoundBitmap(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { roundPx = width / 2; left = 0; top = 0; right = width; bottom = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom); final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); final RectF rectF = new RectF(dst); paint.setAntiAlias(true);// 设置画笔无锯齿 canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas paint.setColor(color); // 以下有两种方法画圆,drawRounRect和drawCircle // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。 canvas.drawCircle(roundPx, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452 canvas.drawBitmap(bitmap, src, dst, paint); //以Mode.SRC_IN模式合并bitmap和已经draw了的Circle return output; }
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦