public class TestGUI
extends JFrame
{
public TestGUI()
throws Exception
{
DisplayPanel p = new DisplayPanel();
add(p);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setVisible(true);
Thread.sleep(2000);
p.BildInvertieren();
p.repaint();
}
public static void main(String[] args)
throws Exception
{
new TestGUI();
}
}
class DisplayPanel
extends JPanel
{
// Image displayImage = null;
BufferedImage speicherbares, biSrc, biDest, bi, temp;
Graphics2D big;
RescaleOp rescale;
RescaleOp rescale2;
float kontrastFaktor; // 1.0f;
float helligkeitOffset; // 10;
float gammaFaktor; // 1.0f;
boolean helligkeitNegi, kontrastPosi, gammaButton;
int w;
int h;
// Pfad zum Desktop
static String DB = "C:/Dokumente und Einstellungen/scanset.bachmann/DB";
// Bild nach dem Scannen
File scannerBild = new File(DB + "/Kamera/image.jpg");
// Bild nach der Berabeitung vor dem Auswerten
static File nachBearbeiten = new File(DB + "/Kamera Bild/imageB.bmp");
DisplayPanel()
throws IOException
{
setBackground(Color.black);
loadImage();
setSize(speicherbares.getWidth(this), speicherbares.getWidth(this));
createBufferedImages();
}
public void setPreferredSize(int i, int j)
{
setSize(i, j);
}
public void loadImage()
throws IOException
{
final BufferedImage a = new BufferedImage(50, 100, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < a.getWidth(); i++)
{
for (int j = 0; j < a.getHeight(); j++)
{
a.setRGB(i, j, (i + j) * 2);
}
}
speicherbares = a;
// speicherbares = ImageIO.read(scannerBild);
// MediaTracker mt1 = new MediaTracker(this);
// mt1.addImage(speicherbares, 1);
// try
// {
// mt1.waitForAll();
// }
// catch (Exception e)
// {
// System.out.println("Exception while loading.");
// }
//
// if (speicherbares.getWidth(this) == -1)
// {
// System.out.println("No jpg file");
// }
w = speicherbares.getWidth(this);
h = speicherbares.getHeight(this);
System.out.println("w: " + w + ", " + h);
// mt1 = null;
}
public void wertUebergabe(float helligkeit, float kontrast, float gamma)
{
helligkeitOffset = helligkeit;
kontrastFaktor = kontrast;
gammaFaktor = gamma;
}
public void createBufferedImages()
{
biSrc = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
big = biSrc.createGraphics();
big.drawImage(speicherbares, 0, 0, this);
biDest = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
bi = biSrc;
}
public float changeHelligkeit()
{
if (helligkeitNegi)
{
if (helligkeitOffset < 255.0) helligkeitOffset = helligkeitOffset + 5.0f;
}
else
{
if (helligkeitOffset > -255.0) helligkeitOffset = helligkeitOffset - 5.0f;
}
return Math.round(helligkeitOffset);
}
public float changeKontrast()
{
if (kontrastPosi)
{
if (kontrastFaktor < 2.0) kontrastFaktor = kontrastFaktor + 0.1f;
}
else
{
if (kontrastFaktor > -2.0) kontrastFaktor = kontrastFaktor - 0.1f;
}
return Math.round(kontrastFaktor);
}
public float changeGamma()
{
if (gammaButton)
{
if (gammaFaktor < 500.0) gammaFaktor = gammaFaktor - 0.01f;
}
else
{
if (gammaFaktor > -500.0) gammaFaktor = gammaFaktor + 0.01f;
}
return Math.round(gammaFaktor);
}
public void BildInvertieren()
{
temp = rescale2();
biDest = invert(speicherbares);
bi = biDest;
System.out.println("Bild inertiren");
}
public void bildAktual()
{
// aufruf Methode
temp = rescale2();
biDest = setGamma(temp, gammaFaktor);
bi = biDest;
}
public BufferedImage rescale2()
{
rescale2 = new RescaleOp(kontrastFaktor, helligkeitOffset, 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.paintComponents(g);
Graphics2D g2D = (Graphics2D)g;
g2D.drawImage(bi, 0, 0, this);
}
public void speichern()
throws IOException
{
BufferedImage output = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
output.getGraphics().drawImage(bi, 0, 0, null);
ImageIO.write(output, "BMP", nachBearbeiten);
speicherbares = null;
// output = null;
biSrc = null;
biDest = null;
bi = null;
temp = null;
big = null;
System.gc();
// Aufruf der Eyesight
// DatenAustEyeSight.ablauf();
}
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;
}
public BufferedImage invert(BufferedImage image)
{
int w = image.getWidth(), h = image.getHeight();
BufferedImage bearbeiten = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
int neu = 255 + 256 * 255 + 256 * 256 * 255 - image.getRGB(x, y);
bearbeiten.setRGB(x, y, neu);
}
}
return bearbeiten;
}
}