215 lines
7.3 KiB
Java
215 lines
7.3 KiB
Java
package edu.hsog.db;
|
|
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.sql.Blob;
|
|
import java.sql.Connection;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
import javax.imageio.ImageIO;
|
|
import javax.swing.ImageIcon;
|
|
import javax.swing.Icon;
|
|
|
|
|
|
public class Converter {
|
|
|
|
public static Icon loadIconFromFile(String path) {
|
|
ImageIcon icon = new ImageIcon(path);
|
|
Image image = icon.getImage();
|
|
int maxX, maxY, max;
|
|
double ratio = 0;
|
|
maxX = image.getWidth(null);
|
|
maxY = image.getHeight(null);
|
|
max = Math.max(maxX, maxY);
|
|
ratio = (250.0 / (float) max);
|
|
image = image.getScaledInstance((int) (maxX * ratio),
|
|
(int) (maxY * ratio), Image.SCALE_DEFAULT);
|
|
ImageIcon ic = new ImageIcon(image);
|
|
return (Icon) ic;
|
|
}
|
|
|
|
public static ImageIcon image2ImageIcon(Image i) {
|
|
if (i == null) return null;
|
|
|
|
ImageIcon ic = new ImageIcon(i);
|
|
return ic;
|
|
}
|
|
|
|
public static Image imageIcon2Image(ImageIcon ic) {
|
|
if (ic == null) return null;
|
|
|
|
Image i = ic.getImage();
|
|
return i;
|
|
}
|
|
|
|
public static Blob imageIcon2Blob(ImageIcon myImageIcon, Connection con) {
|
|
if (myImageIcon == null) return null;
|
|
Blob myBlob = null;
|
|
|
|
try {
|
|
|
|
ByteArrayOutputStream myByteArrayOutputStream = new ByteArrayOutputStream();
|
|
BufferedImage buImg = new BufferedImage(myImageIcon.getIconWidth(), myImageIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
|
|
buImg.getGraphics().drawImage(myImageIcon.getImage(), 0, 0, myImageIcon.getImageObserver());
|
|
ImageIO.write(buImg, "png", myByteArrayOutputStream);
|
|
byte[] myByteArray = myByteArrayOutputStream.toByteArray();
|
|
myBlob = con.createBlob();
|
|
myBlob.setBytes(1, myByteArray);
|
|
} catch (Exception ex) {
|
|
Logger.getLogger(Converter.class.getName()).log(Level.SEVERE, null, ex);
|
|
}
|
|
return myBlob;
|
|
}
|
|
|
|
public static Blob icon2Blob(Icon myIcon, Connection con) {
|
|
return imageIcon2Blob((ImageIcon) myIcon, con);
|
|
}
|
|
|
|
public static ImageIcon blob2ImageIcon(Blob myBlob) {
|
|
|
|
if (myBlob == null) return null;
|
|
|
|
ImageIcon myImageIcon = null;
|
|
try {
|
|
byte[] byteArray = myBlob.getBytes((long) 1, (int) myBlob.length());
|
|
myImageIcon = new ImageIcon(byteArray);
|
|
|
|
} catch (SQLException ex) {
|
|
Logger.getLogger(Converter.class.getName()).log(Level.SEVERE, null, ex);
|
|
}
|
|
return myImageIcon;
|
|
}
|
|
|
|
public static Icon blob2Icon(Blob myBlob) {
|
|
return (Icon) blob2ImageIcon(myBlob);
|
|
}
|
|
|
|
public static BufferedImage iconToBufferedImage(Icon icon) {
|
|
// Create a buffered image with the same dimensions as the icon
|
|
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
|
|
Graphics g = image.createGraphics();
|
|
|
|
// Paint the icon onto the buffered image
|
|
icon.paintIcon(null, g, 0, 0);
|
|
g.dispose();
|
|
|
|
return image;
|
|
}
|
|
|
|
public static Icon mergeIcons(ArrayList<Icon> icons) {
|
|
int iconsPerRow = 3;
|
|
if (icons == null || icons.isEmpty()) {
|
|
throw new IllegalArgumentException("Die Icon-Liste darf nicht leer sein.");
|
|
}
|
|
if (iconsPerRow <= 0) {
|
|
throw new IllegalArgumentException("Die Anzahl der Icons pro Zeile muss größer als 0 sein.");
|
|
}
|
|
|
|
// Berechnung der Dimensionen des kombinierten Icons
|
|
int rows = (int) Math.ceil((double) icons.size() / iconsPerRow); // Gesamtanzahl der Zeilen
|
|
int maxWidth = 0;
|
|
int totalHeight = 0;
|
|
int[] rowHeights = new int[rows]; // Speichert die maximale Höhe jeder Zeile
|
|
|
|
for (int row = 0; row < rows; row++) {
|
|
int currentRowWidth = 0;
|
|
int currentRowHeight = 0;
|
|
|
|
for (int col = 0; col < iconsPerRow; col++) {
|
|
int index = row * iconsPerRow + col;
|
|
if (index >= icons.size()) break; // Keine weiteren Icons
|
|
|
|
Icon icon = icons.get(index);
|
|
currentRowWidth += icon.getIconWidth();
|
|
currentRowHeight = Math.max(currentRowHeight, icon.getIconHeight());
|
|
}
|
|
|
|
maxWidth = Math.max(maxWidth, currentRowWidth);
|
|
totalHeight += currentRowHeight;
|
|
rowHeights[row] = currentRowHeight;
|
|
}
|
|
|
|
// Neues kombiniertes Bild erstellen
|
|
BufferedImage combinedImage = new BufferedImage(maxWidth, totalHeight, BufferedImage.TYPE_INT_ARGB);
|
|
Graphics g = combinedImage.getGraphics();
|
|
|
|
// Icons zeichnen
|
|
int currentY = 0;
|
|
for (int row = 0; row < rows; row++) {
|
|
int currentX = 0;
|
|
for (int col = 0; col < iconsPerRow; col++) {
|
|
int index = row * iconsPerRow + col;
|
|
if (index >= icons.size()) break;
|
|
|
|
Icon icon = icons.get(index);
|
|
icon.paintIcon(null, g, currentX, currentY);
|
|
currentX += icon.getIconWidth();
|
|
}
|
|
currentY += rowHeights[row];
|
|
}
|
|
|
|
g.dispose();
|
|
|
|
// Kombiniertes Bild als Icon zurückgeben
|
|
return new ImageIcon(resizeTo250x250(combinedImage));
|
|
}
|
|
|
|
public static BufferedImage resizeTo250x250(BufferedImage originalImage) {
|
|
if (originalImage == null) {
|
|
throw new IllegalArgumentException("Das Bild darf nicht null sein.");
|
|
}
|
|
|
|
// Neue Dimensionen für das Bild
|
|
int targetWidth = 200;
|
|
int targetHeight = 300;
|
|
|
|
// Erstelle ein neues BufferedImage mit den Zielmaßen
|
|
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
|
|
|
|
// Zeichne das originale Bild auf das neue Bild mit Skalierung
|
|
Graphics2D g2d = resizedImage.createGraphics();
|
|
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
|
g2d.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
|
|
g2d.dispose();
|
|
|
|
return resizedImage;
|
|
}
|
|
|
|
public static Icon generatePatternIcon() {
|
|
// Bildgröße
|
|
int width = 250;
|
|
int height = 250;
|
|
|
|
// Erstelle ein BufferedImage
|
|
BufferedImage patternImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
|
|
|
// Muster erzeugen (z.B. Rechteckmuster mit zufälligen Farben)
|
|
Graphics2D g2d = patternImage.createGraphics();
|
|
|
|
for (int y = 0; y < height; y += 25) { // Abstand der horizontalen Streifen
|
|
for (int x = 0; x < width; x += 25) { // Abstand der vertikalen Streifen
|
|
// Erzeuge eine zufällige Farbe
|
|
Color randomColor = new Color(
|
|
(int) (Math.random() * 255), // Rot
|
|
(int) (Math.random() * 255), // Grün
|
|
(int) (Math.random() * 255) // Blau
|
|
);
|
|
|
|
// Zeichne Rechtecke für das Muster
|
|
g2d.setColor(randomColor);
|
|
g2d.fillRect(x, y, 25, 25);
|
|
}
|
|
}
|
|
|
|
g2d.dispose();
|
|
|
|
// Rückgabe des generierten Musters als Icon
|
|
return new ImageIcon(patternImage);
|
|
}
|
|
}
|
|
|
|
|