如何裁剪位图的中间区域?

4
我怎样才能剪切位图的中间区域? 这是我的示例代码:
 public void onPictureTaken(byte[] paramArrayOfByte, Camera paramCamera)

{

 FileOutputStream fileOutputStream = null;
 try {

     File saveDir = new File("/sdcard/CameraExample/");

     if (!saveDir.exists())
     {
     saveDir.mkdirs();
     }

     BitmapFactory.Options options = new BitmapFactory.Options();

     options.inSampleSize = 5;

     Bitmap myImage = BitmapFactory.decodeByteArray(paramArrayOfByte, 0,paramArrayOfByte.length, options);

     Bitmap bmpResult = Bitmap.createBitmap(myImage.getWidth(), myImage.getHeight(),Config.RGB_565);



     int length = myImage.getHeight()*myImage.getWidth();

     int[] pixels = new int[length];


     myImage.getPixels(pixels, 0, myImage.getWidth(), 0,0, myImage.getWidth(), myImage.getHeight());

     Bitmap TygolykovLOL = Bitmap.createBitmap(pixels, 0, myImage.getWidth(), myImage.getWidth(),myImage.getHeight(), Config.RGB_565);

     Paint paint = new Paint();         

     Canvas myCanvas = new Canvas(bmpResult);

     myCanvas.drawBitmap(TygolykovLOL, 0, 0, paint);





  fileOutputStream = new FileOutputStream("/sdcard/CameraExample/"  + "1ggggqqqqGj2.bmp");

     BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream );


     bmpResult.compress(CompressFormat.PNG, 100, bos);

     bos.flush();
     bos.close();

乍一看,这段代码似乎是使用getPixels()从Bitmap中剪切出中间部分。您是否遇到了错误或者正在尝试做其他事情? - Barry Fruitman
当我指向XY时,会出现错误,更准确地说,我不明白如何指定一个坐标位置来在myImage中间切出一个矩形。 - Domos
如果你只说“出错了”,而没有提供错误信息,那么帮助就很困难。 - Barry Fruitman
1个回答

8
您可能需要使用 createBitmap 其他重载方法 - 它具有x,y,width和height参数,您可以使用这些参数将位图的中间部分裁剪成一个新的位图。
例如: Bitmap cropped = Bitmap.createBitmap(sourceBitmap, 50, 50, sourceBitmap.getWidth() - 100, sourceBitmap.getHeight() - 100); 以剪切出距边缘50像素的所有内容。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接