裁剪位图图像

时间:2013-04-03 13:43:51

标签: 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);

有人可以帮我解决这个@Thanks

4 个答案:

答案 0 :(得分:117)

我使用这种方法裁剪图像,效果很好:

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

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

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

答案 1 :(得分: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);

......你已经完成了!

答案 2 :(得分:1)

我有类似的裁剪问题,在尝试了很多方法后,我想出了一个对我有意义的方法。这种方法只能将图像裁剪成方形,我仍在处理圆形(可以随意修改代码以获得所需的形状)。

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

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

图像信息存储在int []数组中,它只不过是一个包含每个像素颜色值的整数数组,从图像的左上角开始,索引为0,右下角结束使用索引N.您可以使用Bitmap.getPixels()方法获取此数组,该方法采用各种参数。

我们需要方形,因此我们需要缩短边长。此外,为了使图像保持居中,需要在图像的两侧进行裁剪。希望图像能帮助您理解我的意思。 Visual representation of the cropping. 图像中的红点代表我们需要的初始和最终像素,带破折号的变量在数值上等于没有破折号的同一变量。

现在终于代码:

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;

答案 3 :(得分:1)

要从左侧和右侧裁剪具有给定宽度的位图,只需使用此代码

int totalCropWidth = "your total crop width"; int cropingSize = totalCropWidth / 2; Bitmap croppedBitmap = Bitmap.createBitmap(yourSourceBitmap, cropingSize ,0,yourSourceBitmapwidth-totalCropWidth , yourheight);