
選擇Application WindowApplication Name: AdjContrastDemoLayout: Absolute layout新增兩組JSlider元件新增兩個static變數 alpha、beta完整原始碼
1: package Basic;
2:
3: import java.awt.EventQueue;
4: import java.awt.image.BufferedImage;
5:
6: import javax.swing.ImageIcon;
7: import javax.swing.JFrame;
8: import javax.swing.JOptionPane;
9: import javax.swing.JSlider;
10: import javax.swing.JLabel;
11: import javax.swing.event.ChangeListener;
12: import javax.swing.event.ChangeEvent;
13:
14: import org.opencv.core.Core;
15: import org.opencv.core.Mat;
16: import org.opencv.imgcodecs.Imgcodecs;
17: import org.opencv.imgproc.Imgproc;
18:
19: public class AdjContrastDemo { 20: static{System.loadLibrary(Core.NATIVE_LIBRARY_NAME);} 21: private JFrame frmContrastAdjustment;
22: static double alpha = 1;
23: static double beta = 50;
24: JSlider slider;
25: JSlider slider_1;
26: JLabel lblAlphaVal;
27: JLabel lblBetaVal;
28: JLabel lblImage;
29: static Mat imgSrc;
30: static Mat imgG = new Mat();
31: static Mat imgDst = new Mat();
32: /**
33: * Launch the application.
34: */
35: public static void main(String[] args) { 36: EventQueue.invokeLater(new Runnable() { 37: public void run() { 38: try { 39: //imgSrc = Imgcodecs.imread("C:\\OpenCV\\images\\left01.jpg"); 40: imgSrc = Imgcodecs.imread("C:\\OpenCV\\images\\lena.jpg"); 41: if(imgSrc.empty())
42: { 43: JOptionPane.showMessageDialog(null, "無法讀取圖片","警告",
44: JOptionPane.WARNING_MESSAGE);
45: }
46: if(imgSrc.channels()==3)
47: imgSrc.copyTo(imgG);
48: //Imgproc.cvtColor(imgSrc, imgG, Imgproc.COLOR_BGR2GRAY);
49: else
50: imgSrc.copyTo(imgG);
51: //imgDst = new Mat();
52: AdjContrastDemo window = new AdjContrastDemo();
53: window.frmContrastAdjustment.setVisible(true);
54: } catch (Exception e) { 55: e.printStackTrace();
56: }
57: }
58: });
59: }
60:
61: /**
62: * Create the application.
63: */
64: public AdjContrastDemo() { 65: initialize();
66: }
67:
68: /**
69: * Initialize the contents of the frame.
70: */
71: private void initialize() { 72: frmContrastAdjustment = new JFrame();
73: frmContrastAdjustment.setTitle("Contrast Adjustment"); 74: frmContrastAdjustment.setBounds(100, 100, 725, 692);
75: frmContrastAdjustment.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
76: frmContrastAdjustment.getContentPane().setLayout(null);
77:
78: slider = new JSlider();
79: slider.setValue((int)alpha);
80: slider.addChangeListener(new ChangeListener() { 81: public void stateChanged(ChangeEvent arg0) { 82: lblAlphaVal.setText(slider.getValue()+"");
83: alpha = slider.getValue();
84: AdjustContrast();
85: }
86: });
87:
88: slider.setBounds(125, 40, 370, 19);
89: frmContrastAdjustment.getContentPane().add(slider);
90:
91: slider_1 = new JSlider();
92: slider_1.setValue((int)beta);
93: slider_1.addChangeListener(new ChangeListener() { 94: public void stateChanged(ChangeEvent arg0) { 95: lblBetaVal.setText(slider_1.getValue()+"");
96: beta = slider_1.getValue();
97: AdjustContrast();
98: }
99: });
100: slider_1.setBounds(125, 90, 370, 19);
101: frmContrastAdjustment.getContentPane().add(slider_1);
102:
103: JLabel lblAlpha = new JLabel();
104: lblAlpha.setText("alpha"); 105: lblAlpha.setBounds(55, 29, 71, 36);
106: frmContrastAdjustment.getContentPane().add(lblAlpha);
107:
108: JLabel lblBeta = new JLabel();
109: lblBeta.setText("beta"); 110: lblBeta.setBounds(55, 80, 71, 36);
111: frmContrastAdjustment.getContentPane().add(lblBeta);
112:
113: lblAlphaVal = new JLabel("0"); 114: lblAlphaVal.setBounds(499, 29, 55, 36);
115: lblAlphaVal.setText(""+alpha); 116: frmContrastAdjustment.getContentPane().add(lblAlphaVal);
117:
118: lblBetaVal = new JLabel("0"); 119: lblBetaVal.setBounds(499, 80, 55, 36);
120: lblBetaVal.setText(""+beta); 121: frmContrastAdjustment.getContentPane().add(lblBetaVal);
122:
123: lblImage = new JLabel(""); 124: lblImage.setBounds(31, 154, 587, 467);
125: frmContrastAdjustment.getContentPane().add(lblImage);
126: }
127: public void AdjustContrast()
128: { 129: imgG.convertTo(imgDst, -1, alpha, beta);
130: BufferedImage bufImg = matToBufferedImage(imgDst);
131: lblImage.setIcon(new ImageIcon(bufImg));
132: }
133: public BufferedImage matToBufferedImage(Mat matrix){ 134: int cols = matrix.cols();
135: int rows = matrix.rows();
136: int elemSize = (int)matrix.elemSize();
137: byte[] data = new byte[cols*rows*elemSize];
138: if(matrix.channels()==3)
139: Imgproc.cvtColor(matrix, matrix, Imgproc.COLOR_BGR2RGB);
140: matrix.get(0, 0, data);
141: BufferedImage bufImg = null;
142: switch(matrix.channels())
143: { 144: case 1:
145: bufImg = new BufferedImage(cols, rows, BufferedImage.TYPE_BYTE_GRAY);
146: break;
147: case 3:
148: bufImg = new BufferedImage(cols,rows, BufferedImage.TYPE_3BYTE_BGR);
149: break;
150: }
151: bufImg.getRaster().setDataElements(0, 0, cols, rows, data);
152: return bufImg;
153: }
154: }