【问题标题】:How do I cut out the middle area of the bitmap?如何剪切位图的中间区域?
【发布时间】:2011-06-21 17:02:21
【问题描述】:

如何将位图的中间区域剪掉? 这是我的示例代码:

 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() 剪切了位图的中间部分。您是否遇到错误或尝试做其他事情?
  • 当我指向 XY 时出现错误,更准确地说,我不明白如何指定一个坐标位置以在 myImage 的中间切出一个矩形。
  • 如果您只说“错误”而不提供错误消息,这很难提供帮助。

标签: android


【解决方案1】:

您可能想要使用 createBitmap 的另一个重载 - 它具有 x、y、宽度和高度参数,您可以使用这些参数将位图的中间部分裁剪为新的位图。

类似这样的:

Bitmap cropped = Bitmap.createBitmap(sourceBitmap, 50, 50, sourceBitmap.getWidth() - 100, sourceBitmap.getHeight() - 100);

从边缘剪掉 50 像素内的所有内容。

【讨论】:

  • 非常感谢您对我的帮助!
猜你喜欢
  • 1970-01-01
  • 2010-11-22
  • 2019-01-18
  • 1970-01-01
  • 2012-08-09
  • 2023-04-08
  • 2015-09-29
  • 2020-06-12
  • 1970-01-01
相关资源
最近更新 更多