Advertisement
Le_BuG63

Untitled

Feb 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. public class BitmapResizer {
  2.     public static Bitmap bitmapResizerNN(Bitmap bitmap, int newWidth, int newHeight) {
  3.         int[] colors = new int[newWidth * newHeight];
  4.  
  5.         int width = bitmap.getWidth();
  6.         int height = bitmap.getHeight();
  7.  
  8.         int xRatio = ((width << 16) / newWidth) + 1;
  9.         int yRatio = ((height << 16) / newHeight) + 1;
  10.  
  11.         for(int i = 0; i < newHeight; ++i) {
  12.             for(int j = 0; j < newWidth; ++j) {
  13.                 int x = (j*xRatio) >> 16;
  14.                 int y = (i*yRatio) >> 16;
  15.  
  16.                 colors[i * newWidth + j] = bitmap.getPixel(x, y);
  17.             }
  18.         }
  19.  
  20.         Bitmap scaledBitmap = Bitmap.createBitmap(colors, newWidth, newHeight, Bitmap.Config.ARGB_8888);
  21.  
  22.         return scaledBitmap;
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement