Converting an array of RGB bytes into an Image

Hello,
I have written a scanner intreface (SANE) in JNI, and it gives me an array of RGB bytes as output. There is no transparency (obviously). I'm sure there is a way to use MemoryImageSource to turn this raw array into a plain old Image, but I'm not finding how to do it. I've tried:
     ColorSpace rgbColorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
     ColorModel ccm = new ComponentColorModel(rgbColorSpace, new int[] { 8, 8, 8 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
     MemoryImageSource mis = new MemoryImageSource(width, height, ccm, imageBytes, 0, 0);
     Toolkit kit = Toolkit.getDefaultToolkit();
     Image image = kit.createImage(mis);but that doesn't work. Any ideas on this? Surely it is possible, but it seems like Java always wants an alpha channel, something which I don't have.
Thanks

In other words, if I dump the bytes to a file, I can get an image like this:
rawtoppm -rgb 424 585 foo.raw > foo.ppm
where the image is 424x585, and the image comes up nicely. Surely there is some way to do what rawtoppm is doing in Java?

Similar Messages

  • How can I convert an array with varying length into a cluster

    Hi,
    I need to convert an array of n elements into a cluster. Usually, it only involves the "Array to Cluster" function. But since the array length is not constant and the number of elements in the function is constant and can not be changed programaticaly, I need to find another way to do that.
    Any ideas?
    Thanks.

    Hi,
    I also tried to do this but without success. I think that's not possible.
    For me it's a general problem using labview. I often wanted to generate standard controls dynamically which is not possible (or I haven't found the solution yet). So I think that's the reason why you can't create a cluster during runtime (the program would have to add controls dynamically to the cluster).
    Do you have an upper limit for your array-size? Although it needs a lot more memory, it would be a possibility to create the cluster with the maximum number of elements.
    If you don't have a maximum, you will have to look for another way, I'm afraid.
    Thomas
    Using LV8.0
    Don't be afraid to rate a good answer...

  • How to save Byte Array of raw data into JPEG image.

    Hello!
    I have a image and I stored its data as byte array as
    bimage = bitmap1.getRawData();
    now I have Byte[] bimage, I want to save it as .jpeg image.
    and show that image..............

    the short way is this:
    ImageIO.write(bimage, "jpeg", new File("image.jpg"));
    Where you use the original Image object... but it has to be a java.awt.image.RenderedImage (which a java.awt.image.BufferedImage is). So this method would come in handy.
         public static BufferedImage getBufferedImage(Image img) {
              // if the image is already a BufferedImage, cast and return it
              if((img instanceof BufferedImage) && background == null) {
                   return (BufferedImage)img;
              // otherwise, create a new BufferedImage and draw the original
              // image on it
              int w = img.getWidth(null);
              int h = img.getHeight(null);
              BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
              Graphics2D g2d = bi.createGraphics();
              g2d.drawImage(img, 0, 0, w, h, null);
              g2d.dispose();
              return bi;
         }If the byte array you have is raw image data, then you can look at the javax.imageio package and see what you can do with those classes.

  • How to turn a byte[] into an image?

    My app downloads image data from a server, storing the data in a byte[] called imageDataByteArray.
    I want to use imageDataByteArray to create an image. For this, I have tried using:
    int w = imageWidth; //488
    int h = imageHeight; //245
    int imageOffset = 0;
    int scan = w;
    Image image = component.createImage(new MemoryImageSource(w, h, java.awt.image.ColorModel.getRGBdefault(), imageDataByteArray, imageOffset, scan));
    ...but the image created is empty.
    I think the length of imageDataByteArray is a problem - it's length is only 21461, whereas the number of pixels in the image ( = imageWidth * imageHeight) is 119560.
    I've played around for a while, but cannot solve. Can anyone help?

    Hiya Rodney,
    Yes, it's in jpg format. I've tried your code but the image does not display correctly.
    To shed some more light, here's how I'm getting the image:
    I am using standard http code to connect to the image url (http://www...../image.jpg) which returns the image data as a String, called httpContent. I then convert this String to imageDataByteArray with httpContent.getBytes[] - but am obviously struggling to recreate the image.
    Any ideas where I might be going wrong?
    Thanks,
    James

  • How can I convert an array off byte into an Object ?

    Hi folks...
    I�m developing an application that comunicates a PDA and a computer via Wi-Fi. I�m using a DataStream ( Input and Output ) to receive / send information from / to the computer. Most off the data received from him is in the byte[] type...
    How can I convert an array off byte ( byte[] ) into an Object using MIDP 2.0 / CLDC 1.1 ?
    I found on the web 2 functions that made this... but it uses a ObjectOutputStream and ObjectInputStream classes that is not provided by the J2ME plataform...
    How can I do this ?
    Waiting answers
    Rodrigo Kerkhoff

    There are no ObjectOutputStream and ObjectInputStream classes in CLDC. You must know what you are writing to and reading from the DataStream. You should write the primitives like int, String to the DataOutputstream at one end and read those in exactly the same sequence at the outher end using readInt(), readUTF() methods.

  • How do I convert an array of BYTES (where each BYTE represents a bit) into a single Hex number?

    I am reading a signal from a USB-8451. This signal is stored as an array where each element represents a bit in the signal, but is stored in the array as a byte. How do I convert this array into a single Hex number. I attatched what I have so far, there are a few extra things to help me see what ia going on. One code uses Queue and the other uses arrays, let me know if you can help.
    Attachments:
    845x_EEPROMarrays.vi ‏27 KB
    845x_EEPROM.vi ‏26 KB

    mkssnwbrd wrote:
    ... so we can't introduce any other forms of signals or power into the circuit other than what the circuit already has. Trithfully I don't really know how I2C devices work, but my mentor here says that we can't use an I2C method becuase it will introduce voltage into the circuit and may damage out TCON chip.
    That makes absolutely no sense. What do you think is happening when you write the digital lines? You're setting a pin high. That voltage is being generated by the 8451x. I think you're not understanding what your mentor is saying. If it's an I2C device then you should be able to use the I2C function to simply talk to it. You still have not indicated what the device is, so there's little more I can say about that aspect of it.
    As far as the conversion is concerned, you basically need loop through your array of "bits", taking 16 at time since you said you have 16-bit values. It's not clear from your code whether your eventual goal is to get a numeric value or a string. This does not appear to be a subVI, so a simply numeric indicator formatted to display in hex format should be quite adequate. The array you are generating is an array of rings, whose datatype is I32, but they will have values of 0 or 1. You can use the example just posted, or you can use the attached variation.
    Attachments:
    Bits to Hex 2.vi ‏17 KB

  • Is there an easier way to convert bytes into bit(boolean) arrays?

    I am currently using this method to convert bytes into bit arrays:
    /*convert byte to int such that it is between 0-255 this is the bytes[] array
                        if ((bytes/128)==1)
                             bit[c+0]=true;
                        else
                             bit[c+0]=false;
                        if ((bytes[i]-bitInt[c+0]*128)/64==1)
                             bit[c+1]=true;
                        else
                             bit[c+1]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64)/32==1)
                             bit[c+2]=true;
                        else
                             bit[c+2]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32)/16==1)
                             bit[c+3]=true;
                        else
                             bit[c+3]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16)/8==1)
                             bit[c+4]=true;
                        else
                             bit[c+4]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16-bitInt[c+4]*8)/4==1)
                             bit[c+5]=true;
                        else
                             bit[c+5]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16-bitInt[c+4]*8-bitInt[c+5]*4)/2==1)
                             bit[c+6]=true;
                        else
                             bit[c+6]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16-bitInt[c+4]*8-bitInt[c+5]*4-bitInt[c+6]*2)==1)
                             bit[c+7]=true;
                        else
                             bit[c+7]= false;

    You can loop through and use a bitwise operator instead. Here is an example without the loop.
    byte b = 6 ;
    if( b & Math.pow( 2, 0 ) == Math.pow( 2, 0 ) ) ;
    // the 2^0 bit is on
    if( b & Math.pow( 2, 1 ) == Math.pow( 2, 1 ) ) ;
    // the 2^1 bit is onetc...
    You should get something like 110 when you're done.
    If you're wonder what & does (no, its not a boolean &&), it takes the bits in the two numbers you give it and returns a number with all the bits on that are on for each of them.
    For example:
    10011011 &
    11001101 =
    10001001
    So if we take
    110 (6) &
    010 (2^1, or 2) =
    010 (2 again)
    Which means that the number (6) has the 2^1 bit on.

  • How to convert bytes[] into File object

    hi
    how to convert byte array into File object
    pls.. help me
    Regards
    srinu

    rrrr007 wrote:
    Hi,
    How to convert bytes[] into multipage File object?? ]There's no such thing as a "multipage File object." You ought to re-read this thread closely, and read the [API docs for File|http://java.sun.com/javase/6/docs/api/java/io/File.html] to clear up your confusion about what a File object is.
    I used the java.io.SequenceInputStream to concatenate two input streams (basically .pdf files) into a single input stream. I need to create a single multipage pdf file using this input stream. Then you need a pdf API, like iText or fop. You can't just concatenate pdf files, word docs, excel sheets, etc., like you can text files. Google for java pdf api.

  • Byte into bit array?

    I want to implement a little compression programm, and I have to convert the byte array, in which I read a file into a bit array, because I have to be able to change single bits in that array.
    1.Is there a wrapper class?
    2.How many bits does the class Byte contain?
    If it is 8, then i can convert a single byte into 8 bit by using a simple algorithm, right?
    The byte simply displays it�s value (0-255)in hex, right?
    thanks for your reply!
    tim

    There is a class named BitSet that seems to be close to what you are looking for (at least in SDK 1.3). But if you can manipulate bytes, it might be more efficient to write your own code.

  • How to convert digital array data into analog signal

    i want help to convert digital data array into analog signal

    shubham62 wrote:
    We are implimenting real time audio trans-receiver. We have converted input audio data(analog form) into digital data. At the receiver side we received digital data which is in the form of digital data array.But we are unable to recover back our original input data. So please help us to sort out the problem.
    Still no useful information.  How was the data converted to digital in the first place?  What conversion parameters did you use?  As was said, you just need to do the math backwards to get it back into the analog.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • Convert an array of strings into a single string

    Hi
    I am having trouble trying to figure out how to convert an array of strings into a single string.
    I am taking serial data via serial read in a loop to improve data transfer.  This means I am taking the data in chunks and these chunks are being dumped into an array.  However I want to combine all elements in the array into a single string (should be easy but I can't seem to make it work).
    In addition to this I would also like to then split the string by the comma separator so if any advice could be given on this it would be much appreciated.
    Many Thanks
    Ashley.

    Well, you don't even need to create the intermediary string array, right? This does exactly the same as CCs attachment:
    Back to your serial code:
    Why don't you built the array at the loop boundary? Same result.
    You could even built the string directly as shown here.
    Message Edited by altenbach on 12-20-2005 09:39 AM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    autoindexing.png ‏5 KB
    concatenate.png ‏5 KB
    StringToU32Array.png ‏3 KB

  • How to convert an array of numbers into a time signal?

    I have an array of 512 data points. I want to convert this array into a time domain signal (the one with the brown color). How would I do this? Please let me know soon...Thanks!

    If you are referring to waveform data type, use the "build waveform" function as illustrated in the attached vi. Also check LabVIEW help waveform example for more sample vi.
    Attachments:
    sample_vi.vi ‏30 KB

  • Converting an array of bytes to an image

    I think this is the right forum for my question.
    This code comes from the J2ME side of my application. It reads in an int from my server, and if its over a certain length, I can tell its an image so i can decode it from an array of bytes back into an image, but it throws an IllegalArgument Exception on image = Image.createImage(b,0,length);
    I've decoded the byte array before it gets sent and convereted it back into an image and that works, and ive run the debugger and the array is full of numbers on the J2ME side of the app, but the call to createImage, just keeps throwing this exception and i can't find an answer. If you need anymore code, I'll post it.
    int l = in.readInt();
    imageArray = new ImageItem[l];
    for(int i = 0; i < imageArray.length; i++){
    byte[] b = null;
    int length = in.readInt();
    System.out.println("length = " + length);
    if(length < 10) {
    imageArray[i] = null;
    System.out.println("null image");
    } else {
    b = new byte[length];
    in.readFully(b,0,length);
    System.out.println("image not null");
    Image image = Image.createImage(b,0,length);
    System.out.println("hit");
    ImageItem imageItem = new ImageItem("null", image, 0, "null");
    imageArray[i] = imageItem;
    If anyone can tell me how to indent the code, i would appreciate it, it looks indented when i post it my side.
    Message was edited by:
    trisonetwo
    Message was edited by:
    trisonetwo

    If it works before sending then check your code for sending the image.
    Also you can compare the byte array before sending with the array which was received at the other end.
    For indent and syntex highliting use [ c o d e ] and [ / c o d e ] tags (without spaces)
    Ex:-
    int l = in.readInt();
    imageArray = new ImageItem[l];
    for(int i = 0; i < imageArray.length; i++){
       byte[] b = null;
       int length = in.readInt();
       System.out.println("length = " + length);
       if(length < 10) {
          imageArray = null;
          System.out.println("null image");
       } else {
          b = new byte[length];
          in.readFully(b,0,length);
          System.out.println("image not null");
          Image image = Image.createImage(b,0,length);
          System.out.println("hit");
          ImageItem imageItem = new ImageItem("null", image, 0, "null");
         imageArray = imageItem;
    }

  • How to convert byte into string

    can any tell me how to convert byte into string
    when im an debugging thid code in eclipse it shows the result in integer format instead of string but in command prompt it is showing result in string format..........plz help
    package str;
    import java.io.*;
    public class Testfile {
    public static void main(String rags[])
    byte b[]=new byte[100];
    try{
    FileInputStream file=new FileInputStream("abc.txt");
    file.read(b,0,50);
    catch(Exception e)
         System.out.println("Exception is:"+e);
    System.out.println(b);
    String str=new String(b);
    System.out.println(str);
    }

    Namrata.Kakkar wrote:
    errors: count cannot be resolved and Unhandled exception type Unsupported Encoding Exception.
    If i write an integer value instead of "count" then Unhandled exception type Unsupported Encoding Exception error is left.This is elementary. You need to go back to [http://java.sun.com/docs/books/tutorial/|http://java.sun.com/docs/books/tutorial/] .

  • How to convert bytes[] into multipage File object

    Hi,
    How to convert bytes[] into multipage File object??
    I used the java.io.SequenceInputStream to concatenate two input streams (basically .pdf files) into a single input stream. I need to create a single multipage pdf file using this input stream.
    Thanks for you help in advance..

    Only text format allows you to concatenate two files together to get a longer files.
    Most formats have a header and a footer and so you cannot simply add one to the other.
    You need to use a PDF API which will allow you to build the new document (if one exists)

Maybe you are looking for

  • Ipod Touch 5.1.1 App Store Problem

    Can't download any apps from the app store from my ipod touch 5.1.1 8gb I bought yesterday and it works normally I could download 5 free apps very well but in the night I try download a new app from app store when I selected and touch the button to i

  • Redeploying new version component with JSPM

    Hi Friends, I am trying to upgrade the older version .SCA component by Using JSPM. But it is showing "No applicable Software Component(S) found in development in inbox directory \usr\sap\... but all.sca file are in the same same location I have tried

  • Software updates causes black screen on macbook

    The laptop is an Apple Unibody MacBook Intel Core 2 Duo, processor - 2.4 GHz Intel Core 2 Duo. Memory - 2 GB 1067 MHz DDR3. OSX 10.9.1 Maverick. The Macbook prompts software updates in which the macbook is supposed to restart after the software insta

  • Security for General Object Services

    Trying to restrict authorizations for GOS at the document type level. We are currently in 4.6c.  We are using the authorization object S_WFAR_OBJ to restrict document type, object type, and the activity level.  This authorization object works as expe

  • Please help!!!! URGENT !!!! using environment variable

    How do I use the environment variable in my sql script. example: I am setting up environment variables through my .bat file. The batch file contain the following lines... SET ORACLE_SID=test SET SID_PWD=test SQLPLUS /NOLOG @C:\TEST.SQL In my TEST.SQL