import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.apache.commons.codec.binary.Base64;
public class ImageTransformation{
private static BufferedImage toBufferedImage(Image src) {
int w = src.getWidth(null);
int h = src.getHeight(null);
int type = BufferedImage.TYPE_INT_RGB; // other options
BufferedImage dest = new BufferedImage(w, h, type);
Graphics2D g2 = dest.createGraphics();
g2.drawImage(src, 0, 0, null);
g2.dispose();
return dest;
}
public static void main(String[] args) throws FileNotFoundException, IOException{
Image image = ImageIO.read(new FileInputStream(new File("probescan.jpeg")));
JOptionPane.showConfirmDialog(null, new Object[]{new JLabel("Image 1"),new JLabel(new ImageIcon(image))});
Base64 b64 = new Base64();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BufferedImage bi = toBufferedImage(image);
ImageIO.write(bi, "jpeg", bos);
//Das ist der Base64 codierte String der das Bild repräsentiert
String imageAsBase64 = b64.encodeToString(bos.toByteArray());
System.out.println(imageAsBase64);
//Ab hier wird aus dem String wieder ein Bild erstellt.
byte[] nArray = b64.decode(imageAsBase64);
Image image2 = Toolkit.getDefaultToolkit().createImage(nArray);
JOptionPane.showConfirmDialog(null, new Object[]{new JLabel("Image 2"),new JLabel(new ImageIcon(image2))});
}
}