Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cut out the middle area of the bitmap?

Tags:

android

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();
like image 879
Domos Avatar asked Feb 23 '23 19:02

Domos


1 Answers

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.

like image 150
ravuya Avatar answered Mar 07 '23 06:03

ravuya