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

📄 saveimage.java

📁 jpeg2000编解码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                bos.write(bis.read());            }            bos.close();            bis.close();        } catch (IOException e) {            JOptionPane.showMessageDialog(null,"Cannot save to file "+out,                                          "Error",JOptionPane.ERROR_MESSAGE);            return false;        }        return true;            }    /**      * Encode the selected image with the JPEG format. It first ask for a     * quality factor thanks to a simple dialog box.      * */    private void saveJPEG() {        jpgQuality = new JSlider(JSlider.HORIZONTAL,0,100,75);        jpgQuality.setMajorTickSpacing(10);        jpgQuality.setMinorTickSpacing(1);        jpgQuality.setPaintTicks(true);        jpgQuality.setPaintLabels(true);        jpgQuality.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));        jpgQuality.addChangeListener(new ChangeListener() {                public void stateChanged(ChangeEvent e) {                    jpgDialog.setTitle("JPEG quality factor: "+                                      jpgQuality.getValue());                }            });        jpgDialog = new JDialog(mainFrame,"JPEG quality factor: "+                                jpgQuality.getValue());        JPanel pan = new JPanel();        pan.add(jpgQuality);        JButton okBut = new JButton("OK");        okBut.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent e) {                    jpgDialog.setVisible(false);                    try {                        encodeJPEG();                    } catch(IOException ioe) {                        JOptionPane.showMessageDialog(mainFrame,                                                      "Cannot save to JPEG",                                                      "Error",                                                      JOptionPane.                                                      ERROR_MESSAGE);                    }                    jpgDialog = null;                }            });        JButton cancelBut = new JButton("Cancel");        cancelBut.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent e) {                    jpgDialog.setVisible(false);                    jpgDialog = null;                }            });        pan.add(okBut);        pan.add(cancelBut);        jpgDialog.getContentPane().add(pan);        jpgDialog.pack();        jpgDialog.show();    }    private void encodeJPEG() throws IOException {        FileOutputStream os;        JPEGImageEncoder encoder;        JPEGEncodeParam param;        BufferedImage img;        int[] data;        ImgReader imr;        int nc = imgPan.getNumComps();        int width = imgPan.getOrigWidth();        int height = imgPan.getOrigHeight();        // Opeb BufferedImage        img = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);        if(nc==1) {            // Use ImgReaderPGM            imr = new ImgReaderPGM(inputFile.getPath());            // Get data            DataBlkInt db = new DataBlkInt();            db.ulx = db.uly = 0;            db.w = width; db.h = height;            db = (DataBlkInt)imr.getInternCompData(db,0);            data = db.getDataInt();                        // Remove DC offset and prepare the RGB data buffer            for(int i=width*height-1; i>=0; i--) {                data[i] = 0xff&(data[i]+128);                data[i] = (data[i]<<16) | (data[i]<<8) | data[i];            }        } else {            // Use ImgReaderPPM            imr = new ImgReaderPPM(inputFile.getPath());            // Get data            DataBlkInt db0 = new DataBlkInt();            db0.ulx = db0.uly = 0;            db0.w = width; db0.h = height;            db0 = (DataBlkInt)imr.getInternCompData(db0,0);            DataBlkInt db1 = new DataBlkInt();            db1.ulx = db1.uly = 0;            db1.w = width; db1.h = height;            db1 = (DataBlkInt)imr.getInternCompData(db1,1);            DataBlkInt db2 = new DataBlkInt();            db2.ulx = db2.uly = 0;            db2.w = width; db2.h = height;            db2 = (DataBlkInt)imr.getInternCompData(db2,2);            data = new int[width*height];            int[] data0 = db0.getDataInt();            int[] data1 = db1.getDataInt();            int[] data2 = db2.getDataInt();            // Remove DC offset and prepare the RGB data buffer            for(int i=width*height-1; i>=0; i--) {                data0[i] = 0xff&(data0[i]+128);                data1[i] = 0xff&(data1[i]+128);                data2[i] = 0xff&(data2[i]+128);                data[i] = (data0[i]<<16) | (data1[i]<<8) | data2[i];            }        }        img.setRGB(0,0,width,height,data,0,width);        imr.close();        os = new FileOutputStream(outFile);        encoder = JPEGCodec.createJPEGEncoder(os);        param = encoder.getDefaultJPEGEncodeParam(img);        param.setQuality(jpgQuality.getValue()/100f,true);        encoder.encode(img,param);        os.close();        // Show final bit-rate        double rate = outFile.length()*8d/width/height;        JOptionPane.showMessageDialog(mainFrame,"Ecoding bit-rate: "+                                      df.format(rate),"Info",                                      JOptionPane.INFORMATION_MESSAGE);    }    /**      * Encode the selected image with the JPEG 2000 format. It first     * terminates the ParameterList instance initialization and then call the     * JJ2000 encoder.      * */    private void saveJPEG2000(ParameterList pl) {        // Input file        pl.put("i",inputFile.getPath());                // Output file        pl.put("o",outFile.getPath());                if(fileType==FILE_TYPE_JP2) {            pl.put("file_format", "on");        }        pl.put("debug","on");	// Register the FacilityManager (Dialogs)        JJPopup mypop = new JJPopup(desktop);	FacilityManager.registerMsgLogger(null,mypop);	FacilityManager.registerProgressWatch(null,mainFrame);        	// Instantiate encoder        enc = new Encoder(pl);        if (enc.getExitCode() != 0) { // An error ocurred	    FacilityManager.getMsgLogger().		printmsg(MsgLogger.ERROR,"An error ocurred "+			 "while instantiating the encoder.");            FacilityManager.getMsgLogger().flush();            return;        }        // Run the encoder	Thread thenc = new Thread(enc);		// Watch the encoder termination	ThreadWatch tw = new ThreadWatch(this,thenc);	Thread thWatch = new Thread(tw);	thWatch.start();    }    /** Displayed encoding bit-rate when encoder is over */    public void terminatedThread() {        // Show final bit-rate        int width = imgPan.getOrigWidth();        int height = imgPan.getOrigHeight();        double rate = outFile.length()*8d/width/height;        JOptionPane.showMessageDialog(mainFrame,"Encoding bit-rate: "+                                      df.format(rate),"Info",                                      JOptionPane.INFORMATION_MESSAGE);    }    /** Handle operations happened on registered components */    public void actionPerformed(ActionEvent e) {        Object o = e.getSource();        if(j2kencoder!=null) {            if(o==j2kencoder.encOptOkBut) { // Simple JPEG 2000 encoding                ParameterList pl = j2kencoder.getSimpParameters();                saveJPEG2000(pl);                mainFrame.enableZoom(true);            } else if(o==j2kencoder.encOkBut) { // Advanced JPEG 2000 encoding)                j2kencoder.setSelectType(J2KGuiEncoder.NONE);		imgPan.setOffset(0,0);                ParameterList pl = j2kencoder.getAdvParameters();                saveJPEG2000(pl);                mainFrame.enableZoom(true);            }        }    }}

⌨️ 快捷键说明

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