裁剪位图图像

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

我有一个位图图像,我想裁剪!

这里是代码:

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

有没有人可以帮我关于这个@谢谢

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

 Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.xyz); resizedbitmap1=Bitmap.createBitmap(bmp, 0,0,yourwidth, yourheight); 

createBitmap()需要位图,启动X,开始Y,宽度和高度作为参数

使用上面的答案是不行的,如果你想切片/作物领域特别是越界 ! 使用此代码,您将始终得到您所需的大小 – 即使源更小。

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

…你完成了!

我有种类似的问题,尝试了很多方法后,我发现了这个对我有意义的问题。 这种方法只裁剪图像的方形,我仍然在工作的圆形(随意修改代码来获得你需要的形状)。

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

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

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

我们需要方形,所以我们需要缩短双方的时间。 而且,为了保持图像居中,裁剪需要在图像的两侧进行。 希望这张照片能帮助你理解我的意思。 视觉表示的裁剪。 图像中的红点表示我们需要的初始像素和最终像素,具有短划线的variables在数字上等于没有短划线的相同variables。

现在最后的代码:

 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; 
Interesting Posts