?? image.java
字號:
for (int j = 0; j < size; j++) { if (transparency == null) { int alpha = (pixels[j] >> 24) & 0xff; if (alpha == 0) { transparency = new int[6]; transparency[0] = transparency[1] = (pixels[j] >> 16) & 0xff; transparency[2] = transparency[3] = (pixels[j] >> 8) & 0xff; transparency[4] = transparency[5] = pixels[j] & 0xff; } } pixelsByte[index++] = (byte) ((pixels[j] >> 16) & 0xff); pixelsByte[index++] = (byte) ((pixels[j] >> 8) & 0xff); pixelsByte[index++] = (byte) ((pixels[j]) & 0xff); } } return Image.getInstance(w, h, 3, 8, pixelsByte, transparency); } } /** * 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 an Image. * * @param filename a filename * @return an object of type <CODE>Gif</CODE>, <CODE>Jpeg</CODE> or <CODE>Png</CODE> */ public static Image getInstance(String filename) throws BadElementException, MalformedURLException, IOException { return getInstance(toURL(filename)); } /** * Gets an instance of an Image. * * @param img a byte array * @return an object of type <CODE>Gif</CODE>, <CODE>Jpeg</CODE> or <CODE>Png</CODE> */ public static Image getInstance(byte[] img) throws BadElementException, MalformedURLException, IOException { InputStream is = null; try { is = new java.io.ByteArrayInputStream(img); int c1 = is.read(); int c2 = is.read(); is.close(); is = null; if (c1 == 'G' && c2 == 'I') { return new Gif(img); } if (c1 == 0xFF && c2 == 0xD8) { return new Jpeg(img); } if (c1 == Png.PNGID[0] && c2 == Png.PNGID[1]) { return new Png(img); } if (c1 == 0xD7 && c2 == 0xCD) { return new ImgWMF(img); } throw new IOException("Could not find a recognized imageformat."); } finally { if (is != null) { is.close(); } } } /** * Gets an instance of an Image in raw mode. * * @param width the width of the image in pixels * @param height the height of the image in pixels * @param components 1,3 or 4 for GrayScale, RGB and CMYK * @param data the image data * @param bpc bits per component * @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[]) throws BadElementException { return Image.getInstance(width, height, components, bpc, data, null); } public static Image getInstance(PdfTemplate template) throws BadElementException { return new ImgTemplate(template); } public static Image getInstance(int width, int height, boolean reverseBits, int typeCCITT, int parameters, byte[] data) throws BadElementException { return Image.getInstance(width, height, reverseBits, typeCCITT, parameters, data, null); } public static Image getInstance(int width, int height, boolean reverseBits, int typeCCITT, int parameters, byte[] data, int transparency[]) throws BadElementException { if (transparency != null && transparency.length != 2) throw new BadElementException("Transparency length must be equal to 2 with CCITT images"); Image img = new ImgCCITT(width, height, reverseBits, typeCCITT, parameters, data); img.transparency = transparency; return img; } /** * Gets an instance of an Image in raw mode. * * @param width the width of the image in pixels * @param height the height of the image in pixels * @param components 1,3 or 4 for GrayScale, RGB and CMYK * @param data the image data * @param bpc bits per component * @param transparency 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; } /** * Returns an <CODE>Image</CODE> that has been constructed taking in account * the value of some <VAR>attributes</VAR>. * * @param attributes Some attributes * @return an <CODE>Image</CODE> */ public static Image getInstance(Properties attributes) throws BadElementException, MalformedURLException, IOException { String value = (String)attributes.remove(ElementTags.URL); if (value == null) throw new MalformedURLException("The URL of the image is missing."); Image image = Image.getInstance(value); int align = 0; if ((value = (String)attributes.remove(ElementTags.ALIGN)) != null) { if (ElementTags.ALIGN_LEFT.equalsIgnoreCase(value)) align |= Image.LEFT; else if (ElementTags.ALIGN_RIGHT.equalsIgnoreCase(value)) align |= Image.RIGHT; else if (ElementTags.ALIGN_MIDDLE.equalsIgnoreCase(value)) align |= Image.MIDDLE; } if ((value = (String)attributes.remove(ElementTags.UNDERLYING)) != null) { if (new Boolean(value).booleanValue()) align |= Image.UNDERLYING; } if ((value = (String)attributes.remove(ElementTags.TEXTWRAP)) != null) { if (new Boolean(value).booleanValue()) align |= Image.TEXTWRAP; } image.setAlignment(align); if ((value = (String)attributes.remove(ElementTags.ALT)) != null) { image.setAlt(value); } String x; String y; if (((x = (String)attributes.remove(ElementTags.ABSOLUTEX)) != null) && ((y = (String)attributes.remove(ElementTags.ABSOLUTEY)) != null)) { image.setAbsolutePosition(Float.valueOf(x + "f").floatValue(), Float.valueOf(y + "f").floatValue()); } if ((value = (String)attributes.remove(ElementTags.PLAINWIDTH)) != null) { image.scaleAbsoluteWidth(Float.valueOf(value + "f").floatValue()); } if ((value = (String)attributes.remove(ElementTags.PLAINHEIGHT)) != null) { image.scaleAbsoluteHeight(Float.valueOf(value + "f").floatValue()); } if ((value = (String)attributes.remove(ElementTags.ROTATION)) != null) { image.setRotation(Float.valueOf(value + "f").floatValue()); } if (attributes.size() > 0) image.setMarkupAttributes(attributes); return image; } // methods to set information /** * Sets the alignment for the image. * * @param alignment the alignment */ public void setAlignment(int alignment) { this.alignment = alignment; } /** * Sets the alternative information for the image. * * @param alt the alternative information */ public void setAlt(String alt) { this.alt = alt; } /** * Sets the absolute position of the <CODE>Image</CODE>. * * @param absoluteX * @param absoluteY */ public void setAbsolutePosition(float absoluteX, float absoluteY) { this.absoluteX = absoluteX; this.absoluteY = absoluteY; } /** * Scale the image to an absolute width and an absolute height. * * @param newWidth the new width * @param newHeight the new height */ public void scaleAbsolute(float newWidth, float newHeight) { plainWidth = newWidth; plainHeight = newHeight; float[] matrix = matrix(); scaledWidth = matrix[DX] - matrix[CX]; scaledHeight = matrix[DY] - matrix[CY]; } /** * Scale the image to an absolute width. * * @param newWidth the new width */ public void scaleAbsoluteWidth(float newWidth) { plainWidth = newWidth; float[] matrix = matrix(); scaledWidth = matrix[DX] - matrix[CX]; scaledHeight = matrix[DY] - matrix[CY]; } /** * Scale the image to an absolute height. * * @param newHeight the new height */ public void scaleAbsoluteHeight(float newHeight) { plainHeight = newHeight; float[] matrix = matrix(); scaledWidth = matrix[DX] - matrix[CX]; scaledHeight = matrix[DY] - matrix[CY]; } /** * Scale the image to a certain percentage. * * @param percent the scaling percentage */ public void scalePercent(float percent) { scalePercent(percent, percent); } /** * Scale the width and height of an image to a certain percentage. * * @param percentX the scaling percentage of the width * @param percentY the scaling percentage of the height */ public void scalePercent(float percentX, float percentY) { plainWidth = (width() * percentX) / 100f; plainHeight = (height() * percentY) / 100f; float[] matrix = matrix(); scaledWidth = matrix[DX] - matrix[CX]; scaledHeight = matrix[DY] - matrix[CY]; } /** * Scales the image so that it fits a certain width and height. * * @param fitWidth the width to fit * @param fitHeight the height to fit */ public void scaleToFit(float fitWidth, float fitHeight) { float percentX = (fitWidth * 100) / width(); float percentY = (fitHeight * 100) / height(); scalePercent(percentX < percentY ? percentX : percentY); } /** * Sets the rotation of the image in radians. * * @param r rotation in radians */ public void setRotation(float r) { double d=Math.PI; //__IDS__ rotation = (float)(r % (2.0 * d)); //__IDS__ if (rotation < 0) { rotation += 2.0 * d; //__IDS__ } float[] matrix = matrix(); scaledWidth = matrix[DX] - matrix[CX]; scaledHeight = matrix[DY] - matrix[CY]; } /** * Sets the rotation of the image in degrees. * * @param deg rotation in degrees */ public void setRotationDegrees(float deg) { double d=Math.PI; //__IDS__ setRotation(deg / 180 * (float)d); //__IDS__ } /** * Sets the annotation of this Image. * * @param annotation the annotation */ public void setAnnotation(Annotation annotation) { this.annotation = annotation; } /** * Gets the annotation. * * @return the annotation that is linked to this image */ public Annotation annotation() { return annotation; } // methods to retrieve information /** Gets the bpc for the image. * <P> * Remark: this only makes sense for Images of the type <CODE>RawImage</CODE>. * * @return a bpc value */ public int bpc() { return bpc; } /** * Gets the raw data for the image. * <P> * Remark: this only makes sense for Images of the type <CODE>RawImage</CODE>. * * @return the raw data */ public byte[] rawData() { return rawData; } /** * Gets the template to be used as an image. * <P> * Remark: this only makes sense for Images of the type <CODE>ImgTemplate</CODE>. * * @return the template */ public PdfTemplate templateData() { return template; } public void setTemplateData(PdfTemplate template) { this.template = template; } /** * Checks if the <CODE>Images</CODE> has to be added at an absolute position. * * @return a boolean */ public boolean hasAbsolutePosition() { return !Float.isNaN(absoluteY); } /** * Checks if the <CODE>Images</CODE> has to be added at an absolute X position. * * @return a boolean */ public boolean hasAbsoluteX() { return !Float.isNaN(absoluteX); } /** * Returns the absolute X position. * * @return a position */ public float absoluteX() { return absoluteX; } /**
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -