This commit is contained in:
JunkJumper 2020-05-05 11:24:05 +02:00
parent 40ad1cb599
commit e1baaa02f8
2 changed files with 16 additions and 78 deletions

View File

@ -1,62 +0,0 @@
package database;
/**
* @author James Thompson
* @source https://gist.github.com/jamesthompson/3344090
* @date 08/12/2012
**/
import java.io.ByteArrayInputStream;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javafx.scene.image.Image;
import javax.imageio.ImageIO;
import java.awt.image.*;
import java.io.ByteArrayOutputStream;
public class ByteaToCardImage {
public static Image getJavaFXImage(byte[] rawPixels) {
//public Image getJavaFXImage(byte[] rawPixels, int width, int height) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write((RenderedImage) createBufferedImage(rawPixels, 467, 652), "png", out);
//ImageIO.write((RenderedImage) createBufferedImage(rawPixels, width, height), "png", out);
out.flush();
} catch (IOException ex) {
}
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
return new javafx.scene.image.Image(in);
}
private static BufferedImage createBufferedImage(byte[] pixels, int width, int height) {
SampleModel sm = getIndexSampleModel(width, height);
DataBuffer db = new DataBufferByte(pixels, width*height, 0);
WritableRaster raster = Raster.createWritableRaster(sm, db, null);
IndexColorModel cm = getDefaultColorModel();
BufferedImage image = new BufferedImage(cm, raster, false, null);
return image;
}
private static SampleModel getIndexSampleModel(int width, int height) {
IndexColorModel icm = getDefaultColorModel();
WritableRaster wr = icm.createCompatibleWritableRaster(1, 1);
SampleModel sampleModel = wr.getSampleModel();
sampleModel = sampleModel.createCompatibleSampleModel(width, height);
return sampleModel;
}
private static IndexColorModel getDefaultColorModel() {
byte[] r = new byte[256];
byte[] g = new byte[256];
byte[] b = new byte[256];
for(int i=0; i<256; i++) {
r[i]=(byte)i;
g[i]=(byte)i;
b[i]=(byte)i;
}
IndexColorModel defaultColorModel = new IndexColorModel(8, 256, r, g, b);
return defaultColorModel;
}
}

View File

@ -2,13 +2,11 @@ package database;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javafx.scene.image.Image;
/*
import java.sql.Connection;
import java.sql.DriverManager;
@ -23,20 +21,22 @@ public class DatabaseTesting {
a.remplirTableAllFrom("CartesAll");
System.out.println(a.toString());
Image image = ByteaToCardImage.getJavaFXImage(a.getList().get(1).getImg());
System.out.println(image);
//a.getList().get(1).getImg()
//a.getList().get(1).getImg();
}
BufferedImage image = ImageIO.read( new ByteArrayInputStream( a.getList().get(1).getImg() ) );
ImageIO.write(image, "JPG", new File("filename.jpg"));
public BufferedImage createImageFromBytes(byte[] imageData) {
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
try {
return ImageIO.read(bais);
} catch (IOException e) {
throw new RuntimeException(e);
}
/*
* PreparedStatement ps = conn.prepareStatement("SELECT img FROM images WHERE imgname = ?");
ps.setString(1, "myimage.gif");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
byte[] imgBytes = rs.getBytes(1); OR byte []out = (byte[])(rs.getObject(1));
// use the data in some way here
}
rs.close();
ps.close();*/