裁剪位图图像

52 android crop image-editing

如何裁剪位图图像?这是我的问题,我已尝试使用意图的一些概念,但仍然失败..

我有一个我要裁剪的位图图像!!

这是代码:

 Intent intent = new Intent("com.android.camera.action.CROP");  
                      intent.setClassName("com.android.camera", "com.android.camera.CropImage");  
                      File file = new File(filePath);  
                      Uri uri = Uri.fromFile(file);  
                      intent.setData(uri);  
                      intent.putExtra("crop", "true");  
                      intent.putExtra("aspectX", 1);  
                      intent.putExtra("aspectY", 1);  
                      intent.putExtra("outputX", 96);  
                      intent.putExtra("outputY", 96);  
                      intent.putExtra("noFaceDetection", true);  
                      intent.putExtra("return-data", true);                                  
                      startActivityForResult(intent, REQUEST_CROP_ICON);
Run Code Online (Sandbox Code Playgroud)

@Thanks可以帮助我吗?

san*_*ttt 122

我使用这种方法裁剪图像,它完美无缺:

Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.xyz);

resizedbitmap1=Bitmap.createBitmap(bmp, 0,0,yourwidth, yourheight);
Run Code Online (Sandbox Code Playgroud)

createBitmap()采用位图,启动X,启动Y,宽度和高度作为参数

  • 有没有办法裁剪位图,但输出位图将具有特定大小,而无需创建另一个位图? (2认同)

coy*_*yer 13

如果您想切片/裁剪区域特定的界限,使用上面的答案不起作用!使用此代码,您将始终获得所需的大小 - 即使源较小.

//  Here I want to slice a piece "out of bounds" starting at -50, -25
//  Given an endposition of 150, 75 you will get a result of 200x100px
Rect rect = new Rect(-50, -25, 150, 75);  
//  Be sure that there is at least 1px to slice.
assert(rect.left < rect.right && rect.top < rect.bottom);
//  Create our resulting image (150--50),(75--25) = 200x100px
Bitmap resultBmp = Bitmap.createBitmap(rect.right-rect.left, rect.bottom-rect.top, Bitmap.Config.ARGB_8888);
//  draw source bitmap into resulting image at given position:
new Canvas(resultBmp).drawBitmap(bmp, -rect.left, -rect.top, null);
Run Code Online (Sandbox Code Playgroud)

......你完成了!