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

时间:2011-06-21 17:02:21

标签: android

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

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

1 个答案:

答案 0 :(得分:8)

您可能希望使用createBitmap的其他重载 - 它具有x,y,width和height参数,您可以使用这些参数将位图的中间部分裁剪为新的位图。

这样的事情:

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

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