|
Oct 30
|
Create a ByteArrayInputStream from your byte array and then use ImageIO class to read image from that stream.
InputStream in = new ByteArrayInputStream(bytearray); BufferedImage image = ImageIO.read(in);
|
|
Create a ByteArrayInputStream from your byte array and then use ImageIO class to read image from that stream. InputStream in = new ByteArrayInputStream(bytearray); BufferedImage image = ImageIO.read(in);
The TexturePaint class can be used to achieve that.
public void paintComponent(Graphics g)
{
if(paint == null)
{
try
{
// Create TexturePaint instance the first time
int height = image.getHeight(this);
int width = image.getWidth(this);
bi = (BufferedImage) createImage(width, height);
Graphics2D biG2d = (Graphics2D) bi.getGraphics();
biG2d.drawImage(image, 0, 0, Color.black, this);
paint = new TexturePaint(bi,
new Rectangle(0,0,width,height));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
// Now actually do the painting
Graphics2D g2d = (Graphics2D) g;
if(paint != null)
{
g2d.setPaint(paint);
g2d.fill(g2d.getClip());
}
}
The ImageIO class can be used to write an image as a PNG encoded stream.If you write that stream to a ByteArrayOutputStream then you will end up with a byte array that contains the PNG encoded image. ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(image, "PNG", out); byte[] imageBytes = out.toByteArray(); |
|
Recent Comments