Re: [問題]java做影像處理的參考資料?

看板java作者 (^_^)時間17年前 (2007/03/16 16:03), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串6/6 (看更多)
我也來分享一下以前寫的 code 好了 1. 程式由 FileChooser 讀路徑 2. ImageIO 把圖檔讀進 BufferedImage 3. 由 BufferedImage 取出影像陣列做處理 4. 把陣列轉回 Image 顯示 其中 4. 有二種作法 第二種是最近在 "電腦王" 雜誌上看到再新加的 大家可以參考一下 影像處理我還是推薦用 OpenCV 啦 import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.swing.*; import javax.imageio.*; public class ImgProcessFrame extends JFrame implements ActionListener{ JLabel status; // status bar JButton btn_browse, btn_process; JScrollPane pane_left, pane_right; JFileChooser chooser; String imgPath = null; JLabel img_left, img_right; public ImgProcessFrame(){ super("Image Process Frame"); // GUI JLabel lbl_browse = new JLabel("Choose Image:"); btn_browse = new JButton("Browse"); btn_browse.addActionListener(this); btn_process = new JButton("Process"); btn_process.addActionListener(this); JPanel upPanel = new JPanel(); // default: flow layout upPanel.add(lbl_browse); upPanel.add(btn_browse); upPanel.add(btn_process); pane_left = new JScrollPane(); pane_left.setColumnHeaderView(new JLabel("Input Image")); pane_right = new JScrollPane(); pane_right.setColumnHeaderView(new JLabel("Output Image")); JPanel ctrPanel = new JPanel(new GridLayout(1,2)); ctrPanel.add(pane_left); ctrPanel.add(pane_right); status = new JLabel("Status"); Container contentPane = getContentPane();// default: border layout contentPane.add(upPanel, BorderLayout.NORTH); contentPane.add(ctrPanel, BorderLayout.CENTER); contentPane.add(status, BorderLayout.SOUTH); // File chooser chooser = new JFileChooser(); //chooser.addChoosableFileFilter(new ImageFilter()); //chooser.setAcceptAllFileFilterUsed(false); //chooser.setAccessory(new ImagePreview(chooser)); img_left = new JLabel(); img_right = new JLabel(); // JFrame parameter setDefaultCloseOperation(EXIT_ON_CLOSE); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setBounds(20,20,screenSize.width-100,screenSize.height-100); setVisible(true); } public void actionPerformed(ActionEvent e){ // Get image if(e.getSource()==btn_browse){ int result = chooser.showOpenDialog(this); if(result == JFileChooser.APPROVE_OPTION){ try{ File fileObj = chooser.getSelectedFile(); imgPath = fileObj.getPath(); // display original image BufferedImage bfImg = ImageIO.read(new File(imgPath)); img_left.setIcon( new ImageIcon(bfImg) ); pane_left.setViewportView( img_left ); status.setText(" Image loaded " + fileObj.getPath()); }catch(IOException ioe){ ioe.printStackTrace(); } } } // Process image else if(e.getSource()==btn_process){ if(imgPath != null){ try{ BufferedImage bfImg = ImageIO.read(new File(imgPath)); int imgW = bfImg.getWidth(); int imgH = bfImg.getHeight(); int pixIn[] = new int[imgW*imgH]; bfImg.getRGB(0,0,imgW,imgH,pixIn,0,imgW); process(pixIn,imgW,imgH); // 建立 Image 的方法 1. Component.createImage /*Image img = createImage( new MemoryImageSource(imgW, imgH, pixIn, 0, imgW));*/ // 建立 Image 的方法 2. 直接建立 BufferedImage BufferedImage img = CreateBufferedImage_Direct(pixIn,imgW,imgH); img_right.setIcon(new ImageIcon(img)); pane_right.setViewportView( img_right ); }catch(IOException ioe){ ioe.printStackTrace(); } }else{ status.setText(" Image not yet loaded"); } } } // 處理影像的部分 public void process(int p[], int w, int h){ int r, g, b; for(int col=0; col<h-1; col++){ for(int row=0; row<w-1; row++){ r = (p[col*w+row] & 0xFF0000)>>16; g = (p[col*w+row] & 0xFF00)>>8; b = (p[col*w+row] & 0xFF); int mean = (r+g+b)/3; p[col*w+row] = 0xff000000 | mean<<16 | mean<<8 | mean; // 0xff0 00000 代表 alpha } } } public static BufferedImage CreateBufferedImage_Direct(int rgbData[], int widt h, int height){ // 建立 DataBuffer DataBuffer db = new DataBufferInt(rgbData,width*height); // 建立 Raster(包含 DataBuffer, SampleModel), 其中 SampleModel 自動用 SingleP ixelPackedSampleModel WritableRaster raster = Raster.createPackedRaster(db, width, height, width, new int[]{0xff0000,0x00ff00,0xff}, null); // 建立 ColorModel ColorModel cm = new DirectColorModel(24, 0xff0000, 0x00ff00, 0xff); // BufferedImage consists of 1. ColorModel, 2. Raster return new BufferedImage(cm, raster, false, null); } public static void main(String args[]){ JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); ImgProcessFrame ef = new ImgProcessFrame(); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.72.158
文章代碼(AID): #15-azSxN (java)
討論串 (同標題文章)
文章代碼(AID): #15-azSxN (java)