📄 image.java
字号:
} 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 userdefined 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 userdefined 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;
}
// 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;
}
/**
* 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[] getRawData() {
return rawData;
}
/**
* 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 getBpc() {
return bpc;
}
/**
* 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 getTemplateData() {
return template[0];
}
/**
* Sets data from a PdfTemplate
*
* @param template
* the template with the content
*/
public void setTemplateData(PdfTemplate template) {
this.template[0] = template;
}
/**
* Gets the alignment for the image.
*
* @return a value
*/
public int getAlignment() {
return alignment;
}
/**
* Sets the alignment for the image.
*
* @param alignment
* the alignment
*/
public void setAlignment(int alignment) {
this.alignment = alignment;
}
/**
* Gets the alternative text for the image.
*
* @return a <CODE>String</CODE>
*/
public String getAlt() {
return alt;
}
/**
* 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;
}
/**
* 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 getAbsoluteX() {
return absoluteX;
}
/**
* Checks if the <CODE>Images</CODE> has to be added at an absolute
* position.
*
* @return a boolean
*/
public boolean hasAbsoluteY() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -