⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scaler.java

📁 纯Java实现的对图片进行缩放
💻 JAVA
字号:
package com.botwave;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Scaler {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// testMakeThuib();
		testResize();
		
		System.out.println("end");
	}

	public static void testResize() throws IOException {
		BufferedImage bi = ImageIO.read(new File("D:/mobilephone_pics/createStall2.bmp"));
		int dstWidth = 1120;
		int dstHeight = 1680;
		BufferedImage biRet = resize(bi.getWidth()/16, bi.getHeight()/16, bi);
		ImageIO.write(biRet, "png", new File(
				"D:/mobilephone_pics/temp2.png"));

		System.out.println("End1");

	}

	public static BufferedImage resize(int dstWidth, int dstHeight,
			BufferedImage oriBufImage) {
		BufferedImage biRet = new BufferedImage(dstWidth, dstHeight,
				BufferedImage.TYPE_3BYTE_BGR);
		boolean interpolate = true; // 插值模式
		int roiWidth = oriBufImage.getWidth();
		int roiHeight = oriBufImage.getHeight();
		int width = oriBufImage.getWidth();
		double srcCenterX = roiWidth / 2.0;
		double srcCenterY = roiHeight / 2.0;
		double dstCenterX = dstWidth / 2.0;
		double dstCenterY = dstHeight / 2.0;
		double xScale = (double) dstWidth / roiWidth;
		double yScale = (double) dstHeight / roiHeight;

		double xlimit = width - 1.0, xlimit2 = width - 1.001;

		if (interpolate) {
			// if (xScale<=0.25 && yScale<=0.25){
			// makeThumbnail();
			// return ;
			// }
			dstCenterX += xScale / 2.0;
			dstCenterY += yScale / 2.0;
		}

		double xs, ys;
		for (int y = 0; y <= dstHeight - 1; y++) {
			ys = (y - dstCenterY) / yScale + srcCenterY;

			for (int x = 0; x <= dstWidth - 1; x++) {
				xs = (x - dstCenterX) / xScale + srcCenterX;
				if (interpolate) {
					if (xs < 0.0)
						xs = 0.0;
					if (xs >= xlimit)
						xs = xlimit2;
					biRet.setRGB(x, y,
							getInterpolatedPixel(xs, ys, oriBufImage));
				}
			}
		}
		return biRet;
	}

	/**
	 * Uses bilinear interpolation to find the pixel value at real coordinates
	 * (x,y).
	 */
	private static final int getInterpolatedPixel(double x, double y,
			BufferedImage bi) {
		int xbase = (int) x;
		int ybase = (int) y;
		double xFraction = x - xbase;
		double yFraction = y - ybase;

		int lowerLeft = bi.getRGB((int) x, (int) y);
		// lowerLeft = lowerLeft << 8 >>> 8;
		int rll = (lowerLeft & 0xff0000) >> 16;
		int gll = (lowerLeft & 0xff00) >> 8;
		int bll = lowerLeft & 0xff;

		int lowerRight = bi.getRGB((int) x + 1, (int) y);
		// lowerRight = lowerRight << 8 >>> 8;
		int rlr = (lowerRight & 0xff0000) >> 16;
		int glr = (lowerRight & 0xff00) >> 8;
		int blr = lowerRight & 0xff;

		int upperRight = bi.getRGB((int) x + 1, (int) y + 1);
		// upperRight = upperRight << 8 >>> 8;
		int rur = (upperRight & 0xff0000) >> 16;
		int gur = (upperRight & 0xff00) >> 8;
		int bur = upperRight & 0xff;

		int upperLeft = bi.getRGB((int) x, (int) y + 1);
		// upperLeft = upperLeft << 8 >>> 8;
		int rul = (upperLeft & 0xff0000) >> 16;
		int gul = (upperLeft & 0xff00) >> 8;
		int bul = upperLeft & 0xff;

		int r, g, b;
		double upperAverage, lowerAverage;
		upperAverage = rul + xFraction * (rur - rul);
		lowerAverage = rll + xFraction * (rlr - rll);
		r = (int) (lowerAverage + yFraction * (upperAverage - lowerAverage) + 0.5);
		upperAverage = gul + xFraction * (gur - gul);
		lowerAverage = gll + xFraction * (glr - gll);
		g = (int) (lowerAverage + yFraction * (upperAverage - lowerAverage) + 0.5);
		upperAverage = bul + xFraction * (bur - bul);
		lowerAverage = bll + xFraction * (blr - bll);
		b = (int) (lowerAverage + yFraction * (upperAverage - lowerAverage) + 0.5);

		return 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | b & 0xff;
	}

	public static void makeThumbnail() throws IOException {
		BufferedImage bi = ImageIO.read(new File("d:/test/ImageScaleTest.png"));

		int width2 = 300;
		int height2 = 300;
		float smoothFactor = 1f;
		BufferedImage biRet = new BufferedImage(width2, height2,
				BufferedImage.TYPE_3BYTE_BGR);

		int width = bi.getWidth();
		int height = bi.getHeight();
		int[] pixel = new int[3];
		int[] sum = new int[3];
		double xscale, yscale;
		int w, h;
		double product;
		xscale = (double) width / width2;
		yscale = (double) height / height2;
		w = (int) (xscale * smoothFactor);
		h = (int) (yscale * smoothFactor);
		product = w * h;

		for (int y = 0; y < height2; y++) {
			for (int x = 0; x < width2; x++) {
				for (int i = 0; i < 3; i++)
					sum[i] = 0;
				int xbase = (int) (x * xscale);
				int ybase = (int) (y * yscale);
				for (int y2 = 0; y2 < h; y2++) {
					for (int x2 = 0; x2 < w; x2++) {
						int val = bi.getRGB(xbase + x2, ybase + y2);
						val <<= 8;
						pixel[1] = val >>> 24;
						pixel[0] = (val << 8) >>> 24;
						pixel[2] = (val << 16) >>> 24;
						for (int i = 0; i < 3; i++)
							sum[i] += pixel[i];
					}
				}
				for (int i = 0; i < 3; i++)
					sum[i] = (int) (sum[i] / product + 0.5);
				int ret = 0xff;
				ret <<= 24;
				ret += sum[0] << 8;
				ret += sum[1] << 16;
				ret += sum[2] << 24;
				biRet.setRGB(x, y, ret);
			}
		}

		ImageIO.write(biRet, "jpg", new File(
				"d:/test/ImageScaleTestResoult.jpg"));
		System.out.println("End!");
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -