1 4 package com.openedit.archive; 5 6 import java.awt.Graphics2D ; 7 import java.awt.image.BufferedImage ; 8 import java.io.ByteArrayInputStream ; 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.FileNotFoundException ; 12 import java.io.FileOutputStream ; 13 import java.io.FilenameFilter ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.OutputStream ; 17 import java.util.zip.ZipEntry ; 18 import java.util.zip.ZipInputStream ; 19 import java.util.zip.ZipOutputStream ; 20 21 import javax.imageio.ImageIO ; 22 23 import com.openedit.OpenEditException; 24 import com.openedit.util.FileUtils; 25 import com.openedit.util.OutputFiller; 26 27 32 public class WatermarkMaker 33 { 34 protected File fieldWaterMark; 35 protected BufferedImage fieldBufferedWatermark; 36 37 public void addWaterMark(InputStream in, OutputStream out) throws IOException 38 { 39 40 BufferedImage original = ImageIO.read(in); FileUtils.safeClose(in); 42 BufferedImage result = new BufferedImage (original.getWidth(), 43 original.getHeight(), 44 BufferedImage.TYPE_INT_RGB); 45 46 Graphics2D g = result.createGraphics(); 47 g.drawImage(original, 0, 0, null); 48 g.drawImage(getBufferedWatermark(), 0, 0, null); 49 50 ImageIO.write(result, "jpg", out); 51 out.flush(); 52 } 53 54 55 public File getWaterMark() 56 { 57 return fieldWaterMark; 58 } 59 60 61 public void setWaterMark(File inWaterMark) 62 { 63 fieldWaterMark = inWaterMark; 64 setBufferedWatermark(null); 65 } 66 67 68 protected BufferedImage getBufferedWatermark() throws IOException 69 { 70 if ( fieldBufferedWatermark == null) 71 { 72 fieldBufferedWatermark = ImageIO.read(getWaterMark()); 73 } 74 return fieldBufferedWatermark; 75 } 76 77 78 protected void setBufferedWatermark(BufferedImage inBufferedWatermark) 79 { 80 fieldBufferedWatermark = inBufferedWatermark; 81 } 82 83 84 public void addWaterMarkToZip(InputStream inStream, OutputStream inToBrowser) throws Exception 85 { 86 File tmpDir = unzipToTmp(inStream); 88 applyWaterMark(tmpDir); 89 90 ZipOutputStream finalZip = new ZipOutputStream (inToBrowser); 91 addToZip(tmpDir,finalZip); 92 finalZip.close(); 93 94 95 } 96 97 98 private void addToZip(File inTmpDir, ZipOutputStream inFinalZip) throws Exception 99 { 100 if( inTmpDir.isDirectory()) 101 { 102 File [] files = inTmpDir.listFiles(); 103 for (int i = 0; i < files.length; i++) 104 { 105 addToZip(files[i],inFinalZip); 106 } 107 } 108 else 109 { 110 ZipEntry entry = new ZipEntry ( inTmpDir.getName() ); 111 entry.setSize( inTmpDir.length() ); 112 entry.setTime( inTmpDir.lastModified() ); 113 FileInputStream fis = new FileInputStream ( inTmpDir ); 114 inFinalZip.putNextEntry( entry ); 115 try 116 { 117 new OutputFiller().fill( fis, inFinalZip ); 118 } 119 finally 120 { 121 fis.close(); 122 } 123 inFinalZip.closeEntry(); 124 } 125 } 126 127 128 private void applyWaterMark(File inTmpDir) throws IOException 129 { 130 if ( inTmpDir.isDirectory()) 131 { 132 File [] files = inTmpDir.listFiles(new FilenameFilter () 133 { 134 public boolean accept(File dir, String name) { if ( name.toLowerCase().endsWith(".jpg")) return true; return false;}; 135 }); 136 if ( files != null) 137 { 138 for (int i = 0; i < files.length; i++) 139 { 140 applyWaterMark(files[i]); 141 } 142 } 143 } 144 else if( inTmpDir.getName().toLowerCase().endsWith(".jpg")) 145 { 146 FileInputStream jpg = new FileInputStream (inTmpDir); 147 File junk = File.createTempFile("jpg","tmp"); 148 FileOutputStream out = new FileOutputStream (junk); 149 addWaterMark(jpg,out); 150 FileUtils.safeClose(jpg); 151 FileUtils.safeClose(out); 152 inTmpDir.delete(); 153 new OutputFiller().fill(junk,inTmpDir); 154 junk.delete(); 155 } 156 } 157 158 159 private File unzipToTmp(InputStream inStream) throws IOException , FileNotFoundException 160 { 161 ZipInputStream unzip = new ZipInputStream (inStream); 162 ZipEntry entry = unzip.getNextEntry(); 163 byte[] buf = new byte[1024]; 164 File tmpDir = File.createTempFile("archive","zip"); 165 tmpDir.delete(); 166 tmpDir.mkdirs(); 167 168 while( entry != null) 169 { 170 if ( !entry.isDirectory() ) 172 { 173 File ufile = new File ( tmpDir, entry.getName()); 174 FileOutputStream tout = new FileOutputStream (ufile); 175 int len; 176 while ( (len = unzip.read(buf)) > 0) { 177 tout.write(buf, 0, len); 178 } 179 } 180 entry = unzip.getNextEntry(); 181 } 182 return tmpDir; 183 } 184 } | Popular Tags |