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

📄 picturefiledata.java

📁 [linux.rar] - 嵌入式linux开发教程
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            try {
                return new FileInputStream(this.transformedPictureFile);
            } catch (FileNotFoundException e) {
                throw new JUploadIOException(e);
            }
        }
        // Otherwise : we read the file, in the standard way.
        return super.getInputStream();
    }

    /**
     * Cleaning of the temporary file on the hard drive, if any. <BR>
     * <B>Note:</B> if the debugLevel is 100 (or more) this temporary file is
     * not removed. This allow control of this created file.
     */
    @Override
    public void afterUpload() {
        super.afterUpload();

        // Free the temporary file ... if any.
        if (this.transformedPictureFile != null) {
            // for debug : if the debugLevel is enough, we keep the temporary
            // file (for check).
            if (this.uploadPolicy.getDebugLevel() >= 100) {
                this.uploadPolicy.displayWarn("Temporary file not deleted");
            } else {
                deleteTransformedPictureFile();
            }
        }
    }

    /**
     * This method creates a new Image, from the current picture. The resulting
     * width and height will be less or equal than the given maximum width and
     * height. The scale is maintained. Thus the width or height may be inferior
     * than the given values.
     * 
     * @param canvas The canvas on which the picture will be displayed.
     * @param shadow True if the pictureFileData should store this picture.
     *            False if the pictureFileData instance should not store this
     *            picture. Store this picture avoid calculating the image each
     *            time the user selects it in the file panel.
     * @return The rescaled image.
     * @throws JUploadException Encapsulation of the Exception, if any would
     *             occurs.
     */
    public Image getImage(Canvas canvas, boolean shadow)
            throws JUploadException {
        Image localImage = null;

        if (canvas == null) {
            throw new JUploadException(
                    "canvas null in PictureFileData.getImage");
        }

        int canvasWidth = canvas.getWidth();
        int canvasHeight = canvas.getHeight();
        if (canvasWidth <= 0 || canvasHeight <= 0) {
            this.uploadPolicy
                    .displayDebug(
                            "canvas width and/or height null in PictureFileData.getImage()",
                            1);
        } else if (shadow && this.offscreenImage != null) {
            // We take and return the previous calculated image for this
            // PictureFileData.
            localImage = this.offscreenImage;
        } else if (this.isPicture) {
            try {
                // First: load the picture.
                ImageReaderWriterHelper irwh = new ImageReaderWriterHelper(
                        (PictureUploadPolicy) this.uploadPolicy, this);
                BufferedImage sourceImage = irwh.readImage(0);
                irwh.dispose();
                irwh = null;
                ImageHelper ih = new ImageHelper(
                        (PictureUploadPolicy) this.uploadPolicy, this,
                        canvasWidth, canvasHeight, this.quarterRotation);
                localImage = ih.getBufferedImage(
                        ((PictureUploadPolicy) this.uploadPolicy)
                                .getHighQualityPreview(), sourceImage);
                // We free memory ASAP.
                sourceImage.flush();
                sourceImage = null;
            } catch (OutOfMemoryError e) {
                // Too bad
                localImage = null;
                tooBigPicture();
            }
        } // If isPicture

        // We store it, if asked to.
        if (shadow) {
            this.offscreenImage = localImage;
        }

        freeMemory("end of " + this.getClass().getName() + ".getImage()");

        // The picture is now loaded. We clear the progressBar
        this.uploadPolicy.getApplet().getUploadPanel()
                .getPreparationProgressBar().setValue(0);

        return localImage;
    }// getImage

    /**
     * This function is used to rotate the picture. The current rotation state
     * is kept in the quarterRotation private attribute.
     * 
     * @param quarter Number of quarters (90 degrees) the picture should rotate.
     *            1 means rotating of 90 degrees clockwise. Can be negative.
     */
    public void addRotation(int quarter) {
        this.quarterRotation += quarter;

        // We'll have to recalculate the upload length, as the resulting file is
        // different.
        this.uploadLength = -1;

        // We keep the 'quarter' in the segment [0;4[
        while (this.quarterRotation < 0) {
            this.quarterRotation += 4;
        }
        while (this.quarterRotation >= 4) {
            this.quarterRotation -= 4;
        }

        // We need to change the precalculated picture, if any
        if (this.offscreenImage != null) {
            this.offscreenImage.flush();
            this.offscreenImage = null;
        }
    }

    /**
     * Indicates if this file is actually a picture or not.
     * 
     * @return the isPicture flag.
     */
    public boolean isPicture() {
        return this.isPicture;
    }

    /** @see FileData#getMimeType() */
    @Override
    public String getMimeType() {
        return this.mimeType;
    }

    // ///////////////////////////////////////////////////////////////////////////////////////////
    // /////////////////////////// private METHODS
    // ///////////////////////////////////////////////////////////////////////////////////////////

    /**
     * File.deleteOnExit() is pretty unreliable, especially in applets.
     * Therefore the applet provides a callback which is executed during applet
     * termination. This method performs the actual cleanup.
     */
    public void deleteTransformedPictureFile() {
        if (null != this.transformedPictureFile) {
            this.transformedPictureFile.delete();
            this.transformedPictureFile = null;
            this.uploadLength = -1;
        }
    }

    /**
     * Creation of a temporary file, that contains the transformed picture. For
     * instance, it can be resized or rotated. This method doesn't throw
     * exception when there is an IOException within its procedure. If an
     * exception occurs while building the temporary file, the exception is
     * caught, a warning is displayed, the temporary file is deleted (if it was
     * created), and the upload will go on with the original file. <BR>
     * Note: any JUploadException thrown by a method called within
     * getTransformedPictureFile() will be thrown within this method.
     */
    private void initTransformedPictureFile() throws JUploadException {
        int targetMaxWidth;
        int targetMaxHeight;

        // If the image is rotated, we compare to realMaxWidth and
        // realMaxHeight, instead of maxWidth and maxHeight. This allows
        // to have a different picture size for rotated and not rotated
        // pictures. See the UploadPolicy javadoc for details ... and a
        // good reason ! ;-)
        if (this.quarterRotation == 0) {
            targetMaxWidth = ((PictureUploadPolicy) this.uploadPolicy)
                    .getMaxWidth();
            targetMaxHeight = ((PictureUploadPolicy) this.uploadPolicy)
                    .getMaxHeight();
        } else {
            targetMaxWidth = ((PictureUploadPolicy) this.uploadPolicy)
                    .getRealMaxWidth();
            targetMaxHeight = ((PictureUploadPolicy) this.uploadPolicy)
                    .getRealMaxHeight();
        }

        // Some Helper will .. help us !
        // I like useful comment :-)
        ImageHelper imageHelper = new ImageHelper(
                (PictureUploadPolicy) this.uploadPolicy, this, targetMaxWidth,
                targetMaxHeight, this.quarterRotation);

        // Should transform the file, and do we already created the transformed
        // file ?
        if (imageHelper.hasToTransformPicture()
                && this.transformedPictureFile == null) {

            // We have to create a resized or rotated picture file, and all
            // needed information.
            // ...let's do it
            try {
                createTranformedPictureFile(imageHelper);
            } catch (JUploadException e) {
                // Hum, too bad.
                // if any file was created, we remove it.
                if (this.transformedPictureFile != null) {
                    this.transformedPictureFile.delete();
                    this.transformedPictureFile = null;
                }
                throw e;
            }
        }
    }// end of initTransformedPictureFile

    /**
     * Creates a transformed picture file of the given max width and max height.
     * If the {@link #transformedPictureFile} attribute is not set before
     * calling this method, it will be set. If set before, the existing
     * {@link #transformedPictureFile} is replaced by the newly transformed
     * picture file. It is cleared if an error occured. <BR>
     * 
     * @param imageHelper The {@link ImageHelper} that was initialized with
     *            current parameters.
     */
    void createTranformedPictureFile(ImageHelper imageHelper)
            throws JUploadException {
        IIOMetadata metadata = null;
        IIOImage iioImage = null;
        BufferedImage originalImage = null;
        BufferedImage transformedImage = null;
        ImageReaderWriterHelper imageWriterHelper = new ImageReaderWriterHelper(
                (PictureUploadPolicy) this.uploadPolicy, this);
        boolean transmitMetadata = ((PictureUploadPolicy) this.uploadPolicy)
                .getPictureTransmitMetadata();

        // Creation of the transformed picture file.
        createTransformedTempFile();
        imageWriterHelper.setOutput(this.transformedPictureFile);

        // How many picture should we read from the input file.
        // Default number of pictures is one.
        int nbPictures = 1;
        // For gif file, we put a max to MAX_VALUE, and we check the
        // IndexOutOfBoundsException to identify when we've read all pictures
        if (getExtension(getFile()).equalsIgnoreCase("gif")) {
            nbPictures = Integer.MAX_VALUE;
        }
        this.uploadPolicy.displayDebug(
                "Reading image with imageWriterHelper.readImage(i)", 50);
        // Now, we have to read each picture from the original file, apply
        // the calculated transformation, and write each transformed picture
        // to the writer.
        // As indicated in javadoc for ImageReader.getNumImages(), we go
        // through pictures, until we get an IndexOutOfBoundsException.
        try {
            for (int i = 0; i < nbPictures; i += 1) {
                originalImage = imageWriterHelper.readImage(i);

                transformedImage = imageHelper.getBufferedImage(true,
                        originalImage);

                // If necessary, we load the metadata for the current
                // picture
                if (transmitMetadata) {
                    metadata = imageWriterHelper.getImageMetadata(i);
                }

                iioImage = new IIOImage(transformedImage, null, metadata);
                imageWriterHelper.write(iioImage);

                // Let's clear picture, to force getBufferedImage to read a new
                // one,
                // in the next loop.
                if (originalImage != null) {
                    originalImage.flush();

⌨️ 快捷键说明

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