| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package net.sf.webphotos.tools; |
| 17 | |
|
| 18 | |
import com.sun.image.codec.jpeg.JPEGCodec; |
| 19 | |
import com.sun.image.codec.jpeg.JPEGImageEncoder; |
| 20 | |
import java.awt.*; |
| 21 | |
import java.awt.image.BufferedImage; |
| 22 | |
import java.io.File; |
| 23 | |
import java.io.FileOutputStream; |
| 24 | |
import java.io.IOException; |
| 25 | |
import java.util.Iterator; |
| 26 | |
import javax.imageio.ImageIO; |
| 27 | |
import javax.imageio.ImageReader; |
| 28 | |
import javax.imageio.stream.ImageInputStream; |
| 29 | |
import javax.swing.ImageIcon; |
| 30 | |
import net.sf.webphotos.BancoImagem; |
| 31 | |
import net.sf.webphotos.util.Util; |
| 32 | |
import org.apache.commons.configuration.Configuration; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0 | public final class Thumbnail { |
| 40 | |
|
| 41 | |
|
| 42 | |
private static int t1, t2, t3, t4; |
| 43 | |
|
| 44 | |
private static String marcadagua, mdTeste; |
| 45 | |
private static String texto; |
| 46 | |
private static int mdPosicao, mdMargem, mdTransparencia; |
| 47 | |
private static String txFamilia; |
| 48 | |
private static int txTamanho, txPosicao, txMargem, txEstilo; |
| 49 | |
private static Color txCorFrente, txCorFundo; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
private static void inicializar() { |
| 59 | |
|
| 60 | |
|
| 61 | 3 | Configuration c = Util.getConfig(); |
| 62 | |
|
| 63 | |
|
| 64 | 3 | t1 = c.getInt("thumbnail1"); |
| 65 | 3 | t2 = c.getInt("thumbnail2"); |
| 66 | 3 | t3 = c.getInt("thumbnail3"); |
| 67 | 3 | t4 = c.getInt("thumbnail4"); |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
try { |
| 72 | 3 | marcadagua = c.getString("marcadagua"); |
| 73 | 3 | mdPosicao = c.getInt("marcadagua.posicao"); |
| 74 | 3 | mdMargem = c.getInt("marcadagua.margem"); |
| 75 | 3 | mdTransparencia = c.getInt("marcadagua.transparencia"); |
| 76 | 3 | mdTeste = c.getString("marcadagua.teste"); |
| 77 | 0 | } catch (Exception ex) { |
| 78 | 0 | ex.printStackTrace(Util.err); |
| 79 | 3 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
try { |
| 83 | 3 | texto = c.getString("texto"); |
| 84 | 3 | txPosicao = c.getInt("texto.posicao"); |
| 85 | 3 | txMargem = c.getInt("texto.margem"); |
| 86 | 3 | txTamanho = c.getInt("texto.tamanho"); |
| 87 | 3 | txEstilo = c.getInt("texto.estilo"); |
| 88 | 3 | txFamilia = c.getString("texto.familia"); |
| 89 | |
|
| 90 | 3 | String[] aux = c.getStringArray("texto.corFrente"); |
| 91 | 3 | txCorFrente = new Color(Integer.parseInt(aux[0]), Integer.parseInt(aux[1]), Integer.parseInt(aux[2])); |
| 92 | 3 | aux = c.getStringArray("texto.corFundo"); |
| 93 | 3 | txCorFundo = new Color(Integer.parseInt(aux[0]), Integer.parseInt(aux[1]), Integer.parseInt(aux[2])); |
| 94 | 0 | } catch (Exception ex) { |
| 95 | 0 | ex.printStackTrace(Util.err); |
| 96 | 3 | } |
| 97 | |
|
| 98 | 3 | } |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public static void makeThumbs(String caminhoCompletoImagem) { |
| 113 | |
|
| 114 | |
String diretorio, arquivo; |
| 115 | 6 | if (t1 == 0) { |
| 116 | 3 | inicializar(); |
| 117 | |
} |
| 118 | |
|
| 119 | |
try { |
| 120 | 6 | File f = new File(caminhoCompletoImagem); |
| 121 | 6 | if (!f.isFile() || !f.canRead()) { |
| 122 | 0 | Util.err.println("[Thumbnail.makeThumbs]/ERRO: Erro no caminho do arquivo " + caminhoCompletoImagem + " incorreto"); |
| 123 | 0 | return; |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | 6 | if (getFormatName(f) == null) { |
| 128 | 0 | Util.err.println("[Thumbnail.makeThumbs]/ERRO: Foto Corrompida"); |
| 129 | 0 | return; |
| 130 | |
} else { |
| 131 | 6 | Util.out.println("[Thumbnail.makeThumbs]/INFO: Foto Ok!"); |
| 132 | |
} |
| 133 | |
|
| 134 | 6 | diretorio = f.getParent(); |
| 135 | 6 | arquivo = f.getName(); |
| 136 | |
|
| 137 | 6 | ImageIcon ii = new ImageIcon(f.getCanonicalPath()); |
| 138 | 6 | Image i = ii.getImage(); |
| 139 | |
|
| 140 | |
Image tumb1, tumb2, tumb3, tumb4; |
| 141 | |
|
| 142 | |
|
| 143 | 6 | int iWidth = i.getWidth(null); |
| 144 | 6 | int iHeight = i.getHeight(null); |
| 145 | |
|
| 146 | |
|
| 147 | 6 | if (iWidth > iHeight) { |
| 148 | 6 | tumb1 = i.getScaledInstance(t1, (t1 * iHeight) / iWidth, Image.SCALE_SMOOTH); |
| 149 | 6 | tumb2 = i.getScaledInstance(t2, (t2 * iHeight) / iWidth, Image.SCALE_SMOOTH); |
| 150 | 6 | tumb3 = i.getScaledInstance(t3, (t3 * iHeight) / iWidth, Image.SCALE_SMOOTH); |
| 151 | 6 | tumb4 = i.getScaledInstance(t4, (t4 * iHeight) / iWidth, Image.SCALE_SMOOTH); |
| 152 | |
|
| 153 | |
|
| 154 | |
} else { |
| 155 | 0 | tumb1 = i.getScaledInstance((t1 * iWidth) / iHeight, t1, Image.SCALE_SMOOTH); |
| 156 | 0 | tumb2 = i.getScaledInstance((t2 * iWidth) / iHeight, t2, Image.SCALE_SMOOTH); |
| 157 | 0 | tumb3 = i.getScaledInstance((t3 * iWidth) / iHeight, t3, Image.SCALE_SMOOTH); |
| 158 | 0 | tumb4 = i.getScaledInstance((t4 * iWidth) / iHeight, t4, Image.SCALE_SMOOTH); |
| 159 | |
|
| 160 | |
|
| 161 | |
} |
| 162 | |
|
| 163 | 6 | tumb4 = estampar(tumb4); |
| 164 | |
|
| 165 | 6 | Util.log("Salvando Imagens"); |
| 166 | |
|
| 167 | 6 | save(tumb1, diretorio + File.separator + "_a" + arquivo); |
| 168 | 6 | save(tumb2, diretorio + File.separator + "_b" + arquivo); |
| 169 | 6 | save(tumb3, diretorio + File.separator + "_c" + arquivo); |
| 170 | 6 | save(tumb4, diretorio + File.separator + "_d" + arquivo); |
| 171 | |
|
| 172 | 0 | } catch (Exception e) { |
| 173 | 0 | Util.err.println("[Thumbnail.makeThumbs]/ERRO: Inesperado - " + e.getMessage()); |
| 174 | 0 | e.printStackTrace(Util.err); |
| 175 | 6 | } |
| 176 | 6 | } |
| 177 | |
|
| 178 | |
private static Image estampar(Image im) { |
| 179 | |
try { |
| 180 | 6 | Image temp = new ImageIcon(im).getImage(); |
| 181 | |
|
| 182 | 6 | BufferedImage buf = new BufferedImage(temp.getWidth(null), |
| 183 | |
temp.getHeight(null), |
| 184 | |
BufferedImage.TYPE_INT_RGB); |
| 185 | |
|
| 186 | 6 | Graphics2D g2 = (Graphics2D) buf.getGraphics(); |
| 187 | |
|
| 188 | 6 | g2.drawImage(temp, 0, 0, null); |
| 189 | 6 | g2.setBackground(Color.BLUE); |
| 190 | |
|
| 191 | 6 | Dimension dimensaoFoto = new Dimension(im.getWidth(null), im.getHeight(null)); |
| 192 | |
|
| 193 | |
|
| 194 | 6 | if (texto != null) { |
| 195 | 6 | Util.out.println("aplicando texto " + texto); |
| 196 | |
|
| 197 | 6 | Font fonte = new Font(txFamilia, txEstilo, txTamanho); |
| 198 | 6 | g2.setFont(fonte); |
| 199 | 6 | FontMetrics fm = g2.getFontMetrics(fonte); |
| 200 | 6 | Dimension dimensaoTexto = new Dimension(fm.stringWidth(texto), fm.getHeight()); |
| 201 | 6 | Point posTexto = calculaPosicao(dimensaoFoto, dimensaoTexto, txMargem, txPosicao); |
| 202 | |
|
| 203 | 6 | g2.setPaint(txCorFundo); |
| 204 | 6 | g2.drawString(texto, (int) posTexto.getX() + 1, (int) posTexto.getY() + 1 + fm.getHeight()); |
| 205 | 6 | g2.setPaint(txCorFrente); |
| 206 | 6 | g2.drawString(texto, (int) posTexto.getX(), (int) posTexto.getY() + fm.getHeight()); |
| 207 | |
} |
| 208 | |
|
| 209 | |
|
| 210 | 6 | if (marcadagua != null) { |
| 211 | 6 | Image marca = new ImageIcon(marcadagua).getImage(); |
| 212 | 6 | int rule = AlphaComposite.SRC_OVER; |
| 213 | 6 | float alpha = (float) mdTransparencia / 100; |
| 214 | 6 | Point pos = calculaPosicao(dimensaoFoto, |
| 215 | |
new Dimension(marca.getWidth(null), marca.getHeight(null)), |
| 216 | |
mdMargem, mdPosicao); |
| 217 | |
|
| 218 | 6 | g2.setComposite(AlphaComposite.getInstance(rule, alpha)); |
| 219 | 6 | g2.drawImage(marca, (int) pos.getX(), (int) pos.getY(), null); |
| 220 | |
} |
| 221 | |
|
| 222 | 6 | g2.dispose(); |
| 223 | |
|
| 224 | 6 | return Toolkit.getDefaultToolkit().createImage(buf.getSource()); |
| 225 | 0 | } catch (Exception e) { |
| 226 | 0 | return im; |
| 227 | |
} |
| 228 | |
} |
| 229 | |
|
| 230 | |
private static boolean save(Image thumbnail, String nmArquivo) { |
| 231 | |
try { |
| 232 | |
|
| 233 | |
|
| 234 | 24 | Image temp = new ImageIcon(thumbnail).getImage(); |
| 235 | |
|
| 236 | |
|
| 237 | 24 | BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), |
| 238 | |
temp.getHeight(null), BufferedImage.TYPE_INT_RGB); |
| 239 | |
|
| 240 | |
|
| 241 | 24 | Graphics g = bufferedImage.createGraphics(); |
| 242 | |
|
| 243 | |
|
| 244 | 24 | g.setColor(Color.white); |
| 245 | 24 | g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null)); |
| 246 | 24 | g.drawImage(temp, 0, 0, null); |
| 247 | 24 | g.dispose(); |
| 248 | |
|
| 249 | |
|
| 250 | 24 | File file = new File(nmArquivo); |
| 251 | |
|
| 252 | 24 | if (file.exists()) { |
| 253 | 24 | Util.out.println("Redefinindo a Imagem: " + nmArquivo); |
| 254 | 24 | file.delete(); |
| 255 | 24 | file = new File(nmArquivo); |
| 256 | |
} |
| 257 | 24 | FileOutputStream out = new FileOutputStream(file); |
| 258 | |
|
| 259 | |
|
| 260 | 24 | JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); |
| 261 | 24 | com.sun.image.codec.jpeg.JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage); |
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | 24 | param.setQuality(1.0f, true); |
| 267 | 24 | encoder.setJPEGEncodeParam(param); |
| 268 | 24 | encoder.encode(bufferedImage); |
| 269 | 24 | return true; |
| 270 | 0 | } catch (IOException ioex) { |
| 271 | 0 | ioex.printStackTrace(Util.err); |
| 272 | 0 | return false; |
| 273 | |
} |
| 274 | |
} |
| 275 | |
|
| 276 | |
private static Point calculaPosicao(Dimension dimensaoExt, |
| 277 | |
Dimension dimensaoInt, int margem, int posicao) { |
| 278 | 12 | int x = 0, y = 0; |
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | 12 | int x1 = margem; |
| 285 | 12 | int x2 = (int) ((float) (dimensaoExt.width - dimensaoInt.width) / 2); |
| 286 | 12 | int x3 = dimensaoExt.width - dimensaoInt.width - margem; |
| 287 | |
|
| 288 | 12 | int y1 = margem; |
| 289 | 12 | int y2 = (int) ((float) (dimensaoExt.height - dimensaoInt.height) / 2); |
| 290 | 12 | int y3 = dimensaoExt.height - dimensaoInt.height - margem; |
| 291 | |
|
| 292 | 12 | if (posicao == 1) { |
| 293 | 0 | x = x1; |
| 294 | 0 | y = y1; |
| 295 | 12 | } else if (posicao == 2) { |
| 296 | 0 | x = x2; |
| 297 | 0 | y = y1; |
| 298 | 12 | } else if (posicao == 3) { |
| 299 | 0 | x = x3; |
| 300 | 0 | y = y1; |
| 301 | 12 | } else if (posicao == 4) { |
| 302 | 0 | x = x1; |
| 303 | 0 | y = y2; |
| 304 | 12 | } else if (posicao == 5) { |
| 305 | 0 | x = x2; |
| 306 | 0 | y = y2; |
| 307 | 12 | } else if (posicao == 6) { |
| 308 | 0 | x = x3; |
| 309 | 0 | y = y2; |
| 310 | 12 | } else if (posicao == 7) { |
| 311 | 6 | x = x1; |
| 312 | 6 | y = y3; |
| 313 | 6 | } else if (posicao == 8) { |
| 314 | 6 | x = x2; |
| 315 | 6 | y = y3; |
| 316 | 0 | } else if (posicao == 9) { |
| 317 | 0 | x = x3; |
| 318 | 0 | y = y3; |
| 319 | |
} |
| 320 | |
|
| 321 | 12 | return new Point(x, y); |
| 322 | |
} |
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
private static String getFormatInFile(File f) { |
| 332 | 0 | return getFormatName(f); |
| 333 | |
} |
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
private static String getFormatFromStream(java.io.InputStream is) { |
| 338 | 0 | return getFormatName(is); |
| 339 | |
} |
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
private static String getFormatName(Object o) { |
| 345 | |
try { |
| 346 | |
|
| 347 | 6 | ImageInputStream iis = ImageIO.createImageInputStream(o); |
| 348 | |
|
| 349 | |
|
| 350 | 6 | Iterator<ImageReader> iter = ImageIO.getImageReaders(iis); |
| 351 | 6 | if (!iter.hasNext()) { |
| 352 | |
|
| 353 | 0 | return null; |
| 354 | |
} |
| 355 | |
|
| 356 | |
|
| 357 | 6 | ImageReader reader = iter.next(); |
| 358 | |
|
| 359 | |
|
| 360 | 6 | iis.close(); |
| 361 | |
|
| 362 | |
|
| 363 | 6 | return reader.getFormatName(); |
| 364 | 0 | } catch (Exception e) { |
| 365 | |
|
| 366 | 0 | return null; |
| 367 | |
} |
| 368 | |
} |
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
public static void main(String[] args) { |
| 378 | 0 | makeThumbs("c:/temp/167.jpg"); |
| 379 | 0 | makeThumbs("d:/bancoImagem/81/312.jpg"); |
| 380 | 0 | makeThumbs("d:/bancoImagem/81/313.jpg"); |
| 381 | 0 | makeThumbs("d:/bancoImagem/81/314.jpg"); |
| 382 | 0 | makeThumbs("d:/bancoImagem/81/315.jpg"); |
| 383 | 0 | makeThumbs("D:/bancoImagem/460/2072.jpg"); |
| 384 | 0 | executaLote(); |
| 385 | 0 | } |
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
public static void executaLote() { |
| 393 | 0 | net.sf.webphotos.BancoImagem db = net.sf.webphotos.BancoImagem.getBancoImagem(); |
| 394 | |
|
| 395 | |
try { |
| 396 | 0 | db.configure("jdbc:mysql://localhost/test", "com.mysql.jdbc.Driver"); |
| 397 | 0 | BancoImagem.login(); |
| 398 | 0 | java.sql.Connection conn = BancoImagem.getConnection(); |
| 399 | 0 | java.sql.Statement st = conn.createStatement(); |
| 400 | 0 | java.sql.ResultSet rs = st.executeQuery("select * from fotos"); |
| 401 | |
|
| 402 | |
int albumID, fotoID; |
| 403 | |
String caminho; |
| 404 | |
|
| 405 | 0 | while (rs.next()) { |
| 406 | 0 | albumID = rs.getInt("albumID"); |
| 407 | 0 | fotoID = rs.getInt("fotoID"); |
| 408 | 0 | caminho = "d:/bancoImagem/" + albumID + "/" + fotoID + ".jpg"; |
| 409 | 0 | makeThumbs(caminho); |
| 410 | 0 | Util.out.println(caminho); |
| 411 | |
} |
| 412 | |
|
| 413 | 0 | rs.close(); |
| 414 | 0 | st.close(); |
| 415 | 0 | conn.close(); |
| 416 | |
|
| 417 | 0 | } catch (Exception e) { |
| 418 | 0 | e.printStackTrace(Util.err); |
| 419 | 0 | } |
| 420 | 0 | } |
| 421 | |
} |