Масштабирование картинки
- public Image scale(Image src, int width, int height, boolean useAntialiasing) {
- int y, c1, startY, endY, ceilY, x, startX, endX, ceilX;
- int alpha, r, g, b;
- int pixCount, yy, c2, factorY, xx, source, factorX, factor;
- int srcw = src.getWidth();
- int srch = src.getHeight();
- if (srcw == width && srch == height) return src;
- int[] buf = new int[srcw * srch];
- int[] buf2 = new int[width * height];
- src.getRGB(buf, 0, srcw, 0, 0, srcw, srch);
- int scaleX = (srcw << 10) / width;
- int scaleY = (srch << 10) / height;
- for (y = 0; y < height; y++) {
- c1 = y * width;
- startY = y * scaleY;
- endY = startY + scaleY;
- ceilY = Math.min(endY + 1023 >> 10, srch);
- for (x = 0; x < width; x++) {
- if (useAntialiasing) {
- startX = x * scaleX;
- endX = startX + scaleX;
- ceilX = Math.min(endX + 1023 >> 10, srcw);
- alpha = 0;
- r = 0;
- g = 0;
- b = 0;
- pixCount = 0;
- for (yy = startY >> 10; yy < ceilY; yy++) {
- c2 = yy * srcw;
- factorY = (yy << 10 < startY) ? (startY - (yy << 10)) : ((yy > ceilY) ? (yy - ceilY << 10) : 1024);
- for (xx = startX >> 10; xx < ceilX; xx++) {
- source = buf[c2 + xx];
- factorX = (xx << 10 < startX) ? (startX - (xx << 10)) : ((xx > ceilX) ? (xx - ceilX << 10) : 1024);
- factor = Math.max(1, factorX * factorY >> 10);
- alpha += (source >> 24 & 255) * factor;
- r += (source >> 16 & 255) * factor;
- g += (source >> 8 & 255) * factor;
- b += (source & 255) * factor;
- pixCount += factor;
- }
- }
- alpha /= pixCount;
- r /= pixCount;
- g /= pixCount;
- b /= pixCount;
- buf2[c1 + x] = (alpha << 24) + (r << 16) + (g << 8) + b;
- }
- else buf2[c1 + x] = buf[(startY >> 10) * srcw + x * (scaleX >> 10)];
- }
- }
- buf = null;
- return Image.createRGBImage(buf2, width, height, true);
- }
Хочу предоставить код для масштабтирования картинки. Код вполне ясен и удобен в использлвании