📄 imagehelper.java
字号:
//
// $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;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import wjhk.jupload2.exception.JUploadException;
import wjhk.jupload2.exception.JUploadIOException;
import wjhk.jupload2.filedata.DefaultFileData;
import wjhk.jupload2.filedata.PictureFileData;
import wjhk.jupload2.policies.PictureUploadPolicy;
/**
* Class that contains various utilities about picture, mainly about picture
* transformation.
*
* @author etienne_sf
*
*/
public class ImageHelper implements ImageObserver {
/**
* hasToTransformPicture indicates whether the picture should be
* transformed. Null if unknown. This can happen (for instance) if no calcul
* where done (during initialization), or after rotating the picture back to
* the original orientation. <BR>
* <B>Note:</B> this attribute is from the class Boolean (and not a simple
* boolean), to allow null value, meaning <I>unknown</I>.
*/
private Boolean hasToTransformPicture = null;
/**
* The {@link PictureFileData} that this helper will have to help.
*/
private PictureFileData pictureFileData;
/**
* Current rotation of the picture: 0 to 3.
*
* @see PictureFileData
*/
private int quarterRotation;
/**
* Maximum width for the current transformation
*/
private int maxWidth;
/**
* Maximum height for the current transformation
*/
private int maxHeight;
/**
* Defines the number of pixel for the current picture. Used to update the
* progress bar.
*
* @see #getBufferedImage(boolean, BufferedImage)
* @see #imageUpdate(Image, int, int, int, int, int)
*/
private int nbPixelsTotal = -1;
/**
* Indicates the number of pixels that have been read.
*
* @see #nbPixelsTotal
* @see #imageUpdate(Image, int, int, int, int, int)
*/
private int nbPixelsRead = 0;
/**
* Width of picture, after rescaling but without rotation. It should be
* scale*originalWidth, but, due to rounding number, it can be transformed
* to scale*originalWidth-1.
*
* @see #initScale()
*/
private int scaledNonRotatedWidth = -1;
/**
* Same as {@link #scaledNonRotatedWidth}
*/
private int scaledNonRotatedHeight = -1;
/**
* The value that has the progress bar when starting to load the picture.
* The {@link #imageUpdate(Image, int, int, int, int, int)} method will add
* from 0 to 100, to indicate progress with a percentage value of picture
* loading.
*/
private int progressBarBaseValue = 0;
/**
* Current scaling factor. If less than 1, means a picture reduction.
*
* @see #initScale()
*/
private double scale = 1;
/**
* Width of picture, after re-scaling and rotation. It should be
* scale*originalWidth or scale*originalHeight (depending on the rotation).
* But, due to rounding number, it can be transformed to
* scale*originalWidth-1 or scale*originalHeight-1.
*
* @see #initScale()
*/
private int scaledRotatedWidth = -1;
/**
* Same as {@link #scaledRotatedWidth}, for the height.
*/
private int scaledRotatedHeight = -1;
/**
* The current upload policy must be a {@link PictureUploadPolicy}
*/
PictureUploadPolicy uploadPolicy;
/**
* Standard constructor.
*
* @param uploadPolicy The current upload policy
* @param pictureFileData The picture file data to help
* @param targetMaxWidth
* @param targetMaxHeight
* @param quarterRotation Current quarter rotation (from 0 to 3)
* @throws JUploadIOException
*/
public ImageHelper(PictureUploadPolicy uploadPolicy,
PictureFileData pictureFileData, int targetMaxWidth,
int targetMaxHeight, int quarterRotation) throws JUploadIOException {
this.uploadPolicy = uploadPolicy;
this.pictureFileData = pictureFileData;
this.maxWidth = targetMaxWidth;
this.maxHeight = targetMaxHeight;
this.quarterRotation = quarterRotation;
// Pre-calculation: should the current picture be rescaled, to match the
// given target size ?
initScale();
}
/**
* Intialization of scale factor, for the current picture state. The scale
* is based on the maximum width and height, the current rotation, and the
* picture size.
*/
private void initScale() throws JUploadIOException {
double theta = Math.toRadians(90 * this.quarterRotation);
// The width and height depend on the current rotation :
// calculation of the width and height of picture after
// rotation.
int nonScaledRotatedWidth = this.pictureFileData.getOriginalWidth();
int nonScaledRotatedHeight = this.pictureFileData.getOriginalHeight();
if (this.quarterRotation % 2 != 0) {
// 90
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -