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

📄 openimage.java

📁 jpeg2000编解码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * Display the Image instance in a panel (JJImgPanel). Then it inserts     * the panel in an internal frame      * */    private void displayImage(String info, boolean isTemporaryFile) {        if(image==null) return; // No image has been created        // +---------------------------------+        // | Display Image in internal frame |        // +---------------------------------+        mainFrame.setCursor(Cursor.WAIT_CURSOR);        JInternalFrame f = new JInternalFrame("",true,true,true,true);        // Set preferred size for using scroll-bars appropriately        JJImgPanel imgPanel = 	    new JJImgPanel(image,new Rectangle(0,0,width,height),ncImg,			   mainFrame);        // Scroll bars, rules and corners        JScrollPane imgScrollPane = new JScrollPane(imgPanel);        f.getContentPane().add(imgScrollPane);          f.pack();        // Display the decoding rate in the title	Rectangle rect = new Rectangle(0,0,width,height);	FrameAttribute fA = new FrameAttribute(ncImg,rawFile,rect,imgPanel,					       inFile.getName()+" @ "+					       df.format(rate)+" bpp",info,					       imgScrollPane,isTemporaryFile);        mainFrame.addInternalFrame(f,fA);        mainFrame.setCursor(Cursor.DEFAULT_CURSOR);                        return;    }    /** Opens three PGX files and display them as a RGB image */    private void openThreePGX(File in) {	mainFrame.setCursor(Cursor.WAIT_CURSOR);	// Retrieve file names prefix	String prefix = in.getPath().	    substring(0,in.getPath().lastIndexOf('.'));		// Image readers	ImgReaderPGX[] imr = new ImgReaderPGX[3];	ImgDataJoiner joiner;	try {	    imr[0] = new ImgReaderPGX(prefix+"-1.pgx");	    imr[1] = new ImgReaderPGX(prefix+"-2.pgx");	    imr[2] = new ImgReaderPGX(prefix+"-3.pgx");	    BlkImgDataSrc[] src = new BlkImgDataSrc[3];	    src[0] = new RescaleImgReader(imr[0]); 	    src[1] = new RescaleImgReader(imr[1]);	    src[2] = new RescaleImgReader(imr[2]);	    int[] cc = new int[3];	    cc[0] = cc[1] = cc[2] = 0;	    joiner = new ImgDataJoiner(src,cc);	    width = imr[0].getImgWidth();	    height = imr[0].getImgHeight();	} catch(IOException e) {            JOptionPane.showMessageDialog(desktop,                                          "Cannot open specified images: "+                                          prefix+"-{0,1,2}.pgx","Error",                                          JOptionPane.ERROR_MESSAGE);            mainFrame.setCursor(Cursor.DEFAULT_CURSOR);                            return;        }        // Image producer        try {            image = BlkImgDataSrcImageProducer.createImage(joiner);        } catch(IllegalArgumentException e) {            JOptionPane.showMessageDialog(desktop,                                          "Cannot create image producer",                                          "Error",                                          JOptionPane.ERROR_MESSAGE);            mainFrame.setCursor(Cursor.DEFAULT_CURSOR);                            return;        }        rate = (inFile.length()*8f)/(width*height);    }    /** Opens the specified PGX file and creates the Image instance */    private void openPGX(File in) {        mainFrame.setCursor(Cursor.WAIT_CURSOR);                // Image reader        ImgReaderPGX imr;        try {            imr = new ImgReaderPGX(in.getPath());            width = imr.getImgWidth();            height = imr.getImgHeight();        } catch(IOException e) {            JOptionPane.showMessageDialog(desktop,                                          "Cannot open specified image: "+                                          in.getPath(),"Error",                                          JOptionPane.ERROR_MESSAGE);            mainFrame.setCursor(Cursor.DEFAULT_CURSOR);                            return;        }        // Image producer        try {            image = BlkImgDataSrcImageProducer.		createImage(new RescaleImgReader(imr));        } catch(IllegalArgumentException e) {            JOptionPane.showMessageDialog(desktop,                                          "Cannot create image producer",                                          "Error",                                          JOptionPane.ERROR_MESSAGE);            mainFrame.setCursor(Cursor.DEFAULT_CURSOR);                            return;        }        rate = inFile.length()*8f/width/height;    }    /** Open the PPM file and creates the Image instance */    private void openPPM(File in) {        mainFrame.setCursor(Cursor.WAIT_CURSOR);                // Image reader        ImgReaderPPM imr;        try {            imr = new ImgReaderPPM(in.getPath());            width = imr.getImgWidth();            height = imr.getImgHeight();            ncImg = 3;	    depth = new int[3];	    depth[0] = depth[1] = depth[2] = 8;        } catch(IOException e) {            JOptionPane.showMessageDialog(desktop,                                          "Cannot open specified image: "+                                          in.getPath(),"Error",                                          JOptionPane.ERROR_MESSAGE);            mainFrame.setCursor(Cursor.DEFAULT_CURSOR);                            return;        }        // Image producer        try {            image = BlkImgDataSrcImageProducer.createImage(imr);        } catch(IllegalArgumentException e) {            JOptionPane.showMessageDialog(desktop,                                          "Cannot create image reader",                                          "Error",                                          JOptionPane.ERROR_MESSAGE);            mainFrame.setCursor(Cursor.DEFAULT_CURSOR);                            return;        }        rawFile = inFile;        rate = inFile.length()*8f/width/height;    }    /** Open the PGM file and creates the Image instance */    private void openPGM(File in) {        mainFrame.setCursor(Cursor.WAIT_CURSOR);                // Image reader        ImgReaderPGM imr;        try {            imr = new ImgReaderPGM(in.getPath());            width = imr.getImgWidth();            height = imr.getImgHeight();        } catch(IOException e) {            JOptionPane.showMessageDialog(desktop,                                          "Cannot open specified image: "+                                          in.getPath(),"Error",                                          JOptionPane.ERROR_MESSAGE);            mainFrame.setCursor(Cursor.DEFAULT_CURSOR);                            return;        }        // Image producer        try {            image = BlkImgDataSrcImageProducer.createImage(imr);        } catch(IllegalArgumentException e) {            JOptionPane.showMessageDialog(desktop,                                          "Cannot create image reader",                                          "Error",                                          JOptionPane.ERROR_MESSAGE);            mainFrame.setCursor(Cursor.DEFAULT_CURSOR);                            return;        }        rawFile = inFile;        rate = inFile.length()*8f/width/height;    }    /** Open the JPEG file, creates the Image instance and the rawFile */    private void openJPEG(File in) {        mainFrame.setCursor(Cursor.WAIT_CURSOR);                JPEGImageDecoder decoder = null;        try {            // Decode the JPEG file and create the BufferedImage instance            decoder = JPEGCodec.createJPEGDecoder(new FileInputStream(in));            image = decoder.decodeAsBufferedImage();            Raster raster = ((BufferedImage)image).getData();            ncImg = raster.getNumBands();            height = raster.getHeight();            width = raster.getWidth();                        // Save decoded image in a temporary PGM or PPM file            if(ncImg==1) { // PGM file                String header = "P5\n"+width+" "+height+"\n255\n";                                rawFile = File.createTempFile("decoded",".pgm");                BufferedOutputStream bos =                     new BufferedOutputStream(new FileOutputStream(rawFile));                bos.write(header.getBytes());                for(int i=0; i<height; i++) {                    for(int j=0; j<width; j++) {                        bos.write(raster.getSample(j,i,0));                    }                }                bos.close();            } else if(ncImg==3) { // PPM file                String header = "P6\n"+width+" "+height+"\n255\n";                                rawFile = File.createTempFile("decoded",".ppm");                BufferedOutputStream bos =                     new BufferedOutputStream(new FileOutputStream(rawFile));                bos.write(header.getBytes());                for(int i=0; i<height; i++) {                    for(int j=0; j<width; j++) {                        bos.write(raster.getSample(j,i,0));                        bos.write(raster.getSample(j,i,1));                        bos.write(raster.getSample(j,i,2));                    }                }                bos.close();            } else { // Not supported number of components                JOptionPane.showMessageDialog(desktop,"Error",                                              "Not supported JPEG format"+                                              inFile.getPath(),                                              JOptionPane.ERROR_MESSAGE);                mainFrame.setCursor(Cursor.DEFAULT_CURSOR);                return;            }                    } catch(IOException e) { // Error when decoding JPEG file            JOptionPane.showMessageDialog(desktop,inFile.getPath(),                                          "Unable to open JPEG file: "+                                          inFile.getPath(),                                          JOptionPane.ERROR_MESSAGE);            mainFrame.setCursor(Cursor.DEFAULT_CURSOR);                            return;        }        rate = inFile.length()*8f/width/height;    }    /**      * Open the JPEG 2000 file/codestream, creates the Image instance and the     * rawFile      * */    private void openJPEG2000(ParameterList pl) {        // Input file        pl.put("i",inFile.getPath());	        // Ouput file        tmpDecFile = null;	for(int c=0; c<ncImg; c++) {	    if(depth[c]>8) {		pgxNeeded = true;	    }	}        try {	    if(pgxNeeded) {		tmpDecFile = File.createTempFile("decoded", ".pgx");	    } else if(ncImg==3) {                tmpDecFile = File.createTempFile("decoded",".ppm");            } else {                tmpDecFile = File.createTempFile("decoded",".pgm");            }         } catch(IOException ioe) {            JOptionPane.showMessageDialog(null,inFile.getPath(),                                          "Cannot decode the codestream",                                          JOptionPane.ERROR_MESSAGE);            return;        }	tmpDecFile.delete();	tmpDecFile.deleteOnExit();        pl.put("o",tmpDecFile.getPath());		// Register the FacilityManager (Dialogs)        JJPopup mypop = new JJPopup(desktop);	FacilityManager.registerMsgLogger(null,mypop);	FacilityManager.registerProgressWatch(null,mainFrame);		// Instantiate decoder        dec = new Decoder(pl);        if(dec.getExitCode() != 0) { // An error ocurred	    FacilityManager.getMsgLogger().		printmsg(MsgLogger.ERROR,"An error ocurred "+			 "while instantiating the decoder.");            FacilityManager.getMsgLogger().flush();            return;        }	        // Run the decoder        dec.setChildProcess(true);	Thread thdec = new Thread(dec);		// Watch decoder termination	ThreadWatch tw = new ThreadWatch(this,thdec);	Thread thWatch = new Thread(tw);	thWatch.start();    }        /** Display the decoded image once the decoding is over */    public void terminatedThread() {        // Create the image        if(ncImg==1) {	    if(pgxNeeded) {		openPGX(tmpDecFile);	    } else {		openPGM(tmpDecFile);	    }        } else if(ncImg==3) {	    if(pgxNeeded) {		openThreePGX(tmpDecFile);	    } else {		openPPM(tmpDecFile);	    }        }        rawFile = tmpDecFile;        rate = j2kdecoder.getRate();                // Display image        String[] info = dec.getCOMInfo();        for(int i=1; i<info.length; i++) {            info[0] += " - "+info[i];        }	if(info.length>0) {	    displayImage(info[0],true);	} else {	    displayImage("",true);	}    }    /**      * Handles events happening to registered components used to open and     * display supported images.      * */    public void actionPerformed(ActionEvent e) {        Object o = e.getSource();                if(j2kdecoder!=null) {            if(o==j2kdecoder.decOptOkBut) { // Simple JPEG 2000 decoding                ParameterList pl = j2kdecoder.getSimpParameters();                openJPEG2000(pl);            } else if(o==j2kdecoder.decOkBut) { // Advanced JPEG 2000 decoding                ParameterList pl = j2kdecoder.getAdvParameters();                openJPEG2000(pl);            }         }    }}

⌨️ 快捷键说明

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