裁剪位图图像(Crop a Bitmap image)

2019-08-19 06:14发布

我如何裁剪位图图像? 这是我一直在使用意图尝试了一些概念,但仍未我的问题..

我有我要裁剪位图图像!

这里是代码:

 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);

任何人可以帮助我对此@Thanks

Answer 1:

我用这个方法来裁剪图像和它的作品完美:

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

resizedbitmap1=Bitmap.createBitmap(bmp, 0,0,yourwidth, yourheight);

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



Answer 2:

使用上面的回答没有工作,如果你想切/作物区特别是出界 ! 使用此代码,你总是会得到你想要的大小 - 即使源较小。

//  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);

......就大功告成了!



Answer 3:

我曾与裁剪类似的问题,并试图多种方法后,我想通了,这其中一个对我有意义。 此方法仅裁剪图像方形,我还在工作的圆形(随意修改代码以获得您所需要的形状)。

所以,首先你要裁剪YOUT位图:

Bitmap image; //you need to initialize it in your code first of course

图像信息被存储在一个int []数组什么不过是包含各像素的颜色值,开始于索引为0的图像的左上角和结束在与索引N右下角整数数组更你可以得到这个阵列Bitmap.getPixels()方法,该方法采取各种参数。

我们需要方形,因此我们需要缩短双方的时间。 此外,为了保持图像居中裁剪需要在图像的两侧进行。 希望这些图像将帮助你明白我的意思。 裁剪的可视化表示。 图像中的红点代表,我们需要的初始和最终的像素和与所述划线的变量在数值上等于无划线相同的变量。

现在终于代码:

int imageHeight = image.getHeight(); //get original image height
int imageWidth = image.getWidth();  //get original image width
int offset = 0;

int shorterSide = imageWidth < imageHeight ? imageWidth : imageHeight;
int longerSide = imageWidth < imageHeight ? imageHeight : imageWidth;
boolean portrait = imageWidth < imageHeight ? true : false;  //find out the image orientation
//number array positions to allocate for one row of the pixels (+ some blanks - explained in the Bitmap.getPixels() documentation)
int stride = shorterSide + 1; 
int lengthToCrop = (longerSide - shorterSide) / 2; //number of pixel to remove from each side 
//size of the array to hold the pixels (amount of pixels) + (amount of strides after every line)
int pixelArraySize = (shorterSide * shorterSide) + (shorterImageDimension * 1);
int pixels = new int[pixelArraySize];

//now fill the pixels with the selected range 
image.getPixels(pixels, 0, stride, portrait ? 0 : lengthToCrop, portrait ? lengthToCrop : 0, shorterSide, shorterSide);

//save memory
image.recycle();

//create new bitmap to contain the cropped pixels
Bitmap croppedBitmap = Bitmap.createBitmap(shorterSide, shorterSide, Bitmap.Config.ARGB_4444);
croppedBitmap.setPixels(pixels, offset, 0, 0, shorterSide, shorterSide);

//I'd recommend to perform these kind of operations on worker thread
listener.imageCropped(croppedBitmap);

//Or if you like to live dangerously
return croppedBitmap;


文章来源: Crop a Bitmap image