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

📄 imagetools.java

📁 本文首先介绍了这些考试系统的形成和发展过程
💻 JAVA
字号:
package net.robin.crm.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.ReplicateScaleFilter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;

public class ImageTools {
	
	public static final String PNG = "png";
	
	public static final String JPEG = "jpeg";
	
	public static final String JPG = "jpg";
	
	public static final String GIF = "gif";
	

	private ImageTools() {
		super();
		// TODO 自动生成构造函数存根
	}

	public static BufferedImage thumbFilter(BufferedImage source, int width,
			int height) {

		BufferedImage thumb = new BufferedImage(width, height,
				BufferedImage.TYPE_4BYTE_ABGR);

		//处理缩放比例
		if (source.getWidth() / (double) width > source.getHeight()
				/ (double) height) {
			height = (int) ((double) width * source.getHeight() / source
					.getWidth());
		} else {
			width = (int) ((double) height * source.getWidth() / source
					.getHeight());
		}

		Graphics2D g = thumb.createGraphics();

		g.setColor(new Color(255, 255, 255, 255));

		//g.drawRect(0, 0, thumb.getWidth(), thumb.getHeight());
		Image buf = source.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		
		g.drawImage(buf, (thumb.getWidth() - width) / 2,
				(thumb.getHeight() - height) / 2, null);

		//g.setColor(new Color(255, 255, 255, 200));
		
		//设置平滑抗锯齿
//		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
//				RenderingHints.VALUE_ANTIALIAS_ON);
//		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
//				RenderingHints.VALUE_INTERPOLATION_BICUBIC);

		//g.drawString("By Robin", 0, 30);

		
		return thumb;
	}

	/**
	 * 读取图片
	 * @param in
	 * @param type png gif jpg
	 * @return
	 * @throws IOException
	 */
	public static BufferedImage readImage(InputStream in, String type)
			throws IOException {

		Iterator readers = ImageIO.getImageReadersByFormatName(type);
		ImageReader reader = (ImageReader) readers.next();

		//Object source; // File or InputStream
		ImageInputStream iis = ImageIO.createImageInputStream(in);

		reader.setInput(iis, true);

		BufferedImage img = reader.read(0);

		return img;

	}

	public static BufferedImage readImage(String fileName) throws IOException{
		String type = fileName.substring(fileName.lastIndexOf(".")+1);
		return readImage(new FileInputStream(fileName), type);
	}
	
	public static BufferedImage readImage(URL url) throws IOException{
		String type = url.toString().substring(url.toString().lastIndexOf(".")+1);
		return readImage(url.openStream(), type);
	}
	
	public static BufferedImage readImage(File file) throws IOException{
		String type = file.getName().substring(file.getName().lastIndexOf(".")+1);
		return readImage(new FileInputStream(file), type);
	}
	

	public static void writeImage(BufferedImage image,
			String filename) throws IOException {

		String type = filename.substring(filename.lastIndexOf(".")+1);
		writeImage(image, type, filename);
	}
	
	public static void writeImage(BufferedImage image, String type,
			String filename) throws IOException {
		File f = new File(filename);
		writeImage(image, type, f);
	}

	public static void writeImage(BufferedImage image, String type, File file)
			throws IOException {

		Iterator writers = ImageIO.getImageWritersByFormatName(type);
		ImageWriter writer = (ImageWriter) writers.next();

		//Once an ImageWriter has been obtained, its destination must be set to an ImageOutputStream:

		//File f = new File(filename);
		ImageOutputStream ios = ImageIO.createImageOutputStream(file);
		writer.setOutput(ios);

		//Finally, the image may be written to the output stream:

		writer.write(image);

	}

}

⌨️ 快捷键说明

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