/** * 获取裁切intent * * @param uri * @param outputX * @param outputY * @param aspectX * @param aspectY * @param outputFormat * @return */ public static Intent getCropIntent( Intent intent, Uri uri, int outputX, int outputY, float aspectX, float aspectY, String outputFormat) { if (intent == null) intent = new Intent(); intent.setAction("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); // intent.putExtra( "circleCrop", "of course" ); // circleCrop不为空则进行圆形裁切 intent.putExtra("aspectX", (int) (aspectX + .5)); intent.putExtra("aspectY", (int) (aspectY + .5)); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); // 黑边 intent.putExtra("scaleUpIfNeeded", true); // 黑边 if (uri != null) { intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", false); // intent.putExtra( "return-data", true ); } else intent.putExtra("return-data", true); if (TextUtils.isEmpty(outputFormat)) intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); else intent.putExtra("outputFormat", outputFormat); intent.putExtra("noFaceDetection", true); return intent; }
// 裁剪图片 private void startImageAction( Uri uri, int outputX, int outputY, int requestCode, boolean isCrop) { Intent intent = null; if (isCrop) { intent = new Intent("com.android.camera.action.CROP"); } else { intent = new Intent(Intent.ACTION_GET_CONTENT, null); } intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); if (requestCode == SeventhConstants.REQUESTCODE_UPLOADAVATAR_GALLERY_CROP) { // 若是相册传来的数据我们需要对url处理,将裁剪后的url保存到心得地方去 File dir = new File(SeventhConstants.MyAvatarDir); if (!dir.exists()) { dir.mkdirs(); } // 原图 File file = new File(dir, new SimpleDateFormat("yyMMddHHmmss").format(new Date()) + ".jpg"); Uri uri2 = Uri.fromFile(file); filePath = file.getAbsolutePath(); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri2); } else { // 用裁剪之后的相机图片覆盖了原图 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); } intent.putExtra("return-data", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); }
/** 调用图片剪辑程序 */ public static Intent getCropImageIntent( Uri photoUri, int aspectX, int aspectY, int outputX, int outputY) { Intent intent; intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(photoUri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", aspectX); // 宽高比 intent.putExtra("aspectY", aspectY); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); cropfile = new File(CameraUtil.CROP_FILE_DIR + File.separator + CameraUtil.getPhotoFileName()); try { if (cropfile.exists()) { cropfile.delete(); } cropfile.getParentFile().mkdirs(); cropfile.createNewFile(); } catch (IOException ex) { Log.e("io", ex.getMessage()); } intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cropfile)); intent.putExtra("return-data", false); // 若为false则表示不返回数据 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", false); return intent; }
/** 获取本地图片,并剪切 */ public void getFromLocal() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 200); intent.putExtra("outputY", 200); intent.putExtra("scale", true); intent.putExtra("return-data", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection baseActivity.startActivityForResult(intent, PhotoUpload.PHOTO_ZOOM); }
/** 从相册截小图 */ public void cropSmallImageFromGallery() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 200); intent.putExtra("outputY", 200); intent.putExtra("scale", true); intent.putExtra("return-data", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, CHOOSE_SMALL_PICTURE); }
// //截图 private void cropImageUri(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"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", false); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); }
/** * 图片剪切功能,需要在活动中监听forresult方法 * * @param paramUri 图片所在资源路径 */ public void startPhotoZoom(Uri paramUri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(paramUri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 200); intent.putExtra("outputY", 200); intent.putExtra("scale", true); intent.putExtra("return-data", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection baseActivity.startActivityForResult(intent, PHOTO_ZOOM); }
private void cropPhoto(Uri data) { Intent intent = new Intent(); intent.setAction("com.android.camera.action.CROP"); intent.setDataAndType(data, "image/*"); // mUri是已经选择的图片Uri intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); // 裁剪框比例 intent.putExtra("aspectY", 1); intent.putExtra("outputX", 150); // 输出图片大小 intent.putExtra("outputY", 150); intent.putExtra("return-data", true); intent.putExtra("scale", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection] startActivityForResult(intent, Constants.ACTION_IMAGE_CROP); }
private Intent startCameraZoomIntent(Uri uri, int size) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", size); intent.putExtra("outputY", size); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection return intent; }
public class CropParams { public static final String CROP_TYPE = "image/*"; public static final String OUTPUT_FORMAT = Bitmap.CompressFormat.JPEG.toString(); public static final int DEFAULT_ASPECT_W = 720; // 选择框比例 public static final int DEFAULT_ASPECT_H = 400; public static final int DEFAULT_OUTPUT_W = 720; // 输出图片大小 public static final int DEFAULT_OUTPUT_H = 400; public Uri uri; public String type; public String outputFormat; public String crop; public boolean scale; public boolean returnData; public boolean noFaceDetection; public boolean scaleUpIfNeeded; public int aspectX; public int aspectY; public static int outputX; public static int outputY; public boolean isCrop = true; public CropParams() { uri = CropHelper.buildUri(); type = CROP_TYPE; outputFormat = OUTPUT_FORMAT; crop = "true"; scale = true; returnData = false; noFaceDetection = true; scaleUpIfNeeded = true; aspectX = DEFAULT_ASPECT_W; aspectY = DEFAULT_ASPECT_H; outputX = DEFAULT_OUTPUT_W; outputY = DEFAULT_OUTPUT_H; } }
/** * 创建调用系统图片裁剪 * * @return intent */ public static Intent startPhotoZoomIntent(Uri uri, Uri toUri, int size) { // Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // intent.setType("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", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, toUri); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); return intent; }
/** * 通用的剪切方法 * * @param uri * @param outputX * @param outputY * @param requestCode */ private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode) { File cacheDir = getCacheDir(); if (!cacheDir.exists()) { cacheDir.mkdirs(); } File file = new File(cacheDir, "take_big.png"); Uri imageUri = Uri.parse("file:///" + file.getAbsolutePath()); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 2); intent.putExtra("aspectY", 1); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra("return-data", false); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); }
public class CropConfig { public static final String CROP_TYPE = "image/*"; public static final String OUTPUT_FORMAT = Bitmap.CompressFormat.JPEG.toString(); public static final int DEFAULT_ASPECT = 1; public static final int DEFAULT_OUTPUT = 300; public Uri uri; public String type; public String outputFormat; public String crop; public boolean scale; public boolean returnData; public boolean noFaceDetection; public boolean scaleUpIfNeeded; public int aspectX; public int aspectY; public int outputX; public int outputY; public CropConfig() { uri = CropUtils.buildUri(); type = CROP_TYPE; outputFormat = OUTPUT_FORMAT; crop = "true"; scale = true; returnData = false; noFaceDetection = true; scaleUpIfNeeded = true; aspectX = DEFAULT_ASPECT; aspectY = DEFAULT_ASPECT; outputX = DEFAULT_OUTPUT; outputY = DEFAULT_OUTPUT; } }
/** * 打开系统自带裁减方法 * * @param url */ private void openCropView(String url, Boolean isCamera) { // 获取屏幕宽度 WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); Intent intent = new Intent("com.android.camera.action.CROP"); if (isCamera) { // 相机图片不用另外保存 File vFile = new File(url); ChooseUri = Uri.fromFile(vFile); } else { String tempUrl = Environment.getExternalStorageDirectory() + "/flashtag/" + System.currentTimeMillis() + ".jpg"; File vFile = new File(tempUrl); if (!vFile.exists()) { File vDirPath = vFile.getParentFile(); // new File(vFile.getParent()); vDirPath.mkdirs(); } if (copyFile(url, tempUrl)) { ChooseUri = Uri.fromFile(vFile); } } intent.setDataAndType(ChooseUri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", aspectX); // 裁剪框比例 intent.putExtra("aspectY", aspectY); intent.putExtra("outputX", outputX); // 输出图片大小 intent.putExtra("outputY", outputY); Log.v("dd", Integer.toString(width)); intent.putExtra("scale", true); intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, ChooseUri); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, PIC_CROP_BACK); }
protected void showActionList() { final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>(); // get list camera action Intent intentCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); List<ResolveInfo> listCapture = getPackageManager().queryIntentActivities(intentCapture, 0); if (listCapture.size() > 1) { for (ResolveInfo res : listCapture) { final CropOption co = new CropOption(); co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo); co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo); co.appIntent = new Intent(intentCapture); co.requestCode = RequestCode.REQUEST_CAPTURE; co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); cropOptions.add(co); } } else if (listCapture.size() == 1) { final ResolveInfo res = listCapture.get(0); final CropOption co = new CropOption(); co.title = getString(R.string.common_camera); co.icon = getResources().getDrawable(R.drawable.img_btn_bottom_camera); co.appIntent = new Intent(intentCapture); co.requestCode = RequestCode.REQUEST_CAPTURE; co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); cropOptions.add(co); } // get list gallery action Intent intentGallery = new Intent(Intent.ACTION_GET_CONTENT); intentGallery .setType("image/*") // .putExtra("crop", "true") Using Aviary to edit photo .putExtra("scale", false) .putExtra("scaleUpIfNeeded", true) .putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); List<ResolveInfo> listGallery = getPackageManager().queryIntentActivities(intentGallery, PackageManager.MATCH_DEFAULT_ONLY); if (listGallery.size() > 0) { final ResolveInfo res = listGallery.get(0); final CropOption co = new CropOption(); co.title = getString(R.string.common_gallery); co.icon = getResources().getDrawable(R.drawable.img_btn_bottom_gallery); co.appIntent = new Intent(intentGallery); co.requestCode = RequestCode.REQUEST_GALLERY; co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); cropOptions.add(co); } CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions); // build dialog to select action final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.dialog_choose_action); builder.setAdapter( adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { try { final CropOption crop = cropOptions.get(item); isLoadGalleryOrCamera = true; generationImageTemp(); if (crop.requestCode == RequestCode.REQUEST_GALLERY) { crop.appIntent.putExtra(MediaStore.EXTRA_OUTPUT, mNewImagePath); } else { ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, mNewImageName); // store content mNewImageUriCamera = getContentResolver() .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); // set the image file name values crop.appIntent.putExtra(MediaStore.EXTRA_OUTPUT, mNewImageUriCamera); } startActivityForResult(crop.appIntent, crop.requestCode); } catch (Exception ex) { // ko tao duoc file t???m th?? show toast th??ng b??o ng?????i d??ng showCenterToast(R.string.msg_err_camera_not_sdcard); } } }); builder.setOnCancelListener( new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { finish(); } }); AlertDialog alert = builder.create(); alert.show(); }