1
2
3
4 package com.buckosoft.PicMan.business.util;
5
6 import java.awt.image.BufferedImage;
7 import java.io.ByteArrayOutputStream;
8 import java.util.Date;
9 import java.util.Iterator;
10 import java.util.List;
11
12 import javax.imageio.ImageIO;
13 import javax.imageio.ImageWriter;
14 import javax.imageio.stream.ImageOutputStream;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 import com.buckosoft.PicMan.business.PicManFacade;
20 import com.buckosoft.PicMan.domain.JobLogEntry;
21 import com.buckosoft.PicMan.domain.Pic;
22 import com.buckosoft.PicMan.image.PicReader;
23
24
25
26
27
28 public class MD5SumUpdater {
29 protected final static Log log = LogFactory.getLog(MD5SumUpdater.class);
30
31 private int picProcessing = 0;
32 private PicManFacade pmf;
33
34
35
36
37 public void setPicMan(PicManFacade pmf) {
38 this.pmf = pmf;
39 }
40
41
42
43
44
45 public int getPicProcessing() {
46 return picProcessing;
47 }
48
49
50
51 public void run() {
52 List<Pic> pics = pmf.getDB().getPics();
53 Iterator<Pic> iter = pics.iterator();
54 int picCount = pics.size();
55 picProcessing = 0;
56 JobLogEntry jle = new JobLogEntry();
57 jle.setType(JobLogEntry.MD5SUM);
58 pmf.addJobToLog(jle);
59 Pic pic;
60 while (iter.hasNext()) {
61 picProcessing++;
62 int cp = picProcessing * 100 / picCount;
63 jle.setName("" + cp + "%");
64 pic = iter.next();
65 calculateMD5Sum(pmf, pic);
66
67 pmf.getDB().updatePic(pic);
68 }
69 jle.setEndTime(new Date());
70 }
71
72 public static void calculateMD5Sum(PicManFacade pmf, Pic pic) {
73
74
75 String md5;
76
77 try {
78 BufferedImage bi = pmf.getPicReader().readPic(pic);
79 ByteArrayOutputStream out = new ByteArrayOutputStream();
80 ImageOutputStream ios = ImageIO.createImageOutputStream(out);
81 Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
82 ImageWriter writer = (ImageWriter)writers.next();
83 writer.setOutput(ios);
84 writer.write(bi);
85 byte[] data = out.toByteArray();
86 md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
87
88
89
90
91
92 } catch (Exception e) {
93
94
95 log.warn("md5 file error on " + PicReader.getFullPath(pmf, pic));
96 pic.setMd5(0);
97 return;
98 }
99 long l = Long.parseLong(md5.substring(0, 8), 16);
100 pic.setMd5(l);
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 }
123 }