Solved

Displaying images from a buffer

Posted on 2000-03-28
9
216 Views
Last Modified: 2008-02-01
Hi!
I have an Oracle db with images. I read an image from this db with an applet. I have the image in a buffer. How can I display this image in that applet?

Thanks!
0
Comment
Question by:atti75
  • 5
  • 3
9 Comments
 
LVL 1

Expert Comment

by:dmaguillo
Comment Utility
Hi atti75. I don´t know how u have stored image date, but here u have an example to display an image stored like InputStream:

public class BMPReader extends Object
{
// Constants indicating how the data is stored
    public static final int BI_RGB = 0;
    public static final int BI_RLE8 = 1;
    public static final int BI_RLE4 = 2;

    public static ImageProducer getBMPImage(InputStream stream)
    throws IOException
    {
// The DataInputStream allows you to read in 16 and 32 bit numbers
        DataInputStream in = new DataInputStream(stream);

// Verify that the header starts with 'BM'

        if (in.read() != 'B') {
            throw new IOException("Not a .BMP file");
        }
        if (in.read() != 'M') {
            throw new IOException("Not a .BMP file");
        }

// Get the total file size
        int fileSize = intelInt(in.readInt());

// Skip the 2 16-bit reserved words
        in.readUnsignedShort();
        in.readUnsignedShort();

        int bitmapOffset = intelInt(in.readInt());

        int bitmapInfoSize = intelInt(in.readInt());

        int width = intelInt(in.readInt());
        int height = intelInt(in.readInt());

// Skip the 16-bit bitplane size
        in.readUnsignedShort();

        int bitCount = intelShort(in.readUnsignedShort());

        int compressionType = intelInt(in.readInt());

        int imageSize = intelInt(in.readInt());

// Skip pixels per meter
        in.readInt();
        in.readInt();

        int colorsUsed = intelInt(in.readInt());
        int colorsImportant = intelInt(in.readInt());
        if (colorsUsed == 0) colorsUsed = 1 << bitCount;

        int colorTable[] = new int[colorsUsed];

// Read the bitmap's color table
        for (int i=0; i < colorsUsed; i++) {
            colorTable[i] = (intelInt(in.readInt()) & 0xffffff) + 0xff000000;
        }

// Create space for the pixels
        int pixels[] = new int[width * height];

// Read the pixels from the stream based on the compression type
        if (compressionType == BI_RGB) {
            if (bitCount == 24) {
                readRGB24(width, height, pixels, in);
            } else {
                readRGB(width, height, colorTable, bitCount,
                    pixels, in);
            }
        } else if (compressionType == BI_RLE8) {
            readRLE(width, height, colorTable, bitCount,
                pixels, in, imageSize, 8);
        } else if (compressionType == BI_RLE4) {
            readRLE(width, height, colorTable, bitCount,
                pixels, in, imageSize, 4);
        }

// Create a memory image source from the pixels
        return new MemoryImageSource(width, height, pixels, 0,
            width);
    }

// Reads in pixels in 24-bit format. There is no color table, and the
// pixels are stored in 3-byte pairs. Oddly, all windows bitmaps are
// stored upside-down - the bottom line is stored first.

    protected static void readRGB24(int width, int height, int pixels[],
        DataInputStream in)
    throws IOException
    {

// Start storing at the bottom of the array
        for (int h = height-1; h >= 0; h--) {
         int pos = h * width;
            for (int w = 0; w < width; w++) {

// Read in the red, green, and blue components
          int red = in.read();
          int green = in.read();
          int blue = in.read();

// Turn the red, green, and blue values into an RGB color with
// an alpha value of 255 (fully opaque)
                pixels[pos++] = 0xff000000 + (red << 16) +
               (green << 8) + blue;
            }
        }
    }

Bye... :)
(This example was extracted from: "JAVA
Expert Solutions by Mark Wutka, et. al."
0
 

Author Comment

by:atti75
Comment Utility
I can read the image with a stream...but i have jpg and gif images..
how can I dispay these? I have a buffer with the image file...
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
Comment Utility
What do you mean a buffer? A 2 dimensional array of colors?
0
 

Author Comment

by:atti75
Comment Utility
Adjusted points from 50 to 100
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:atti75
Comment Utility
:) I have the image in a byte array for exemple...with other words I've got the file in memory not in another storage media(HD)



0
 
LVL 7

Expert Comment

by:Sasha_Mapa
Comment Utility
You need to use the MemoryImageSource class to create an image and then you can do with the Image whatever is possible in Java.
This is achieved like this:

import java.awt.image.*;
import java.awt.*;
import java.applet.*;

public class MyApplet extends Applet{

public Image createImage(int [] pixels,int width, int height){
   return this.createImage(new MemoryImageSource(width,height,pixels,0,width))
}


}
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
Comment Utility
You can then display it in the usual manner with:


public void paint(Graphics g){
  MyImage image = createImage(myBufferImage,width,height);
  g.drawImage(image,0,0,this);
}
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
Comment Utility
Doesn't that answer your question?
0
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 100 total points
Comment Utility
Ok, it seems like my comment answers this question. If anyone disagrees or thinks his response was better, please post a comment and I'll withdraw the answer.

Answer:

You need to use the MemoryImageSource class to create an image and then you can do with the Image whatever is possible in Java. This is achieved like this:

import java.awt.image.*;
import java.awt.*;
import java.applet.*;

public class MyApplet extends Applet{

  public Image createImage(int [] pixels,int width, int height){
      return this.createImage(new MemoryImageSource(width,height,pixels,0,width))
  }


}

You can then display it in the usual manner with:

public void paint(Graphics g){
    Image image = createImage(myBufferImage,width,height);
    g.drawImage(image,0,0,this);
}
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoresSpecial  challenge 13 40
Receive file in Servlet 1 36
mapAB Challlenge 35 84
Groovy:unable to resolve class error 2 28
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now