如何用java 给图片添加logo,如何读取本地图片,如何写入本地磁盘,文件读取.

主程序

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;


public class April6_1
{
public static int HEIGHT = 150;

public static String DATA_PATH = "C:/1.txt";

public static String IMAGE_TO_WRITE_PATH = "D:/1.jpg";

public static String IMAGE_SOURCE_FILE_DIRECTORY = "E:/data/DrWang/图片添加标注";

public static String IMAGE_OUTPUT_PATH = "";

public static int X_COORDINATE = 130;
public static int Y_COORDINATE = 1200;

public static void main(String[] args) {
try {


// if (args.length != 7) {
// System.out.println("ARGUMENTS'S LENGTH:" + args.length);
// System.out.println("COUNT OF ARGUMENTS SHOULD BE 7.");
// System.out.println("USE:java April6_2 [需要转化的图片的目录] [数据文件路径] [放置在图片的X坐标] [放置在图片的Y坐标] [图片宽度] [图片高度] [输出路径]");
// return;
// }
// if (args[0].charAt(args[0].length() - 1) != '/') {
// IMAGE_SOURCE_FILE_DIRECTORY = args[0] + "/";
// } else {
// IMAGE_SOURCE_FILE_DIRECTORY = args[0];
// }
// DATA_PATH = args[1];
// X_COORDINATE = Integer.parseInt(args[2]);
// Y_COORDINATE = Integer.parseInt(args[3]);
// int iWideth = Integer.parseInt(args[4]);
// int iHeight = Integer.parseInt(args[5]);
// if (args[6].charAt(args[6].length() - 1) != '/')
// IMAGE_OUTPUT_PATH = args[6] + "/";
// else
// IMAGE_OUTPUT_PATH = args[6];

IMAGE_OUTPUT_PATH = "E:\\data\\DrWang\\图片添加标注\\处理好的图片\\";
int iWideth = 500;
int iHeight = 100;


IMAGE_TO_WRITE_PATH = IMAGE_SOURCE_FILE_DIRECTORY + "/toWrite.png";
List<RGBColor> data = getColor(DATA_PATH);
BufferedImage view_image = drawImage(data, iWideth, iHeight);
BufferedImage charactor_image = drawCharactor(iWideth);
BufferedImage im = combineImages(charactor_image, view_image);
writeToDisk(im, IMAGE_TO_WRITE_PATH, "COMPARE IMAGE ");


String path = IMAGE_SOURCE_FILE_DIRECTORY;
List<String> fileNames = getNames(path, "jpg");
String newPath = IMAGE_OUTPUT_PATH;
File f = new File(newPath);
if (!f.exists()) {
f.mkdir();
}
for (String i : fileNames) {
if (i != null) {
try {

BufferedImage img = ImageIO.read(new File(path + "/"
+ i));
i = changeFileName(i);
if (i != null) {
Graphics2D gra = img.createGraphics();
gra.drawImage(im, null, X_COORDINATE, Y_COORDINATE);
writeToDisk(img, newPath + i, i + " ");
gra.drawImage(drawCharactor(i.replaceAll(
"\\.cn_01\\.jpg$", "")), null, 1000, 1350);
writeToDisk(img, newPath + i, i + " ");
}

} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("USE:java April6_2 [需要转化的图片的目录] [数据文件路径] [放置在图片的X坐标] [放置在图片的Y坐标] [图片宽度] [图片高度] [输出路径]");
}

}

static List<String> getNames(String path, String type) {
List<String> fileSet = new ArrayList<String>();
File[] files = new File(path).listFiles();
for (File file : files) {
// 由于List的元素是不重复的,不用做重复判断,直接放入
fileSet.add(type == null ? file.getName() : (file.getName()
.indexOf(type) == -1 ? null : file.getName()));
}
// 由于List允许null,所以要去除
fileSet.remove(null);
// 打印出Set的内容(需要的数据)
return fileSet;
}

static BufferedImage getImageByRGB(List<RGBColor> data, BufferedImage im) {
int length = data.size();
for (int i = 0; i < length * 2; i++) {
RGBColor c = data.get(i / 2);
for (int j = 0; j < HEIGHT / 3; j++) {
im
.setRGB(i, j, (255 << 16) + (255 << 8) + (255)
+ (0xff000000));
}
for (int j = HEIGHT / 3; j < HEIGHT; j++) {
im.setRGB(i, j, (c.getR() << 16) + (c.getG() << 8) + (c.getB())
+ (0xff000000));
}
}
return im;
}

static BufferedImage drawImage(List<RGBColor>data,int height,int getWidth) {
int width = data.size();
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < width; i++) {
RGBColor c = data.get(i);
for (int j = 0; j < height; j++) {
bi.setRGB(i, j, (c.getR() << 16) + (c.getG() << 8) + (c.getB())
+ (0xff000000));
}
}
BufferedImage tag = new BufferedImage(height,getWidth, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(bi, 0, 0,height , getWidth, null); // 绘制缩小后的图
return tag;
}

static BufferedImage drawCharactor(int width) {
BufferedImage im = new BufferedImage(width, 50, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < width; i++) {
for (int j = 0; j < 50; j++) {
im.setRGB(i, j, (255 << 16) + (255 << 8) + (255) + (0xff000000));
}
}
Graphics g = im.getGraphics();
g.setFont(new Font("Serif", Font.BOLD, 40));
g.setColor(Color.black);
g.drawString("-50°C", im.getMinX() + 10, 40);
g.drawString("50°C", im.getWidth() - g.getFont().getSize() * 2-g.getFont().getSize()/2, 40);
return im;
}

static BufferedImage combineImages(BufferedImage i1, BufferedImage i2) {
int height = i1.getHeight() + i2.getHeight();
BufferedImage result = new BufferedImage(i1.getWidth(), height, BufferedImage.TYPE_INT_RGB);
Graphics2D gra = result.createGraphics();
gra.drawImage(i1, null, 0, 0);
gra.drawImage(i2, null, 0, i1.getHeight());
return result;
}

static List<RGBColor> getColor(String URL) {
List<RGBColor> result = new ArrayList<RGBColor>();
try {
FileReader reader = new FileReader(URL);
BufferedReader bfReader = new BufferedReader(reader);
String text = null;
while ((text = bfReader.readLine()) != null) {
result.add(strToColor(text.trim()));
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

static RGBColor strToColor(String text) {
RGBColor c = new RGBColor();
c.setIndex(Integer.parseInt(text.split(":")[0]));
String[] str = text.split(":")[1].split(",");
c.setR(Integer.parseInt(str[0].trim()));
c.setB(Integer.parseInt(str[1].trim()));
c.setG(Integer.parseInt(str[2].trim()));
return c;
}

static void writeToDisk(BufferedImage im, String path, String file) {
File f = new File(path);
try {
ImageIO.write(im, "jpg", f);
im.flush();
System.out.println(file + "success!");
} catch (IOException e) {
System.out.println(file + "failed!");
}
}

static String changeFileName(String s) {
return Pattern.compile("_01\\.jpg$").matcher(s).find() ? null : s
.replaceAll("\\.jpg$", "_01.jpg");
}

static BufferedImage drawCharactor(String text) {
BufferedImage im = new BufferedImage(850, 50, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < 600; i++) {
for (int j = 0; j < 50; j++) {
im.setRGB(i, j, (0 << 16) + (0 << 8) + (0) + (0xff000000));
}
}
Graphics g = im.getGraphics();
g.setFont(new Font("Serif", Font.BOLD, 50));
g.setColor(Color.white);
g.drawString(text, im.getMinX() + 10, 40);
return im;
}
}



用到的类

public class RGBColor {
private int index;
private int R;
private int G;
private int B;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getR() {
return R;
}
public void setR(int r) {
R = r;
}
public int getG() {
return G;
}
public void setG(int g) {
G = g;
}
public int getB() {
return B;
}
public void setB(int b) {
B = b;
}
}


可以在linux下直接编译运行,带适当的参数即可
其中需要先编译 RGBColor.java 然后再编译 April6_1.java,代码运行没问题.

数据文件内容如下
 0:0,0,255,255
1: 0,15,240,255
2: 1,16,240,255
3: 3,18,240,255
4: 4,19,240,255
5: 6,21,240,255
6: 8,22,240,255
7: 9,24,240,255
8: 11,25,240,255
9: 12,27,240,255
10: 14,28,240,255
11: 16,30,240,255
12: 17,31,240,255
13: 19,33,240,255
14: 21,34,240,255
15: 22,36,240,255
16: 24,37,240,255
17: 25,39,240,255
18: 27,40,240,255
19: 29,42,240,255
20: 30,43,240,255
21: 32,45,240,255
22: 34,47,240,255
23: 35,48,240,255
24: 37,50,240,255
25: 38,51,240,255
26: 40,53,240,255
27: 42,54,240,255
28: 43,56,240,255
29: 45,57,240,255
30: 46,59,240,255
31: 48,60,240,255
32: 50,62,240,255
33: 51,63,240,255
34: 53,65,240,255
35: 55,66,240,255
36: 56,68,240,255
37: 58,69,240,255
38: 59,71,240,255
39: 61,72,240,255
40: 63,74,240,255
41: 64,75,240,255
42: 66,77,240,255
43: 68,79,240,255
44: 69,80,240,255
45: 71,82,240,255
46: 72,83,240,255
47: 74,85,240,255
48: 76,86,240,255
49: 77,88,240,255
50: 79,89,240,255
51: 80,91,240,255
52: 82,92,240,255
53: 84,94,240,255
54: 85,95,240,255
55: 87,97,240,255
56: 89,98,240,255
57: 90,100,240,255
58: 92,101,240,255
59: 93,103,240,255
60: 95,104,240,255
61: 97,106,240,255
62: 98,107,240,255
63: 100,109,240,255
64: 102,111,240,255
65: 101,112,237,255
66: 100,113,234,255
67: 98,114,231,255
68: 97,116,228,255
69: 95,117,225,255
70: 94,118,222,255
71: 93,120,219,255
72: 91,121,216,255
73: 90,122,213,255
74: 88,124,210,255
75: 87,125,207,255
76: 86,126,204,255
77: 84,128,201,255
78: 83,129,198,255
79: 81,130,195,255
80: 80,132,192,255
81: 79,133,189,255
82: 77,134,186,255
83: 76,135,183,255
84: 74,137,180,255
85: 73,138,177,255
86: 72,139,174,255
87: 70,141,171,255
88: 69,142,168,255
89: 67,143,165,255
90: 66,145,162,255
91: 65,146,159,255
92: 63,147,156,255
93: 62,149,153,255
94: 60,150,150,255
95: 59,151,147,255
96: 57,153,144,255
97: 57,156,147,255
98: 57,159,150,255
99: 57,162,153,255
100: 57,166,156,255
101: 57,169,159,255
102: 58,172,162,255
103: 58,176,165,255
104: 58,179,168,255
105: 58,182,171,255
106: 58,185,174,255
107: 59,189,178,255
108: 59,192,181,255
109: 59,195,184,255
110: 59,199,187,255
111: 59,202,190,255
112: 60,205,193,255
113: 60,208,196,255
114: 60,212,199,255
115: 60,215,202,255
116: 60,218,205,255
117: 61,222,209,255
118: 61,225,212,255
119: 61,228,215,255
120: 61,231,218,255
121: 61,235,221,255
122: 62,238,224,255
123: 62,241,227,255
124: 62,245,230,255
125: 62,248,233,255
126: 62,251,236,255
127: 63,255,240,255
128: 63,252,234,255
129: 63,249,228,255
130: 63,245,222,255
131: 63,242,216,255
132: 63,238,210,255
133: 62,235,203,255
134: 62,231,197,255
135: 62,228,191,255
136: 62,224,185,255
137: 62,221,179,255
138: 61,217,172,255
139: 61,214,166,255
140: 61,210,160,255
141: 61,207,154,255
142: 61,203,148,255
143: 60,200,141,255
144: 60,196,135,255
145: 60,193,129,255
146: 60,189,123,255
147: 60,186,117,255
148: 59,182,110,255
149: 59,179,104,255
150: 59,175,98,255
151: 59,172,92,255
152: 59,168,86,255
153: 58,165,79,255
154: 58,161,73,255
155: 58,158,67,255
156: 58,154,61,255
157: 58,151,55,255
158: 57,147,48,255
159: 57,149,48,255
160: 57,152,48,255
161: 57,155,48,255
162: 57,158,48,255
163: 57,161,48,255
164: 58,164,48,255
165: 58,167,48,255
166: 58,170,48,255
167: 58,173,48,255
168: 58,176,48,255
169: 59,179,48,255
170: 59,181,48,255
171: 59,184,48,255
172: 59,187,48,255
173: 59,190,48,255
174: 59,193,48,255
175: 60,196,48,255
176: 60,199,48,255
177: 60,202,48,255
178: 60,205,48,255
179: 60,208,48,255
180: 61,211,48,255
181: 61,213,48,255
182: 61,216,48,255
183: 61,219,48,255
184: 61,222,48,255
185: 61,225,48,255
186: 62,228,48,255
187: 62,231,48,255
188: 62,234,48,255
189: 62,237,48,255
190: 62,240,48,255
191: 63,243,48,255
192: 68,242,49,255
193: 74,241,50,255
194: 80,239,52,255
195: 85,238,53,255
196: 91,237,55,255
197: 97,235,56,255
198: 103,234,58,255
199: 108,233,59,255
200: 114,231,61,255
201: 120,230,62,255
202: 126,228,64,255
203: 131,227,65,255
204: 137,226,66,255
205: 143,224,68,255
206: 148,223,69,255
207: 154,222,71,255
208: 160,220,72,255
209: 166,219,74,255
210: 171,218,75,255
211: 177,216,77,255
212: 183,215,78,255
213: 189,213,80,255
214: 194,212,81,255
215: 200,211,82,255
216: 206,209,84,255
217: 211,208,85,255
218: 217,207,87,255
219: 223,205,88,255
220: 229,204,90,255
221: 234,203,91,255
222: 240,201,93,255
223: 246,200,94,255
224: 252,198,96,255
225: 252,192,93,255
226: 252,186,90,255
227: 251,179,87,255
228: 251,173,84,255
229: 251,167,81,255
230: 250,160,78,255
231: 250,154,75,255
232: 249,147,72,255
233: 249,141,69,255
234: 249,135,66,255
235: 248,128,62,255
236: 248,122,59,255
237: 247,115,56,255
238: 247,109,53,255
239: 247,103,50,255
240: 246,96,47,255
241: 246,90,44,255
242: 246,84,41,255
243: 245,77,38,255
244: 245,71,35,255
245: 244,64,31,255
246: 244,58,28,255
247: 244,52,25,255
248: 243,45,22,255
249: 243,39,19,255
250: 242,32,16,255
251: 242,26,13,255
252: 242,20,10,255
253: 241,13,7,255
254: 241,7,4,255
255: 240,0,0,255
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值