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

📄 imagereaderwriterhelper.java

📁 [linux.rar] - 嵌入式linux开发教程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//
// $Id$
//
// jupload - A file upload applet.
//
// Copyright 2008 The JUpload Team
//
// Created: 12 f関r. 08
// Creator: etienne_sf
// Last modified: $Date$
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

package wjhk.jupload2.filedata.helper;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;

import wjhk.jupload2.exception.JUploadIOException;
import wjhk.jupload2.filedata.DefaultFileData;
import wjhk.jupload2.filedata.PictureFileData;
import wjhk.jupload2.policies.PictureUploadPolicy;

/**
 * 
 * This package provides low level methods, for picture management. It is used
 * by {@link PictureFileData} to simplify its processing.
 * 
 * @author etienne_sf
 * 
 */
public class ImageReaderWriterHelper {

    /**
     * File input, from which the original picture should be read.
     */
    FileImageInputStream fileImageInputStream = null;

    /**
     * File output stream for the current transformation.
     */
    FileImageOutputStream fileImageOutputStream;

    /**
     * The {@link PictureFileData} that this helper will have to help.
     */
    PictureFileData pictureFileData;

    /**
     * Current ImageReader. Initialized by {@link #initImageReader()}
     */
    ImageReader imageReader = null;

    /**
     * Current ImageWriter. Initialized by {@link #initImageWriter()}
     */
    ImageWriter imageWriter = null;

    /**
     * Current ImageWriter. Initialized by {@link #initImageWriter()}
     */
    ImageWriteParam imageWriterParam = null;

    /**
     * Contains the target picture format: GIF, JPG... It is used to find an
     * ImageWriter, and to define the target filename.
     */
    String targetPictureFormat;

    /**
     * The current upload policy must be a {@link PictureUploadPolicy}
     */
    PictureUploadPolicy uploadPolicy;

    // ////////////////////////////////////////////////////////////////////
    // //////////////////// CONSTRUCTOR
    // ////////////////////////////////////////////////////////////////////

    /**
     * Standard constructor.
     * 
     * @param uploadPolicy The current upload policy.
     * @param pictureFileData The file data to be 'helped'.
     */
    public ImageReaderWriterHelper(PictureUploadPolicy uploadPolicy,
            PictureFileData pictureFileData) {
        this.uploadPolicy = uploadPolicy;
        this.pictureFileData = pictureFileData;

        // localPictureFormat is currently only used to define the correct
        // image writer. There is no transformation between to different
        // picture format (like JPG to GIF)
        if (this.uploadPolicy.getTargetPictureFormat() == null) {
            this.targetPictureFormat = pictureFileData.getFileExtension();
        } else {
            this.targetPictureFormat = this.uploadPolicy
                    .getTargetPictureFormat();
        }
    }

    // ////////////////////////////////////////////////////////////////////
    // //////////////////// PUBLIC METHODS
    // ////////////////////////////////////////////////////////////////////

    /**
     * Creates a FileImageOutputStream, and assign it as the output to the
     * imageWriter.
     * 
     * @param file The file where the output stream must write.
     * @throws JUploadIOException Any error...
     */
    public void setOutput(File file) throws JUploadIOException {
        // We first initialize internal variable.
        initImageWriter();

        try {
            this.fileImageOutputStream = new FileImageOutputStream(file);
        } catch (IOException e) {
            throw new JUploadIOException("ImageReaderWriterHelper.setOutput()",
                    e);
        }
        this.imageWriter.setOutput(this.fileImageOutputStream);
    }

    /**
     * Free all reserved resource by this helper. Includes closing of any input
     * or output stream.
     * 
     * @throws JUploadIOException Any IO Exception
     */
    public void dispose() throws JUploadIOException {
        // First: let's free any resource used by ImageWriter.
        if (this.imageWriter != null) {
            // An imageWriter was initialized. Let's free it.
            this.imageWriter.dispose();
            if (this.fileImageOutputStream != null) {
                try {
                    this.fileImageOutputStream.close();
                } catch (IOException e) {
                    throw new JUploadIOException(
                            "ImageReaderWriterHelper.dispose() [fileImageOutputStream]",
                            e);
                }
            }
            this.imageWriter = null;
            this.fileImageOutputStream = null;
        }

        // Then, all about ImageReader
        if (this.imageReader != null) {
            // An imageReader was initialized. Let's free it.
            this.imageReader.dispose();
            try {
                this.fileImageInputStream.close();
            } catch (IOException e) {
                throw new JUploadIOException(
                        "ImageReaderWriterHelper.dispose() [fileImageInputStream]",
                        e);
            }
            this.imageReader = null;
            this.fileImageInputStream = null;
        }
    }

    /**
     * Call to imageReader.getNumImages(boolean). Caution: may be long, for big
     * files.
     * 
     * @param allowSearch
     * @return The number of image into this file.
     * @throws JUploadIOException
     */
    public int getNumImages(boolean allowSearch) throws JUploadIOException {
        initImageReader();
        try {
            return this.imageReader.getNumImages(allowSearch);
        } catch (IOException e) {
            throw new JUploadIOException(
                    "ImageReaderWriterHelper.getNumImages() [fileImageInputStream]",
                    e);
        }
    }

    /**
     * Call to ImageIO.read(fileImageInputStream).
     * 
     * @return The BufferedImage read
     * @throws JUploadIOException
     */
    public BufferedImage imageIORead() throws JUploadIOException {
        try {
            return ImageIO.read(this.pictureFileData.getWorkingSourceFile());
        } catch (IOException e) {
            throw new JUploadIOException(
                    "ImageReaderWriterHelper.ImageIORead()", e);
        }

    }

    /**
     * Read an image, from the pictureFileData.
     * 
     * @param imageIndex The index number of the picture, in the file. 0 for the
     *            first picture (only valid value for picture containing one
     *            picture)
     * @return The image corresponding to this index, in the picture file.

⌨️ 快捷键说明

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