?? saveimage.java
字號:
package gui;import jj2000.j2k.image.input.*;import jj2000.j2k.encoder.*;import jj2000.j2k.image.*;import jj2000.j2k.util.*;import com.sun.image.codec.jpeg.*;import javax.swing.event.*;import java.awt.image.*;import java.awt.event.*;import javax.swing.*;import java.text.*;import java.awt.*;import java.io.*;/** Class to save displayed image */public class SaveImage implements Runnable, ActionListener, ThreadSurvey { /** Reference to the main frame */ private Main mainFrame = null; /** Reference to the main frame's desktop */ private JDesktopPane desktop = null; /** Reference to the J2KGuiEncoder instance */ private J2KGuiEncoder j2kencoder = null; /** Reference to the encoder instance (for JPEG 2000 encoding) */ private Encoder enc = null; /** Format float for display (3 fractionnal digits) */ private static final DecimalFormat df = new DecimalFormat("##0.000"); /** Reference to the JJImgPanel instance where the input image is * displayed */ private JJImgPanel imgPan = null; /** Reference to the input file */ private File inputFile; /** Input image dimension */ private Dimension inDim; /** Whether or not a file has been selected for the saving operation */ private boolean fileSelected = false; /** Initial directory to open images */ private static File curDir = new File("/home/grosbois/"); /** Selected output file */ private File outFile; /** Type of selected output file */ private int fileType; private JDialog jpgDialog; private JSlider jpgQuality; /** Raw images file filter description */ private final static String rawImgDesc = "Raw images .pgm, .ppm"; /** Raw images files supported extensions */ private final static String[] rawext = { "pgm","ppm" }; /** JPEG 2000 file filter description */ private final static String jpeg2kDesc = "JPEG 2000 images .j2k .jpc .jp2"; /** JPEG 2000 files supported extensions */ private final static String[] j2kext = { "j2k", "jpc", "jp2"}; /** JPEG files filter description */ private final static String jpgDesc = "JPEG images .jpg"; /** JPEG images supported extensions */ private final static String[] jpgext = {"jpg"}; /** Output image type is unsupported */ public static final int FILE_TYPE_UNKNOWN = 0; /** Output image is JPEG */ public static final int FILE_TYPE_JPEG = 1; /** Output image is PGM */ public static final int FILE_TYPE_PGM = 2; /** Output image is PPM */ public static final int FILE_TYPE_PPM = 3; /** Output image is PGX */ public static final int FILE_TYPE_PGX = 4; /** Output image is JPEG 2000 codestream */ public static final int FILE_TYPE_J2K_COD = 5; /** Output type is JP2 file format */ public static final int FILE_TYPE_JP2 = 6; /** * Class constructor. It opens a JFileChooser in order to select the * * output file of the saving operation. Then it determines the output file * type. * */ public SaveImage(Main mainFrame, JDesktopPane desktop,File inputFile, Dimension inDim, JJImgPanel imgPan) { this.mainFrame = mainFrame; this.desktop = desktop; this.inputFile = inputFile; this.inDim = inDim; this.imgPan = imgPan; JFileChooser fcSave = new JFileChooser(curDir); fcSave.addChoosableFileFilter(new JJFileFilter(rawImgDesc,rawext)); fcSave.addChoosableFileFilter(new JJFileFilter(jpgDesc,jpgext)); fcSave.addChoosableFileFilter(new JJFileFilter(jpeg2kDesc,j2kext)); fcSave.setApproveButtonText("Save"); if(fcSave.showDialog(desktop,"Save")==JFileChooser.APPROVE_OPTION) { outFile = fcSave.getSelectedFile(); // Check that this is a "real" file if(outFile.isDirectory()) { return; } curDir = fcSave.getCurrentDirectory(); } else { return; } fileSelected = true; fileType = determineFileType(outFile); } /** Run the saving operation */ public void run() { int iFileType = determineFileType(inputFile); switch(fileType) { case FILE_TYPE_PGM: case FILE_TYPE_PPM: case FILE_TYPE_PGX: if(iFileType!=fileType) { JOptionPane.showMessageDialog(null, "Invalid output file","Error", JOptionPane.ERROR_MESSAGE); return; } if(copyFile(inputFile,outFile)) { mainFrame.renameFrame(outFile); } break; case FILE_TYPE_JP2: case FILE_TYPE_J2K_COD: j2kencoder = new J2KGuiEncoder(mainFrame,this,inputFile,inDim, imgPan); j2kencoder.start(); break; case FILE_TYPE_JPEG: saveJPEG(); break; default: JOptionPane.showMessageDialog(null, "Unknown output file type","Error", JOptionPane.ERROR_MESSAGE); } } /** Determine the output file type by looking at its extension */ private int determineFileType(File file) { String ext = null; String s = file.getPath(); int i = s.lastIndexOf('.'); if (i>0 && i<s.length()-1) { ext = s.substring(i+1).toLowerCase(); } if(ext.equalsIgnoreCase("pgm")) { return FILE_TYPE_PGM; } else if(ext.equalsIgnoreCase("pgx")) { return FILE_TYPE_PGX; } else if(ext.equalsIgnoreCase("ppm")) { return FILE_TYPE_PPM; } else if(ext.equalsIgnoreCase("jp2")) { return FILE_TYPE_JP2; } else if(ext.equalsIgnoreCase("jpg")) { return FILE_TYPE_JPEG; } else { for(int j=0; j<j2kext.length; j++) { if(ext.equalsIgnoreCase(j2kext[j])) { return FILE_TYPE_J2K_COD; } } return FILE_TYPE_UNKNOWN; } } /** Returns the type of the output file */ public int getFileType() { return fileType; } /** * Whether or not a file has been selected for the saving operation. A * file may not have been selected when the Cancel button has been * pressed. * */ public boolean isFileSelected() { return fileSelected; } /** * Copies one specified file to another one and returns whether or not the * operation has succedded. * */ private boolean copyFile(File in, File out) { int confirm = JOptionPane.OK_OPTION; if(out.exists()) { confirm = JOptionPane.showConfirmDialog(null,out+" already exists. Do "+ "you want to overwrite it ?", "Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); } if(confirm!=JOptionPane.OK_OPTION) { return false; } BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(new FileInputStream(in)); bos = new BufferedOutputStream(new FileOutputStream(out)); long len = in.length(); for(int i=0; i<len; i++) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -