📄 picturefiledata.java
字号:
originalImage = null;
}
}// for
} catch (IndexOutOfBoundsException e) {
// Was sent by imageWriterHelper.readImage(i)
// Ok, no more picture to read. We just want to go out of
// the loop. No error.
this.uploadPolicy.displayDebug(
"IndexOutOfBoundsException catched: end of reading for file "
+ getFileName(), 10);
}
if (originalImage != null) {
originalImage.flush();
originalImage = null;
}
// Let's free any used resource.
imageWriterHelper.dispose();
}
/**
* This method is called when an OutOfMemoryError occurs. This can easily
* happen within the navigator, with big pictures: I've put a lot of
* freeMemory calls within the code, but they don't seem to work very well.
* When running from eclipse, the memory is freed Ok !
*/
private void tooBigPicture() {
String msg = String.format(
this.uploadPolicy.getString("tooBigPicture"), getFileName());
JOptionPane.showMessageDialog(null, msg, "Warning",
JOptionPane.WARNING_MESSAGE);
this.uploadPolicy.displayWarn(msg);
}
/**
* This methods set the {@link DefaultFileData#mimeType} to the image mime
* type, that should be associate with the picture.
*/
private void setMimeTypeByExtension(String fileExtension) {
String ext = fileExtension.toLowerCase();
if (ext.equals("jpg")) {
ext = "jpeg";
}
this.mimeType = "image/" + ext;
}
/**
* If {@link #transformedPictureFile} is null, create a new temporary file,
* and assign it to {@link #transformedPictureFile}. Otherwise, no action.
*
* @throws IOException
*/
private void createTransformedTempFile() throws JUploadIOException {
if (this.transformedPictureFile == null) {
try {
this.transformedPictureFile = File.createTempFile("jupload_",
".tmp");
} catch (IOException e) {
throw new JUploadIOException(
"PictureFileData.createTransformedTempFile()", e);
}
this.uploadPolicy.getApplet().registerUnload(this,
"deleteTransformedPictureFile");
this.uploadPolicy.displayDebug("Using transformed temp file "
+ this.transformedPictureFile.getAbsolutePath() + " for "
+ getFileName(), 30);
}
}
/**
* This method loads the picture width and height of the picture. It's
* called by the current instance when necessary.
*
* @throws JUploadIOException
*
* @see #getOriginalHeight()
* @see #getOriginalWidth()
*/
private void initWidthAndHeight() throws JUploadIOException {
// Is it a picture?
if (this.isPicture
&& (this.originalHeight < 0 || this.originalWidth < 0)) {
// Ok: it's a picture and is original width and height have not been
// loaded yet.
// In the windows world, file extension may be in uppercase, which
// is not compatible with the core Java API.
Iterator<ImageReader> iter = ImageIO
.getImageReadersByFormatName(getFileExtension()
.toLowerCase());
if (iter.hasNext()) {
// It's a picture: we store its original width and height, for
// further calculation (rescaling and rotation).
try {
FileImageInputStream fiis = new FileImageInputStream(
getFile());
ImageReader ir = iter.next();
ir.setInput(fiis);
this.originalHeight = ir.getHeight(0);
this.originalWidth = ir.getWidth(0);
ir.dispose();
fiis.close();
} catch (IOException e) {
throw new JUploadIOException("PictureFileData()", e);
}
}
}
}
/**
* If {@link #workingCopyTempFile} is null, create a new temporary file, and
* assign it to {@link #transformedPictureFile}. Otherwise, no action.
*
* @throws IOException
*/
private void createWorkingCopyTempFile() throws IOException {
if (this.workingCopyTempFile == null) {
// The temporary file must have the correct extension, so that
// native Java method works on it.
this.workingCopyTempFile = File.createTempFile("jupload_", ".tmp."
+ DefaultFileData.getExtension(getFile()));
this.uploadPolicy.getApplet().registerUnload(this,
"deleteWorkingCopyPictureFile");
this.uploadPolicy.displayDebug("Using working copy temp file "
+ this.workingCopyTempFile.getAbsolutePath() + " for "
+ getFileName(), 30);
}
}
/**
* 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 deleteWorkingCopyPictureFile() {
if (null != this.workingCopyTempFile) {
this.workingCopyTempFile.delete();
this.workingCopyTempFile = null;
}
}
/**
* Get the file that contains the original picture. This is used as a
* workaround for the following JVM bug: once in the navigator, it can't
* transform picture read from a file whose name contains non-ASCII
* characters, like French accents.
*
* @return The file that contains the original picture, as the source for
* picture transformation
* @throws JUploadIOException
*/
public File getWorkingSourceFile() throws JUploadIOException {
if (this.workingCopyTempFile == null) {
this.uploadPolicy.displayDebug(
"[getWorkingSourceFile] Creating a copy of "
+ getFileName() + " as a source working target.",
30);
FileInputStream is = null;
FileOutputStream os = null;
try {
createWorkingCopyTempFile();
is = new FileInputStream(getFile());
os = new FileOutputStream(this.workingCopyTempFile);
byte b[] = new byte[1024];
int l;
while ((l = is.read(b)) > 0) {
os.write(b, 0, l);
}
} catch (IOException e) {
throw new JUploadIOException(
"ImageReaderWriterHelper.getWorkingSourceFile()", e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
this.uploadPolicy
.displayWarn(e.getClass().getName()
+ " while trying to close FileInputStream, in PictureUploadPolicy.copyOriginalToWorkingCopyTempFile.");
} finally {
is = null;
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
this.uploadPolicy
.displayWarn(e.getClass().getName()
+ " while trying to close FileOutputStream, in PictureUploadPolicy.copyOriginalToWorkingCopyTempFile.");
} finally {
os = null;
}
}
}
}
return this.workingCopyTempFile;
}// getWorkingSourceFile()
/**
* @return the originalWidth of the picture
* @throws JUploadIOException
*/
public int getOriginalWidth() throws JUploadIOException {
initWidthAndHeight();
return this.originalWidth;
}
/**
* @return the originalHeight of the picture
* @throws JUploadIOException
*/
public int getOriginalHeight() throws JUploadIOException {
initWidthAndHeight();
return this.originalHeight;
}
// ////////////////////////////////////////////////////////////////////////////////////////////////////
// /////////////////////// static methods
// ////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Returns an ImageIcon for the given file, resized according to the given
* dimensions. If the original file contains a pictures smaller than these
* width and height, the picture is returned as is (nor resized).
*
* @param pictureFile The file, containing a picture, from which the user
* wants to extract a static picture.
* @param maxWidth The maximum allowed width for the static picture to
* generate.
* @param maxHeight The maximum allowed height for the static picture to
* generate.
* @return The created static picture, or null if the file is null.
*/
public static ImageIcon getImageIcon(File pictureFile, int maxWidth,
int maxHeight) {
ImageIcon thumbnail = null;
if (pictureFile != null) {
ImageIcon tmpIcon = new ImageIcon(pictureFile.getPath());
if (tmpIcon != null) {
double scaleWidth = ((double) maxWidth)
/ tmpIcon.getIconWidth();
double scaleHeight = ((double) maxHeight)
/ tmpIcon.getIconHeight();
double scale = Math.min(scaleWidth, scaleHeight);
if (scale < 1) {
thumbnail = new ImageIcon(tmpIcon.getImage()
.getScaledInstance(
(int) (scale * tmpIcon.getIconWidth()),
(int) (scale * tmpIcon.getIconHeight()),
Image.SCALE_FAST));
} else { // no need to miniaturize
thumbnail = tmpIcon;
}
}
}
return thumbnail;
}
/**
* Indicates whether a file is a picture or not. The information is based on
* the fact the an ImageRead is found, or not, for this file. This test uses
* the core Java API. As in the windows world, file extension may be in
* uppercase, the test is based on the lowercase value for the given file
* extension.
*
* @param file
* @return true if the file can be opened as a picture, false otherwise.
*/
public static boolean isFileAPictrue(File file) {
// In the windows world, file extension may be in uppercase, which is
// not compatible with the core Java API.
Iterator<ImageReader> iter = ImageIO
.getImageReadersByFormatName(DefaultFileData.getExtension(file)
.toLowerCase());
return iter.hasNext();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -