1 31 package org.pdfbox.pdmodel.graphics.xobject; 32 33 import java.awt.image.BufferedImage ; 34 import java.io.InputStream ; 35 import java.io.IOException ; 36 import java.io.OutputStream ; 37 38 import java.util.ArrayList ; 39 import java.util.List ; 40 41 import javax.imageio.ImageIO ; 42 43 import org.pdfbox.cos.COSDictionary; 44 import org.pdfbox.cos.COSName; 45 46 import org.pdfbox.pdmodel.PDDocument; 47 import org.pdfbox.pdmodel.common.PDStream; 48 import org.pdfbox.pdmodel.graphics.color.PDDeviceRGB; 49 50 56 public class PDJpeg extends PDXObjectImage 57 { 58 59 private static final List DCT_FILTERS = new ArrayList (); 60 61 static 62 { 63 DCT_FILTERS.add( COSName.DCT_DECODE.getName() ); 64 DCT_FILTERS.add( COSName.DCT_DECODE_ABBREVIATION.getName() ); 65 } 66 67 72 public PDJpeg(PDStream jpeg) 73 { 74 super(jpeg, "jpg"); 75 } 76 77 84 public PDJpeg( PDDocument doc, InputStream is ) throws IOException 85 { 86 super( new PDStream( doc, is, true ), "jpg" ); 87 COSDictionary dic = getCOSStream(); 88 dic.setItem( COSName.FILTER, COSName.DCT_DECODE ); 89 dic.setItem( COSName.SUBTYPE, COSName.IMAGE); 90 dic.setItem( COSName.TYPE, COSName.getPDFName( "XObject" ) ); 91 92 BufferedImage image = getRGBImage(); 93 setBitsPerComponent( 8 ); 94 setColorSpace( PDDeviceRGB.INSTANCE ); 95 setHeight( image.getHeight() ); 96 setWidth( image.getWidth() ); 97 98 } 99 100 107 public PDJpeg( PDDocument doc, BufferedImage bi ) throws IOException 108 { 109 super( new PDStream( doc ), "jpg" ); 110 111 java.io.OutputStream os = getCOSStream().createFilteredStream(); 112 try 113 { 114 115 ImageIO.write(bi,"jpeg",os); 116 117 COSDictionary dic = getCOSStream(); 118 dic.setItem( COSName.FILTER, COSName.DCT_DECODE ); 119 dic.setItem( COSName.SUBTYPE, COSName.IMAGE); 120 dic.setItem( COSName.TYPE, COSName.getPDFName( "XObject" ) ); 121 122 setBitsPerComponent( 8 ); 123 setColorSpace( PDDeviceRGB.INSTANCE ); 124 setHeight( bi.getHeight() ); 125 setWidth( bi.getWidth() ); 126 } 127 finally 128 { 129 os.close(); 130 } 131 } 132 133 137 public BufferedImage getRGBImage() throws IOException 138 { 139 return ImageIO.read(getPDStream().getPartiallyFilteredStream( DCT_FILTERS )); 140 } 141 142 146 public void write2OutputStream(OutputStream out) throws IOException 147 { 148 InputStream data = getPDStream().getPartiallyFilteredStream( DCT_FILTERS ); 149 byte[] buf = new byte[1024]; 150 int amountRead = -1; 151 while( (amountRead = data.read( buf )) != -1 ) 152 { 153 out.write( buf, 0, amountRead ); 154 } 155 } 156 } 157 | Popular Tags |