📄 image.java
字号:
* transparency information in the Mask format of the image * dictionary * @return an object of type <CODE>ImgRaw</CODE> * @throws BadElementException * on error */ public static Image getInstance(int width, int height, int components, int bpc, byte data[], int transparency[]) throws BadElementException { if (transparency != null && transparency.length != components * 2) throw new BadElementException( "Transparency length must be equal to (componentes * 2)"); if (components == 1 && bpc == 1) { byte g4[] = CCITTG4Encoder.compress(data, width, height); return Image.getInstance(width, height, false, Image.CCITTG4, Image.CCITT_BLACKIS1, g4, transparency); } Image img = new ImgRaw(width, height, components, bpc, data); img.transparency = transparency; return img; } // images from a PdfTemplate /** * gets an instance of an Image * * @param template * a PdfTemplate that has to be wrapped in an Image object * @return an Image object * @throws BadElementException */ public static Image getInstance(PdfTemplate template) throws BadElementException { return new ImgTemplate(template); } // images from a java.awt.Image /** * Gets an instance of an Image from a java.awt.Image. * * @param image * the <CODE>java.awt.Image</CODE> to convert * @param color * if different from <CODE>null</CODE> the transparency pixels * are replaced by this color * @param forceBW * if <CODE>true</CODE> the image is treated as black and white * @return an object of type <CODE>ImgRaw</CODE> * @throws BadElementException * on error * @throws IOException * on error */ public static Image getInstance(java.awt.Image image, java.awt.Color color, boolean forceBW) throws BadElementException, IOException { if(image instanceof BufferedImage){ BufferedImage bi = (BufferedImage) image; if(bi.getType()==BufferedImage.TYPE_BYTE_BINARY) { forceBW=true; } } java.awt.image.PixelGrabber pg = new java.awt.image.PixelGrabber(image, 0, 0, -1, -1, true); try { pg.grabPixels(); } catch (InterruptedException e) { throw new IOException( "java.awt.Image Interrupted waiting for pixels!"); } if ((pg.getStatus() & java.awt.image.ImageObserver.ABORT) != 0) { throw new IOException("java.awt.Image fetch aborted or errored"); } int w = pg.getWidth(); int h = pg.getHeight(); int[] pixels = (int[]) pg.getPixels(); if (forceBW) { int byteWidth = (w / 8) + ((w & 7) != 0 ? 1 : 0); byte[] pixelsByte = new byte[byteWidth * h]; int index = 0; int size = h * w; int transColor = 1; if (color != null) { transColor = (color.getRed() + color.getGreen() + color.getBlue() < 384) ? 0 : 1; } int transparency[] = null; int cbyte = 0x80; int wMarker = 0; int currByte = 0; if (color != null) { for (int j = 0; j < size; j++) { int alpha = (pixels[j] >> 24) & 0xff; if (alpha < 250) { if (transColor == 1) currByte |= cbyte; } else { if ((pixels[j] & 0x888) != 0) currByte |= cbyte; } cbyte >>= 1; if (cbyte == 0 || wMarker + 1 >= w) { pixelsByte[index++] = (byte) currByte; cbyte = 0x80; currByte = 0; } ++wMarker; if (wMarker >= w) wMarker = 0; } } else { for (int j = 0; j < size; j++) { if (transparency == null) { int alpha = (pixels[j] >> 24) & 0xff; if (alpha == 0) { transparency = new int[2]; transparency[0] = transparency[1] = ((pixels[j] & 0x888) != 0) ? 1 : 0; } } if ((pixels[j] & 0x888) != 0) currByte |= cbyte; cbyte >>= 1; if (cbyte == 0 || wMarker + 1 >= w) { pixelsByte[index++] = (byte) currByte; cbyte = 0x80; currByte = 0; } ++wMarker; if (wMarker >= w) wMarker = 0; } } return Image.getInstance(w, h, 1, 1, pixelsByte, transparency); } else { byte[] pixelsByte = new byte[w * h * 3]; byte[] smask = null; int index = 0; int size = h * w; int red = 255; int green = 255; int blue = 255; if (color != null) { red = color.getRed(); green = color.getGreen(); blue = color.getBlue(); } int transparency[] = null; if (color != null) { for (int j = 0; j < size; j++) { int alpha = (pixels[j] >> 24) & 0xff; if (alpha < 250) { pixelsByte[index++] = (byte) red; pixelsByte[index++] = (byte) green; pixelsByte[index++] = (byte) blue; } else { pixelsByte[index++] = (byte) ((pixels[j] >> 16) & 0xff); pixelsByte[index++] = (byte) ((pixels[j] >> 8) & 0xff); pixelsByte[index++] = (byte) ((pixels[j]) & 0xff); } } } else { int transparentPixel = 0; smask = new byte[w * h]; boolean shades = false; for (int j = 0; j < size; j++) { byte alpha = smask[j] = (byte) ((pixels[j] >> 24) & 0xff); /* bugfix by Chris Nokleberg */ if (!shades) { if (alpha != 0 && alpha != -1) { shades = true; } else if (transparency == null) { if (alpha == 0) { transparentPixel = pixels[j] & 0xffffff; transparency = new int[6]; transparency[0] = transparency[1] = (transparentPixel >> 16) & 0xff; transparency[2] = transparency[3] = (transparentPixel >> 8) & 0xff; transparency[4] = transparency[5] = transparentPixel & 0xff; } } else if ((pixels[j] & 0xffffff) != transparentPixel) { shades = true; } } pixelsByte[index++] = (byte) ((pixels[j] >> 16) & 0xff); pixelsByte[index++] = (byte) ((pixels[j] >> 8) & 0xff); pixelsByte[index++] = (byte) ((pixels[j]) & 0xff); } if (shades) transparency = null; else smask = null; } Image img = Image.getInstance(w, h, 3, 8, pixelsByte, transparency); if (smask != null) { Image sm = Image.getInstance(w, h, 1, 8, smask); try { sm.makeMask(); img.setImageMask(sm); } catch (DocumentException de) { throw new ExceptionConverter(de); } } return img; } } /** * Gets an instance of an Image from a java.awt.Image. * * @param image * the <CODE>java.awt.Image</CODE> to convert * @param color * if different from <CODE>null</CODE> the transparency pixels * are replaced by this color * @return an object of type <CODE>ImgRaw</CODE> * @throws BadElementException * on error * @throws IOException * on error */ public static Image getInstance(java.awt.Image image, java.awt.Color color) throws BadElementException, IOException { return Image.getInstance(image, color, false); } /** * Gets an instance of a Image from a java.awt.Image. * The image is added as a JPEG with a user defined quality. * * @param writer * the <CODE>PdfWriter</CODE> object to which the image will be added * @param awtImage * the <CODE>java.awt.Image</CODE> to convert * @param quality * a float value between 0 and 1 * @return an object of type <CODE>PdfTemplate</CODE> * @throws BadElementException * on error * @throws IOException */ public static Image getInstance(PdfWriter writer, java.awt.Image awtImage, float quality) throws BadElementException, IOException { return getInstance(new PdfContentByte(writer), awtImage, quality); } /** * Gets an instance of a Image from a java.awt.Image. * The image is added as a JPEG with a user defined quality. * * @param cb * the <CODE>PdfContentByte</CODE> object to which the image will be added * @param awtImage * the <CODE>java.awt.Image</CODE> to convert * @param quality * a float value between 0 and 1 * @return an object of type <CODE>PdfTemplate</CODE> * @throws BadElementException * on error * @throws IOException */ public static Image getInstance(PdfContentByte cb, java.awt.Image awtImage, float quality) throws BadElementException, IOException { java.awt.image.PixelGrabber pg = new java.awt.image.PixelGrabber(awtImage, 0, 0, -1, -1, true); try { pg.grabPixels(); } catch (InterruptedException e) { throw new IOException( "java.awt.Image Interrupted waiting for pixels!"); } if ((pg.getStatus() & java.awt.image.ImageObserver.ABORT) != 0) { throw new IOException("java.awt.Image fetch aborted or errored"); } int w = pg.getWidth(); int h = pg.getHeight(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2d = tp.createGraphics(w, h, true, quality); g2d.drawImage(awtImage, 0, 0, null); g2d.dispose(); return getInstance(tp); } // image from indirect reference /** * Holds value of property directReference. * An image is embedded into a PDF as an Image XObject. * This object is referenced by a PdfIndirectReference object. */ private PdfIndirectReference directReference; /** * Getter for property directReference. * @return Value of property directReference. */ public PdfIndirectReference getDirectReference() { return this.directReference; } /** * Setter for property directReference. * @param directReference New value of property directReference. */ public void setDirectReference(PdfIndirectReference directReference) { this.directReference = directReference; } /** * Reuses an existing image. * @param ref the reference to the image dictionary * @throws BadElementException on error * @return the image */ public static Image getInstance(PRIndirectReference ref) throws BadElementException { PdfDictionary dic = (PdfDictionary)PdfReader.getPdfObjectRelease(ref); int width = ((PdfNumber)PdfReader.getPdfObjectRelease(dic.get(PdfName.WIDTH))).intValue(); int height = ((PdfNumber)PdfReader.getPdfObjectRelease(dic.get(PdfName.HEIGHT))).intValue(); Image imask = null; PdfObject obj = dic.get(PdfName.SMASK); if (obj != null && obj.isIndirect()) { imask = getInstance((PRIndirectReference)obj); } else { obj = dic.get(PdfName.MASK); if (obj != null && obj.isIndirect()) { PdfObject obj2 = PdfReader.getPdfObjectRelease(obj); if (obj2 instanceof PdfDictionary) imask = getInstance((PRIndirectReference)obj); } } Image img = new ImgRaw(width, height, 1, 1, null); img.imageMask = imask; img.directReference = ref; return img; } // copy constructor /** * Constructs an <CODE>Image</CODE> -object, using an <VAR>url </VAR>. * * @param image * another Image object. */ protected Image(Image image) { super(image); this.type = image.type; this.url = image.url; this.rawData = image.rawData; this.bpc = image.bpc; this.template = image.template; this.alignment = image.alignment; this.alt = image.alt; this.absoluteX = image.absoluteX; this.absoluteY = image.absoluteY; this.plainWidth = image.plainWidth; this.plainHeight = image.plainHeight; this.scaledWidth = image.scaledWidth; this.scaledHeight = image.scaledHeight; this.mySerialId = image.mySerialId; this.directReference = image.directReference; this.rotationRadians = image.rotationRadians; this.initialRotation = image.initialRotation; this.indentationLeft = image.indentationLeft; this.indentationRight = image.indentationRight; this.spacingBefore = image.spacingBefore; this.spacingAfter = image.spacingAfter; this.widthPercentage = image.widthPercentage; this.annotation = image.annotation; this.layer = image.layer; this.interpolation = image.interpolation; this.originalType = image.originalType; this.originalData = image.originalData; this.deflated = image.deflated; this.dpiX = image.dpiX; this.dpiY = image.dpiY; this.XYRatio = image.XYRatio; this.colorspace = image.colorspace; this.invert = image.invert; this.profile = image.profile; this.additional = image.additional; this.mask = image.mask; this.imageMask = image.imageMask; this.smask = image.smask; this.transparency = image.transparency; } /** * gets an instance of an Image * * @param image * an Image object * @return a new Image object */ public static Image getInstance(Image image) { if (image == null) return null; try { Class cs = image.getClass(); Constructor constructor = cs .getDeclaredConstructor(new Class[] { Image.class }); return (Image) constructor.newInstance(new Object[] { image }); } catch (Exception e) { throw new ExceptionConverter(e); } } // implementation of the Element interface /** * Returns the type. * * @return a type */ public int type() { return type; } /** * @see com.lowagie.text.Element#isNestable() * @since iText 2.0.8 */ public boolean isNestable() { return true; } // checking the type of Image /** * Returns <CODE>true</CODE> if the image is a <CODE>Jpeg</CODE> * -object. * * @return a <CODE>boolean</CODE> */ public boolean isJpeg() { return type == JPEG; } /** * Returns <CODE>true</CODE> if the image is a <CODE>ImgRaw</CODE> * -object. * * @return a <CODE>boolean</CODE> */ public boolean isImgRaw() { return type == IMGRAW; } /** * Returns <CODE>true</CODE> if the image is an <CODE>ImgTemplate</CODE> * -object. * * @return a <CODE>boolean</CODE> */ public boolean isImgTemplate() { return type == IMGTEMPLATE; } // getters and setters /** * Gets the <CODE>String</CODE> -representation of the reference to the * image. * * @return a <CODE>String</CODE> */ public URL getUrl() { return url; } /** * Sets the url of the image * * @param url * the url of the image */ public void setUrl(URL url) { this.url = url; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -