update database #4

This commit is contained in:
JunkJumper
2020-05-04 13:41:10 +02:00
parent 80a0c89d49
commit e0b965af5f
10 changed files with 111 additions and 67 deletions

View File

@ -1,20 +1,62 @@
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 BufferedImage getImg(byte[] imageData) {
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
public static Image getJavaFXImage(byte[] rawPixels) {
//public Image getJavaFXImage(byte[] rawPixels, int width, int height) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
return ImageIO.read(bais);
} catch (IOException e) {
throw new RuntimeException(e);
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

@ -1,6 +1,13 @@
package database;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javafx.scene.image.Image;
/*
import java.sql.Connection;
@ -11,21 +18,25 @@ import java.sql.Statement;
*/
public class DatabaseTesting {
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
Table a = new Table("a");
a.remplirTableAllFrom("CartesAll");
System.out.println(a.toString());
BufferedImage jpg = new BufferedImage(467, 652, 1);
Image image = ByteaToCardImage.getJavaFXImage(a.getList().get(1).getImg());
System.out.println(image);
/*
try {
ByteaToCardImage.getImg(a.getList().get(5).getImg());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
//a.getList().get(1).getImg();
}
public BufferedImage createImageFromBytes(byte[] imageData) {
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
try {
return ImageIO.read(bais);
} catch (IOException e) {
throw new RuntimeException(e);
}