⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image.java

📁 iText可以制作中文PDF文件的JAVA源程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * $Id: Image.java,v 1.63 2002/11/19 08:33:32 blowagie Exp $ * $Name:  $ * * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie. * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above.  If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ */package com.lowagie.text;import java.io.File;import java.io.InputStream;import java.io.IOException;import java.net.URL;import java.net.MalformedURLException;import java.util.Properties;import java.util.Set;import java.awt.color.ICC_Profile;import com.lowagie.text.pdf.PdfTemplate;import com.lowagie.text.pdf.CCITTG4Encoder;import java.lang.reflect.Constructor;/** * An <CODE>Image</CODE> is the representation of a graphic element (JPEG, PNG or GIF) * that has to be inserted into the document * * @see		Element * @see		Rectangle */public abstract class Image extends Rectangle implements Element, MarkupAttributes {        // static membervariables (concerning the presence of borders)        /** this is a kind of image alignment. */    public static final int DEFAULT = 0;        /** this is a kind of image alignment. */    public static final int RIGHT = 1;        /** this is a kind of image alignment. */    public static final int LEFT = 2;        /** this is a kind of image alignment. */    public static final int MIDDLE = 3;        /** this is a kind of image alignment. */    public static final int TEXTWRAP = 4;        /** this is a kind of image alignment. */    public static final int UNDERLYING = 8;        /** This represents a coordinate in the transformation matrix. */    public static final int AX = 0;        /** This represents a coordinate in the transformation matrix. */    public static final int AY = 1;        /** This represents a coordinate in the transformation matrix. */    public static final int BX = 2;        /** This represents a coordinate in the transformation matrix. */    public static final int BY = 3;        /** This represents a coordinate in the transformation matrix. */    public static final int CX = 4;        /** This represents a coordinate in the transformation matrix. */    public static final int CY = 5;        /** This represents a coordinate in the transformation matrix. */    public static final int DX = 6;        /** This represents a coordinate in the transformation matrix. */    public static final int DY = 7;        // membervariables        /** Adobe invert CMYK JPEG */    protected boolean invert = false;        /** The imagetype. */    protected int type;        /** The URL of the image. */    protected URL url;        /** The raw data of the image. */    protected byte rawData[];        /** The template to be treated as an image. */    protected PdfTemplate template;        /** The alignment of the Image. */    protected int alignment;        /** Text that can be shown instead of the image. */    protected String alt;        /** This is the absolute X-position of the image. */    protected float absoluteX = Float.NaN;        /** This is the absolute Y-position of the image. */    protected float absoluteY = Float.NaN;        /** This is the width of the image without rotation. */    protected float plainWidth;        /** This is the width of the image without rotation. */    protected float plainHeight;        /** This is the scaled width of the image taking rotation into account. */    protected float scaledWidth;        /** This is the original height of the image taking rotation into account. */    protected float scaledHeight;        /** This is the rotation of the image. */    protected float rotation;        /** this is the colorspace of a jpeg-image. */    protected int colorspace = -1;        /** this is the bits per component of the raw image. It also flags a CCITT image.*/    protected int bpc = 1;        /** this is the transparency information of the raw image*/    protected int transparency[];        // serial stamping        protected Long mySerialId = getSerialId();        static long serialId = 0;        /** Holds value of property dpiX. */    protected int dpiX = 0;        /** Holds value of property dpiY. */    protected int dpiY = 0;        protected boolean mask = false;        protected Image imageMask;        protected boolean invertMask = false;        /** Holds value of property interpolation. */    protected boolean interpolation;        /** if the annotation is not null the image will be clickable. */    protected Annotation annotation = null;        /** Contains extra markupAttributes */    protected Properties markupAttributes;        /** ICC Profile attached */    protected ICC_Profile profile = null;        // constructors        /**     * Constructs an <CODE>Image</CODE>-object, using an <VAR>url</VAR>.     *     * @param		url			the <CODE>URL</CODE> where the image can be found.     */        public Image(URL url) {        super(0, 0);        this.url = url;        this.alignment = DEFAULT;        rotation = 0;    }        /**     * 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.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.rotation = image.rotation;        this.colorspace = image.colorspace;        this.rawData = image.rawData;        this.template = image.template;        this.bpc = image.bpc;        this.transparency = image.transparency;        this.mySerialId = image.mySerialId;        this.invert = image.invert;        this.dpiX = image.dpiX;        this.dpiY = image.dpiY;        this.mask = image.mask;        this.imageMask = image.imageMask;        this.invertMask = image.invertMask;        this.interpolation = image.interpolation;        this.annotation = image.annotation;        this.markupAttributes = image.markupAttributes;        this.profile = image.profile;    }        // gets an instance of an Image        public static Image getInstance(Image image) {        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);        }    }        /**     * Gets an instance of an Image.     *     * @param	url     an URL     * @return	an object of type <CODE>Gif</CODE>, <CODE>Jpeg</CODE> or <CODE>Png</CODE>     */        public static Image getInstance(URL url) throws BadElementException, MalformedURLException, IOException {        InputStream is = null;        try {            is = url.openStream();            int c1 = is.read();            int c2 = is.read();            is.close();                        is = null;            if (c1 == 'G' && c2 == 'I') {                return new Gif(url);            }            if (c1 == 0xFF && c2 == 0xD8) {                return new Jpeg(url);            }            if (c1 == Png.PNGID[0] && c2 == Png.PNGID[1]) {                return new Png(url);            }            if (c1 == 0xD7 && c2 == 0xCD) {                return new ImgWMF(url);            }            throw new IOException(url.toString() + " is not a recognized imageformat.");        }        finally {            if (is != null) {                is.close();            }        }    }        /**     * 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 {        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];                        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 {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -