import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class Berabeiten extends JFrame {
DisplayPanel displayPanel;
JButton brightenButton, darkenButton,
contIncButton, contDecButton,
jBspeichern, jBGammaPos,
jBGammaNeg;
@SuppressWarnings("deprecation")
public Berabeiten() throws IOException {
super();
Container container = getContentPane();
displayPanel = new DisplayPanel();
container.add(displayPanel);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 3));
panel.setBorder(new TitledBorder(
"Klicke ein Button um Kontrast und helligkeit zu ändern"));
brightenButton = new JButton("Helligkeit >>");
brightenButton.addActionListener(new ButtonListener());
darkenButton = new JButton("Helligkeit <<");
darkenButton.addActionListener(new ButtonListener());
contIncButton = new JButton("Kontrast >>");
contIncButton.addActionListener(new ButtonListener());
contDecButton = new JButton("Kontrast <<");
contDecButton.addActionListener(new ButtonListener());
jBspeichern = new JButton("Speichern");
jBspeichern.addActionListener(new ButtonListener());
jBGammaPos = new JButton("Gamma >>");
jBGammaPos.addActionListener(new ButtonListener());
jBGammaNeg = new JButton("Gamma <<");
jBGammaNeg.addActionListener(new ButtonListener());
panel.add(brightenButton);
panel.add(darkenButton);
panel.add(contIncButton);
panel.add(contDecButton);
panel.add(jBspeichern);
panel.add(jBGammaPos);
panel.add(jBGammaNeg);
container.add(BorderLayout.SOUTH, panel);
addWindowListener(new WindowEventHandler());
setSize(displayPanel.getWidth(), displayPanel.getHeight() + 10);
show(); // Display the frame
}
class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public static void main(String arg[]) throws IOException {
new Berabeiten();
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton temp = (JButton) e.getSource();
if (temp.equals(brightenButton)) {
displayPanel.brighten = true;
displayPanel.changeOffSet();
System.out.println(displayPanel.offset + "=offset");
//displayPanel.rescale();
displayPanel.bildAktual();
displayPanel.repaint();
}
else if (temp.equals(darkenButton)) {
displayPanel.brighten = false;
displayPanel.changeOffSet();
System.out.println(displayPanel.offset + "=offset");
//displayPanel.rescale();
displayPanel.bildAktual();
displayPanel.repaint();
}
else if (temp.equals(contIncButton)) {
displayPanel.contrastInc = true;
displayPanel.changeScaleFactor();
System.out.println(displayPanel.scaleFactor + "=scaleF");
//displayPanel.rescale();
displayPanel.bildAktual();
displayPanel.repaint();
}
else if (temp.equals(contDecButton)) {
displayPanel.contrastInc = false;
displayPanel.changeScaleFactor();
System.out.println(displayPanel.scaleFactor + "=scaleF");
//displayPanel.rescale();
displayPanel.bildAktual();
displayPanel.repaint();
}
else if (temp.equals(jBspeichern)) {
try {
displayPanel.speichern();
} catch (IOException e1) {
System.out.println("Fehler beim speichern");
}
}
else if(temp.equals(jBGammaPos)) {
displayPanel.gammaButton = true;
//displayPanel.setGamma(displayPanel.changeGamma());
//displayPanel.rescale();
displayPanel.changeGamma();
displayPanel.bildAktual();
displayPanel.repaint();
}
else if(temp.equals(jBGammaNeg)) {
displayPanel.gammaButton = false;
//displayPanel.setGamma(displayPanel.changeGamma());
//displayPanel.rescale();
displayPanel.changeGamma();
displayPanel.bildAktual();
displayPanel.repaint();
}
}
}
}
@SuppressWarnings("serial")
class DisplayPanel extends JPanel {
Image displayImage;
BufferedImage biSrc, biDest, bi, temp;
Graphics2D big;
RescaleOp rescale;
RescaleOp rescale2;
float scaleFactor = 1.0f;
float offset = 10;
float gammaFaktor = 1.0f;
boolean brighten, contrastInc, gammaButton;
DisplayPanel() throws IOException {
setBackground(Color.black);
loadImage();
setSize(displayImage.getWidth(this),
displayImage.getWidth(this));
createBufferedImages();
}
public void loadImage() throws IOException {
// Bild einlesen
BufferedImage imgIn = ImageIO.read(new File("C:/Dokumente und Einstellungen/rs.KE/workspace/Bachmann Forming/Kamera Bild/image.jpg"));
// Um 90% verkleinern
AffineTransformOp atop = new AffineTransformOp(AffineTransform.getScaleInstance(0.18, 0.18), AffineTransformOp.TYPE_BICUBIC);
// Ausgabe
BufferedImage imgOut = atop.createCompatibleDestImage(imgIn, ColorModel.getRGBdefault());
// Bild verkleinern
atop.filter(imgIn, imgOut);
displayImage = imgOut;
MediaTracker mt = new MediaTracker(this);
mt.addImage(displayImage, 1);
try {
mt.waitForAll();
} catch (Exception e) {
System.out.println("Exception while loading.");
}
if (displayImage.getWidth(this) == -1) {
System.out.println("No jpg file");
System.exit(0);
}
}
public void createBufferedImages() {
biSrc = new BufferedImage(displayImage.getWidth(this),
displayImage.getHeight(this),
BufferedImage.TYPE_INT_RGB);
big = biSrc.createGraphics();
big.drawImage(displayImage, 0, 0, this);
biDest = new BufferedImage(displayImage.getWidth(this),
displayImage.getHeight(this),
BufferedImage.TYPE_INT_RGB);
bi = biSrc;
}
public void changeOffSet() {
if (brighten) {
if (offset < 255)
offset = offset+5.0f;
}
else {
if (offset > -255)
offset = offset-5.0f;
}
}
public void changeScaleFactor() {
if (contrastInc) {
if (scaleFactor < 2)
scaleFactor = scaleFactor+0.1f;
}
else {
if (scaleFactor > -2)
scaleFactor = scaleFactor-0.1f;
}
}
public void changeGamma() {
if (gammaButton) {
if (gammaFaktor < 500)
gammaFaktor = gammaFaktor-0.02f;
}
else {
if (gammaFaktor > 0)
gammaFaktor = gammaFaktor+0.02f;
}
System.out.println(gammaFaktor);
}
public void bildAktual() {
// aufruf Methode
temp = rescale2();
biDest = setGamma(temp, gammaFaktor);
bi = biDest;
}
public void rescale() {
rescale = new RescaleOp(scaleFactor, offset, null);
rescale.filter(biSrc, biDest);
bi = biDest;
}
public BufferedImage rescale2() {
rescale2 = new RescaleOp(scaleFactor, offset, null);
rescale2.filter(biSrc, biDest);
return biDest;
}
public void update(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
paintComponent(g);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(bi, 0, 0, this);
}
public void speichern() throws IOException {
ImageIO.write(bi, "BMP", new File("C:/Dokumente und Einstellungen/rs.KE/workspace/Bachmann Forming/Kamera Bild/image.jpg"));
}
public BufferedImage setGamma(BufferedImage image, double gammaFactor)
{
double gammaPotency = 1.0 / gammaFactor;
int w = image.getWidth(), h = image.getHeight();
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
int argb = image.getRGB(x, y);
int nargb = ((argb >> 24) & 0xFF) << 24; // set alpha
for (int i = 0; i < 24; i += 8)
{
int value = (int) (Math.pow((argb >> i) & 0xFF, gammaPotency));
nargb += (int) ((value > 0xFF) ? (0xFF << i) : (value << i));
}
image.setRGB(x, y, nargb);
}
}
return image;
}
}