📄 imagereaderwriterhelper.java
字号:
* @throws JUploadIOException
* @throws IndexOutOfBoundsException Occurs when the imageIndex is wrong.
*/
public BufferedImage readImage(int imageIndex) throws JUploadIOException,
IndexOutOfBoundsException {
initImageReader();
try {
this.uploadPolicy.displayDebug(
"ImageReaderWriterHelper: reading picture number "
+ imageIndex + " of file "
+ this.pictureFileData.getFileName(), 30);
return this.imageReader.read(imageIndex);
} catch (IndexOutOfBoundsException e) {
// The IndexOutOfBoundsException is transmitted to the caller. It
// indicates that the index is out of bound. It's the good way for
// the caller to stop the loop through available pictures.
throw e;
} catch (IOException e) {
throw new JUploadIOException("ImageReaderWriterHelper.readImage("
+ imageIndex + ")", 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 full image data for this index
* @throws JUploadIOException
* @throws IndexOutOfBoundsException Occurs when the imageIndex is wrong.
*/
public IIOImage readAll(int imageIndex) throws JUploadIOException,
IndexOutOfBoundsException {
initImageReader();
try {
this.uploadPolicy.displayDebug(
"ImageReaderWriterHelper: reading picture number "
+ imageIndex + " of file "
+ this.pictureFileData.getFileName(), 30);
return this.imageReader.readAll(imageIndex, this.imageReader
.getDefaultReadParam());
} catch (IndexOutOfBoundsException e) {
// The IndexOutOfBoundsException is transmitted to the caller. It
// indicates that the index is out of bound. It's the good way for
// the caller to stop the loop through available pictures.
throw e;
} catch (IOException e) {
throw new JUploadIOException("ImageReaderWriterHelper.readAll("
+ imageIndex + ")", e);
}
}
/**
* Load the metadata associated with one picture in the picture file.
*
* @param imageIndex
* @return The metadata loaded
* @throws JUploadIOException Any IOException is encapsulated in this
* exception
*/
public IIOMetadata getImageMetadata(int imageIndex)
throws JUploadIOException {
// We must have the reader initialized
initImageReader();
// Ok, let's go
try {
this.uploadPolicy.displayDebug(
"ImageReaderWriterHelper: reading metadata for picture number "
+ imageIndex + " of file "
+ this.pictureFileData.getFileName(), 30);
return this.imageReader.getImageMetadata(imageIndex);
} catch (IOException e) {
throw new JUploadIOException(
"ImageReaderWriterHelper.getImageMetadata()", e);
}
}
/**
* Write a picture in the output picture file. Called just before an upload.
*
* @param numIndex The index of the image in the transformed picture file.
* @param iioImage The image to write.
* @param iwp The parameter to use to write this image.
* @throws JUploadIOException
*/
public void writeInsert(int numIndex, IIOImage iioImage, ImageWriteParam iwp)
throws JUploadIOException {
initImageWriter();
try {
this.imageWriter.writeInsert(numIndex, iioImage, iwp);
} catch (IOException e) {
throw new JUploadIOException(
"ImageReaderWriterHelper.writeInsert()", e);
}
}
/**
* Write a picture in the output picture file. Called just before an upload.
*
* @param iioImage The image to write.
* @throws JUploadIOException
*/
public void write(IIOImage iioImage) throws JUploadIOException {
initImageWriter();
try {
this.imageWriter.write(null, iioImage, this.imageWriterParam);
} catch (IOException e) {
throw new JUploadIOException("ImageReaderWriterHelper.write()", e);
}
}
// ////////////////////////////////////////////////////////////////////
// //////////////////// PRIVATE METHODS
// ////////////////////////////////////////////////////////////////////
/**
* Initialize the ImageWriter and the ImageWriteParam for the current
* picture helper.
*
* @throws JUploadIOException
*/
private void initImageWriter() throws JUploadIOException {
if (this.imageWriter == null) {
// Get the writer (to choose the compression quality)
// In the windows world, file extension may be in uppercase, which
// is not compatible with the core Java API.
String targetPictureFormatLowerCase = this.targetPictureFormat
.toLowerCase();
Iterator<ImageWriter> iter = ImageIO
.getImageWritersByFormatName(targetPictureFormatLowerCase);
if (!iter.hasNext()) {
// Too bad: no writer for the selected picture format
// A particular case: no gif support in JRE 1.5. A JRE upgrade
// must be done.
if (targetPictureFormatLowerCase.equals("gif")
&& System.getProperty("java.version").startsWith("1.5")) {
throw new JUploadIOException(
"gif pictures are not supported in Java 1.5. Please switch to JRE 1.6.");
}
throw new JUploadIOException("No writer for the '"
+ this.targetPictureFormat + "' picture format.");
} else {
this.imageWriter = iter.next();
this.imageWriterParam = this.imageWriter.getDefaultWriteParam();
// For jpeg pictures, we force the compression level.
if (this.targetPictureFormat.equalsIgnoreCase("jpg")
|| this.targetPictureFormat.equalsIgnoreCase("jpeg")) {
this.imageWriterParam
.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// Let's select a good compromise between picture size
// and quality.
this.imageWriterParam
.setCompressionQuality((this.uploadPolicy)
.getPictureCompressionQuality());
// In some case, we need to force the Huffman tables:
((JPEGImageWriteParam) this.imageWriterParam)
.setOptimizeHuffmanTables(true);
}
//
try {
this.uploadPolicy.displayDebug(
"ImageWriter1 (used), CompressionQuality="
+ this.imageWriterParam
.getCompressionQuality(), 50);
} catch (Exception e2) {
// If we come here, compression is not supported for
// this picture format, or parameters are not explicit
// mode, or ... (etc). May trigger several different
// errors. We just ignore them: this par of code is only
// to write some debug info.
}
}
}
}// initImageWriter
/**
* Initialize the ImageReader for the current helper.
*
* @throws JUploadIOException
*/
private void initImageReader() throws JUploadIOException {
// First: we open a ImageInputStream
try {
this.fileImageInputStream = new FileImageInputStream(
this.pictureFileData.getWorkingSourceFile());
} catch (IOException e) {
throw new JUploadIOException(
"ImageReaderWriterHelper.initImageReader()", e);
}
// Then: we create an ImageReader, and assign the ImageInputStream to
// it.
if (this.imageReader == null) {
String ext = DefaultFileData.getExtension(this.pictureFileData
.getFile());
Iterator<ImageReader> iterator = ImageIO
.getImageReadersBySuffix(ext);
if (iterator.hasNext()) {
this.imageReader = iterator.next();
this.imageReader.setInput(this.fileImageInputStream);
this.uploadPolicy.displayDebug("Foud one reader for " + ext
+ " extension", 50);
}// while
// Did we find our reader ?
if (this.imageReader == null) {
this.uploadPolicy.displayErr("Found no reader for " + ext
+ " extension");
} else if (this.uploadPolicy.getDebugLevel() >= 50) {
// This call may be long, so we do it only if useful.
try {
this.uploadPolicy.displayDebug("Nb images in "
+ this.pictureFileData.getFileName() + ": "
+ this.imageReader.getNumImages(true), 50);
} catch (IOException e) {
// We mask the error, was just for debug...
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -