4

How do I cut out the middle area of ​​the bitmap? it's my sample code:

 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();
Cœur
  • 37,241
  • 25
  • 195
  • 267
Domos
  • 193
  • 3
  • 11
  • At first glance it looks like that code does cut out the middle of a Bitmap with getPixels(). Are you getting an error or trying to do something else? – Barry Fruitman Jun 21 '11 at 17:12
  • I gives an error when I point XY and, more precisely, I do not understand how to specify a coordinate position that would cut out a rectangle in the middle of myImage. – Domos Jun 21 '11 at 17:18
  • It's hard to help when you just say "an error" instead of providing the error message. – Barry Fruitman Jun 23 '11 at 16:12

1 Answers1

8

You might want to use the other overload of createBitmap - it has x, y, width and height parameters which you could use to crop the middle portion of the bitmap into a new bitmap.

Something like this:

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

to clip out everything 50 pixels in from the edges.

ravuya
  • 8,586
  • 4
  • 30
  • 33