1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.event.*; public class BWImageMaker extends JFrame{ private BufferedImage bufimage=null; private ImageIcon icon=null; private JLabel label=null; private JScrollBar sbpixel=null; private JLabel lmessage=null; private String filepath=""; public BWImageMaker(){ super("BW Image Maker"); setSize(400,500); JButton bopen=new JButton("Open"); bopen.addActionListener(new ButtonHandler()); JPanel pmain=new JPanel(); label=new JLabel(); pmain.add(label); lmessage=new JLabel(""); sbpixel=new JScrollBar(JScrollBar.HORIZONTAL,255,0,0,255); sbpixel.addAdjustmentListener(new ScrollBarHandler()); JButton bbw=new JButton("BW"); bbw.addActionListener(new ButtonHandler()); JButton bsave=new JButton("Save"); bsave.addActionListener(new ButtonHandler()); JPanel pdown=new JPanel(); pdown.setPreferredSize(new Dimension(400,80)); pdown.setLayout(new GridLayout(4,1)); pdown.add(lmessage); pdown.add(sbpixel); pdown.add(bbw); pdown.add(bsave); setLayout(new BorderLayout()); add("North",bopen); add("Center",pmain); add("South",pdown); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="Open"){ JFileChooser fc=new JFileChooser(); int val=fc.showOpenDialog(BWImageMaker.this); if(val==JFileChooser.APPROVE_OPTION) { filepath=fc.getSelectedFile().getPath(); } if (filepath!= null){ try{ bufimage=ImageIO.read(new File(filepath)); }catch(Exception ex){ } icon=new ImageIcon(bufimage); label.setIcon(icon); } sbpixel.setValue(255); } if(e.getActionCommand()=="Save"){ JFileChooser fc=new JFileChooser(); int val=fc.showSaveDialog(BWImageMaker.this); if (val==JFileChooser.APPROVE_OPTION) { try{ File fileToSave=fc.getSelectedFile(); ImageIO.write(bufimage,"jpg",new File(fileToSave.getName()+".jpg")); }catch(Exception ex){ } } } if(e.getActionCommand()=="BW"){ if(bufimage==null){ return; } int width=bufimage.getWidth(); int height=bufimage.getHeight(); int counter=0; int pixels[]=new int[width*height]; for(int i=0;i<height;i++){ for(int j=0;j<width;j++){ pixels[counter]=bufimage.getRGB(j,i); counter++; } } counter=0; for(int i=0;i<height;i++){ for(int j=0;j<width;j++){ if(pixels[counter]!=-1){ pixels[counter]=0; bufimage.setRGB(j,i,pixels[counter]); } counter++; } } icon=new ImageIcon(bufimage); label.setIcon(icon); } } } class ScrollBarHandler implements AdjustmentListener{ public void adjustmentValueChanged(AdjustmentEvent e){ if(e.getSource()==sbpixel){ if(bufimage==null){ return; } if(filepath!= null){ try{ bufimage=ImageIO.read(new File(filepath)); }catch(Exception ex){ } icon=new ImageIcon(bufimage); label.setIcon(icon); } int width=bufimage.getWidth(); int height=bufimage.getHeight(); int counter=0; int pixels[]=new int[width*height]; for(int i=0;i<height;i++){ for(int j=0;j<width;j++){ pixels[counter]=bufimage.getRGB(j,i); counter++; } } int pixremoved=sbpixel.getValue(); lmessage.setText("Value: "+pixremoved); counter=0; for(int i=0;i<height;i++){ for(int j=0;j<width;j++){ Color c = new Color(pixels[counter]); int red=c.getRed(); int green=c.getGreen(); int blue=c.getBlue(); if(red>=pixremoved||green>=pixremoved||blue>=pixremoved){ bufimage.setRGB(j,i,-1); } else{ bufimage.setRGB(j,i,pixels[counter]); } counter++; } } icon=new ImageIcon(bufimage); label.setIcon(icon); } } } public static void main(String[] args){ new BWImageMaker(); } } |
Wednesday, April 20, 2016
BW Image Converter
This short program turns a color image into black and white. The user can set the threshold for the RGB value to remove. All pixels with an RGB value higher than that wil be removed. Clicking the BW button will turn the image pure black/white.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment