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;
}

Similar Messages

  • 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

  • Converting a PNG image to an array of bytes and vice versa..

    hi all,
    i need to convert a PNG image to an array of bytes ,then converting back this array of bytes to The PNG image again ,i read this can be done using output streams but i feel like a dump and i can't fix the whole thingy out ! ,can anybody help me in this ,by explaining how can this be established????
    Regards,
    D.Roth

    hi all,
    i need to convert a PNG image to an array of bytes ,then converting back this array of bytes to The PNG image again ,i read this can be done using output streams but i feel like a dump and i can't fix the whole thingy out ! ,can anybody help me in this ,by explaining how can this be established????
    Regards,
    D.Roth

  • 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?

  • How can I convert an array to image(8bit)

    I can convert an array(acquire from usb camera)to a picture,but not images(8 bit grayscale),how can I do it.many thanks!
    Attachments:
    array to image.vi ‏249 KB

    Sorry, this is the kind of thing that happens when you have been out for too long.
    Message Edité par chilly charly le 10-22-2006 11:09 AM
    Chilly Charly    (aka CC)
             E-List Master - Kudos glutton - Press the yellow button on the left...        
    Attachments:
    array%20to%20image[1].png ‏3 KB

  • I want to convert a String into an array of bytes

    I am working with telnet programming with java, where I want to convert login name and password of an online user in 'String' format to 'Bytes', plz help me. Which Input or Output Stream I can use

    Assuming you've got the username / password into a String object, you can convert it to a byte[] array using this method.
    String.getBytes();
    Look at the String API
    http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html
    Hope that helps.

  • How to Convert array of int into array of byte - please help

    I have a 2-dim array of type int and I want to convert it to an array
    of byte. How can I do that? Also, if my 2-dim int array is one dimention
    can I use typecast as in the example below?
    private byte[] getData()
    byte []buff;
    int []data = new int[5];
    //populate data[]
    buff = (byte[]) data; <---- CAN I DO THIS??????
    return buff;

    hi, I suggest you make an array of byte arrays
    --> Byte[][] and use one row for each number
    you can do 2 things,
    take each cipher of one number and put one by one in each column of the row correspondent to that number. of course it may take too much room if the int[] is too big, but that is the easiest way I think
    the other way is dividing your number into bitsets(class BitSet) with sizes of 8 bits and then you can save each bit into each column of your array. and you still have one number in each row. To put your numbers back use the same class.
    Maybe someone has an easier way, I couldnt think of any.

  • How to convert an array of bits into an array of bytes?

    If I have an array of 1s and 0s, how can I pack them
    into an array of bytes (8 bits)?
    Thanks.

    NI recommends to get rid of 4.x mode (Quote from the help file: "National Instruments recommends reworking any application that uses the Convert 4.x Data mode as a long term solution."). For this reason, I have attached an alternative example to give you some ideas.
    (The previous example (using 4.x type cast) is actually slightly flawed, because for some reason it only produces even sized output arrays (why???) so you would need to trim the resulting array as a function of the input array size.)
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    OnesAndZeroesU8v2.vi ‏43 KB

  • Want to convert an array to image in gray scale

    i have an image that i have read into the vi. using the draw unflattened pixmap , i have an array of numbers .after some mathematical computations on the array i should be able to convert the array back to the image. how can i convert from array to image.and then from image to gray scale?

    I am not sure what you are doing. Can you post a simple example program?

  • Array of bytes..

    hi..
    how can i convert array of bytes to string and vise versa
    thanks

    You can convert the string to a char array with String.toCharArray(), but if you want them as a byte array you have to first decide what character encoding should be used with the conversion. You can e.g. use "8859_1" if you only work with characters in the range 0 to 255. Then it would be String.getBytes("8859_1"). You have to add a try-catch block. It will throw an exception if you use an unsupported encoding. ISO 8859-1 is one of the basic supported encodings, so you will not get any exceptions when you use this.
    To go the other way, you use one of the String constructors.

  • Array of bytes containg punctuatio​n

    Hi, I have an array of bytes that I would like to export using the spreadsheet VI. The spreadsheet creates a file with the extension .res, which logs the array as hex values. The problem is, all my data is separated with a punctuation mark, which I guess LabView inserts into the array to separate the numbers.
    So my question is, is there any way to convert an array of U8's into a string of pure hex or just a stream of binary data?
    Thanks

    q-bertsuit wrote:
    Thank you for your answers, I'll try your link. The first suggested solution didn't work.
    You are not giving enough information. Did the second suggestion work?
    q-bertsuit wrote:
    I want to use the spreadsheet VI to make a .res file, saving as a plain text file would cause alot of extra work in MatLab.
    A "spreadsheet" in LabVIEW is nothing more than a plain text file with certain delimiters for columns and newlines delimiting rows. "Write to spreadsheeet file" is a simple tool for typical use. All the formatting can be done explicit and then written to a plain text file, the outcome is exactly the same. There is no extra work involved in Matlab.
    q-bertsuit wrote:
    I might not have explained the problem sufficiently in the first post. If my buffer containing U8s is passed in to the spreadsheet VI, the .res file saves for example the number 255 as ASCII character 2 then character 5 then 5. So 255 would be saved as 32 35 35 instead of FE.
    Again, write to spreadsheet file is not the right tool then, so don't use it.
    It is still not clear. So you have an array of U8. How should the file look like? Should there be any delimiters?
    Example: If you only write 255 to the file, should it contain
    one binary, nonpritable character (xFF)
    two charcters (two instances of the letter F)
    Three characters (letter 2 followed by two letters 5)
    If the array has two elements, how should the second character be stored? Should there be a delimiter (space, comma, tab, etc) or nothing separating it from the first value?
    Once we have the specifications, the rest will be trivial.
    LabVIEW Champion . Do more with less code and in less time .

  • Problem converting U8 array to date-time string

    Hi All,
    How can I convert U8 array (time_t data type from C dll) to date time string?
    A dll function that I am calling has a structure of string, integer and time_t as one of the parameters. Instead of passing cluster, I pass an array of U8 of the size that the structure should be. All the members of the strcuture and parsed correctly except the date/time.
    Function Parameter:
    typedef struct {
    Int AlarmState ;
    Int AlarmGrade ;
    TCHAR AlarmMessage [100] ;
    time_t AlarmTimeStamp ;
    } WV_ALARM_INFO ;
    Total size = 4 + 4 + 100 + 4 = 112
    Using Call Library Function, I set the parameter type to Array of U8 and size 112.
    After the array is populated when the function is called, I have done the following to interpret date and time.
    1) Using Extract Zero Terminated String.VI I converted U8 array (of size 4) to string.
    2) Type casted string to integer (I32).
    3) Swap bytes
    4) Swap Words
    5) Format Date/Time String.
    Please see the attached screenshot for visual display of the above.
    Ideas on why the year is incorrect?
    Mimansa
    Attachments:
    Time.JPG ‏102 KB

    Hi Mimansa,
    Is it only the year that is incorrect? Also, where did you find that ABC\0 VI that you use 3 times? Did you make it or is it in a labview library somewhere?
    Thanks,
    Laura

  • How to convert 1D array of string to string

    How to convert 1D array of string to string.

    Maximus00, as Pavel indicated, there is a lesser known feature of "Concatenate Strings" that does exactly what your code is doing if the input is an array of strings. In one step! See attached image.
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    ConcatenateArray ofStrings.gif ‏7 KB

  • Reading an array of byte with special delimiter....help me!!

    Hi guys,
    i've developed a java application that has in input a txt file with a standard format
    string string string
    string double double
    string double double
    and stores it into an array of byte with a fixed format,
    that is byte(whitespace)byte(whitespace)byte(;)byte(whitespace)byte(whitespace)byte(;)byte(whitespace)byte(whitespace)byte(;)
    that mean introducing byte whitespace value and byte ; value to separate different columns and different rows.
    Now i have to develop inverse routine,that has to read the array of byte and rebuild its original format.
    Can you help me with some idea ord code?
    I'm inexepert...please help me with clear suggest...
    I post you my routine that converts txt file into an array of byte
    P.S.In my txt file number of columns and rows change for each file,what is standard is a firtst headline that is an array of string and others row with the same format string array of doubles....
    public byte[] getBytes(){
              byte middlerow=' ';
              byte endrow=';';
              Vector temp=new Vector(10000000);
              int i=0;     
              String g=null;
              Riga r;
              Double val[];
              while(i<intest.length){
                   //copio tutto nell'array di byte
                   byte []bytes = intest.getBytes();
    //               memorizza in byte un elemento del vettore alla volta
                   for( Byte aByte : bytes ) {
                   temp.addElement( aByte );
                   temp.addElement( Byte.valueOf( middlerow ) );
                   i++;
              temp.addElement(Byte.valueOf(endrow));
              System.out.println("Intestazione convertita in byte");
              for(int l=0;l<rows.size()-1;l++){
                   r=(Riga)rows.get(l);
                   g=r.getgeneid();
              temp.addElement(g.getBytes());
              temp.addElement(Byte.valueOf(middlerow));
              val=r.getvalues();
              for(int e=0;e<=val.length-1;e++){
              temp.addElement(Byte.valueOf(val[e].byteValue()));
              //val[e].byteValue() fa il casting double in byte
              temp.addElement(Byte.valueOf(middlerow));
         temp.addElement(Byte.valueOf(endrow));
              byte [] b=new byte[temp.size()];
              for (int k=0;i<temp.size();i++){
                   b[k]=(((Byte)temp.elementAt(k)).byteValue());
              return b;
    Thanks for your help...

    Ahh, how is the "array of bytes" being populated, Java code?
    If so which encoding are you using? When you create the bytes (from a string at a guess) the other end which encoding do you use?
    If you are using a db, why not use a db? Why this blob of data?
    If I really was banned from using the DB as a DB, and HAD (on pain of death) to use this wonky blob thing, I would do:
    Writing:
    ByteArrayOutputStream daos = new ByteArrayOutputStream() ;
    DataOutputStream dos = new DataOutputStream( daos );
    dos.writeUTF( "me String" );
    dos.writeDouble( 4.2 );
    dos.close;
    byte[] stickInUglyBlob = daos.toByteArray() ;
    Reading:
    DataInputStream dis = new DataInputStream( new ByteInputStream( blobOfUckyness ) );
    String meString = dis.readUTF();
    double meDouble = dis.readDouble();

Maybe you are looking for

  • Error "A web exception has occurred during file upload" when trying to import ESXi image file into Update Manager

    I'm encountering this error and not sure how to fix, I'm quite new to vCenter so please bear with me. I'm trying out vCenter 5.1 with Update Manager 5.1 right now.  No license key has been entered and I still have 50 odd days to try it out. 2 ESXi ho

  • Pass a variable from one frame to another

    I have 2 JFrame, one just has a button which is select file, and it will get the path of the selected file, and i have another JFrame which will get that path and open up another JFrame and load up data using the filepath. How would i pass the path f

  • Can you help me with this code

    hi am new to java so please bare with me , so in this code as you can see i comment some lines of code the one i comment works when i compile it , but why the one with fields does not ? can you please explain this to me public class Demo3            

  • Unplanned Services

    Hi, Where can i maintain the Value of Unplanned services in Service PO,so that i can enter Unplanned services during service entry sheet. Regards

  • Update OSX 10.2.8 Build 6R73 repeatedly fails to install.

    Hi, I have an elderly but beloved iBook, 900Mhz Power PC G3 with 640 Mb. I have just replaced the failed hard drive and I have used the disks that came with the iBook to install OSX 10.2.1. I have successfully installed some updates e.g. Quicktime 6.