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();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值