bios

package net.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtil {

	public void test() throws FileNotFoundException {
		File file = new File("D:\\DDDDDtemp.jpg");
		OutputStream out = new FileOutputStream(file);
		int width = 100;
		int height = 100;
		BufferedImage bi = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		// 创建Graphics2D对象
		Graphics2D g = bi.createGraphics();
		// 填充背景为白色:
		g.setBackground(Color.BLUE);
		g.clearRect(0, 0, width, height);
		// 设置前景色:
		g.setColor(Color.RED);
		// 开始绘图, 绘制一条直线
		g.drawLine(0, 0, 99, 99);
		// 绘图完成,释放资源:
		g.dispose();
		bi.flush();

		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
		param.setQuality(1.0f, false);
		encoder.setJPEGEncodeParam(param);
		try {
			encoder.encode(bi);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	public static void main(String[] args) throws IOException {
		// ImageUtil iu = new ImageUtil();
		// iu.test();
		// PNG.test();
		// C3.test();
		PNG.test();
	}
}

class PNG {

	public static String test() throws IOException {
		int width = 100;
		int height = 100;

		BufferedImage bi = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		// 创建Graphics2D对象
		Graphics2D g = bi.createGraphics();

		// 填充背景为白色:
		g.setBackground(new Color(241, 241, 241));
		g.clearRect(0, 0, width, height);
		// 设置前景色:
		g.setColor(new Color(41, 200, 139));
		// 开始绘图, 绘制一条直线
		g.drawLine(0, 0, 99, 99);
		// 绘图完成,释放资源:
		g.dispose();
		bi.flush();

		/*
		 * byte[] bytes = new byte[1024]; ByteArrayInputStream bis = new
		 * ByteArrayInputStream(bytes);
		 */

		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		// 保存文件
		ImageIO.write(bi, "png", bos);

		byte[] bytes = bos.toByteArray();

		bytes = Base64.encodeBase64(bytes);

		StringBuffer sb = new StringBuffer();

		for (byte bt : bytes) {
			sb.append((char) bt);
		}
		String stt = "data:image/png;base64,";
		stt += sb.toString();
		return stt;
	}

	public static void test2() throws IOException {
		int width = 100;

		int height = 100;

		// 创建BufferedImage对象
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);

		// 获取Graphics2D
		Graphics2D g2d = image.createGraphics();

		// ---------- 增加下面的代码使得背景透明 -----------------
		image = g2d.getDeviceConfiguration().createCompatibleImage(width,
				height, Transparency.TRANSLUCENT);

		g2d.dispose();
		g2d = image.createGraphics();

		// ---------- 背景透明代码结束 -----------------

		// 画图
		g2d.setColor(new Color(255, 0, 0));
		g2d.setStroke(new BasicStroke(1));

		// g2d.draw
		// 释放对象
		g2d.dispose();

		// 保存文件
		ImageIO.write(image, "png", new File("D:\\Dtest.png"));
	}
}

class C3 {

	public static void test() {
		try {
			int width = 128;
			int height = 64;
			// 创建BufferedImage对象
			Font font = new Font("宋体", Font.PLAIN, 16);
			BufferedImage image = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			// 获取Graphics2D
			Graphics2D g2d = image.createGraphics();
			// 画图
			g2d.setBackground(new Color(255, 255, 255));
			g2d.setPaint(new Color(0, 0, 0));
			g2d.clearRect(0, 0, width, height);
			g2d.drawString("名称:娃哈哈纯净水", 0, 10);
			g2d.drawString("产地:浙江杭州", 0, 26);
			g2d.drawString("品牌:娃娃哈哈", 0, 42);
			g2d.drawString("单价:9876543210", 0, 58);
			g2d.setFont(font);
			// 释放对象
			g2d.dispose();
			// 保存文件
			ImageIO.write(image, "png", new File("D:/test.png"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

你可能感兴趣的:(ios)