Bild -> lokale Variable -> DB -> lokale Variable

xerion21

Mitglied
Hallo zusammen,

ich habe momentan folgendes Problem, zu dem ich bis jetzt noch keine zufriedenstellende Lösung gefunden habe.
Ich würde gerne ein Bild (.jpg) in eine lokale Variable speichern.
  1. Diese soll dann per JLabel in einem ImageIcon angezeigt werden.
  2. Per Befehl in der Datenbank (MySQL-Blob) gespeichert werden.

Nachdem ich die Daten zu dem Bild und das Bild gespeichert hätte, würde ich gerne das Bild aus der Datenbank auslesen und in einer lokalen Variable gespeichert werden. Diese soll dann wieder als ImageIcon angezgeigt werden.

Hat jemand eine Idee, wie ich diese 3 Funktionen sinnvoll verwirklichen kann?
 
mit diesen beiden Funktionen kannst du ein ImageIcon in ein byte[] umwandeln und ein byte[] wieder zu einem ImageIcon

ggf. musst du das byte[] noch für deine db umwandeln, aber die meisten DBMS unterstützen byte[] als typ.

Java:
	public static ImageIcon bytesToIcon(byte[] imageData) throws IOException
	{
		BufferedImage imag=ImageIO.read(new ByteArrayInputStream(imageData));
		return new ImageIcon(imag);
	}
	
	public static byte[] iconToBytes(ImageIcon icon, String format) throws IOException
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		Image img = icon.getImage();
		
		BufferedImage bi = new BufferedImage(img.getWidth(null),img.getHeight(null),
                BufferedImage.TYPE_4BYTE_ABGR);
		
		Graphics2D g2 = bi.createGraphics();
		g2.drawImage(img, 0, 0, null);
		g2.dispose();
		
		ImageIO.write(bi, format, baos);
		bi.flush();
		byte[] data = baos.toByteArray();
		return data;
	}


und nochmal der komplette Demo-Code
Java:
package ImageIconAsBytes;

import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ImageIconDemo extends JFrame
{
	public static void main(String[] args) throws IOException
	{
		new ImageIconDemo();
	}
	
	public ImageIconDemo() throws IOException
	{
		this.setSize(500, 500);
		this.setLayout(new FlowLayout());

		// imageIcon load from file
		ImageIcon fileBasedIcon = new ImageIcon("src/ImageIconAsBytes/demo.png");
		// display it to ensure that the originally image has been loaded correct
		this.add(new JLabel(fileBasedIcon));
		
		// convert image to bytes
		byte[] iconAsBytes = iconToBytes(fileBasedIcon, "png");
		
		// convert the bytes back to original image
		ImageIcon reCreatedImageIconByBytes = bytesToIcon(iconAsBytes);
		
		this.add(new JLabel(reCreatedImageIconByBytes));
		
		this.setVisible(true);
	}
	
	
	public static ImageIcon bytesToIcon(byte[] imageData) throws IOException
	{
		BufferedImage imag=ImageIO.read(new ByteArrayInputStream(imageData));
		return new ImageIcon(imag);
	}
	
	public static byte[] iconToBytes(ImageIcon icon, String format) throws IOException
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		Image img = icon.getImage();
		
		BufferedImage bi = new BufferedImage(img.getWidth(null),img.getHeight(null),
                BufferedImage.TYPE_4BYTE_ABGR);
		
		Graphics2D g2 = bi.createGraphics();
		g2.drawImage(img, 0, 0, null);
		g2.dispose();
		
		ImageIO.write(bi, format, baos);
		bi.flush();
		byte[] data = baos.toByteArray();
		return data;
	}
}
 

Zurück
Oben