×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    13

    image -> byte array -> image

    How it is possible to convert image to byte array and then back to image? I tried this but it will throw IllegalArgumentException

    Code:
    Image exampleImage = Image.createImage( "/test.jpg" );	
    	
    int[] rgb = new int[exampleImage.getWidth() * exampleImage.getHeight()];
    exampleImage.getRGB(rgb, 0, exampleImage.getWidth(), 0, 0, exampleImage.getWidth(), exampleImage.getHeight());
    int rgbLength = rgb.length;
    
    for (int i = 0; i < rgbLength; i++) 
    {
        dataOutputStream.writeInt(rgb[i]);
    }
    
    byte[] rgbByteArray = byteArrayOutputStream.toByteArray();
    	
    // this will fail
    Image second = Image.createImage( rgbByteArray, 0, rgbByteArray.length );

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    13

    Re: image -> byte array -> image

    Ok, I found something that works.

    Code:
    public Image imageToByteToImage( Image img, int width, int height )
    {
    	if( img == null || width < 0 || height < 0 )
    	{
    		throw new IllegalArgumentException( "Check arguments" );
    	}
    
    	int[] imgRgbData = new int[width * height];
    
    	try
    	{
    		img.getRGB( imgRgbData, 0, width, 0, 0, width, height );
    	} catch( Exception e )
    	{
    	}
    	try
    	{
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    		DataOutputStream dos = new DataOutputStream( baos );
    
    		for( int i = 0; i < imgRgbData.length; i++ )
    		{
    			dos.writeInt( imgRgbData[i] );
    		}
    
    		byte[] imageData = baos.toByteArray();
    		
    		ByteArrayInputStream bin = new ByteArrayInputStream( imageData );
    		DataInputStream din = new DataInputStream( bin );
    
    		int remaining = ( imageData.length ) / 4;
    
    		int[] rawdata = new int[remaining];
    
    		for( int k = 0; k < rawdata.length; k++ )
    		{
    			rawdata[k] = din.readInt();
    		}
    
    		baos.close();
    		dos.close();
    		bin.close();
    		din.close();
    		
    		return Image.createRGBImage( rawdata, width, height, false );
    	} catch( Exception e )
    	{
    		e.printStackTrace();
    	}
    	return null;
    }
    Last edited by kolada; 2007-05-29 at 08:01.

  3. #3
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280

    Re: image -> byte array -> image

    All this:
    Code:
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    		DataOutputStream dos = new DataOutputStream( baos );
    
    		for( int i = 0; i < imgRgbData.length; i++ )
    		{
    			dos.writeInt( imgRgbData[i] );
    		}
    
    		byte[] imageData = baos.toByteArray();
    		
    		ByteArrayInputStream bin = new ByteArrayInputStream( imageData );
    		DataInputStream din = new DataInputStream( bin );
    
    		int remaining = ( imageData.length ) / 4;
    
    		int[] rawdata = new int[remaining];
    
    		for( int k = 0; k < rawdata.length; k++ )
    		{
    			rawdata[k] = din.readInt();
    		}
    
    		baos.close();
    		dos.close();
    		bin.close();
    		din.close();
    doesn't do anything except copy the array "imgRgbData" into the array "rawdata" in a very convoluted way.

    You could have just replaced the Image.createImage() call in the first example you posted with Image.createRGBImage().

    shmoove

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    40

    Re: image -> byte array -> image

    no post here
    Last edited by huriniin; 2009-05-29 at 18:33. Reason: canceled
    Going to Reach UnreachableThing

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    1

    byte array -> image

    Method to write byte[] to image its working

    private static void writeImage(byte [] b, String filename) throws Exception{
    //InputStream sImage=;
    FileOutputStream outStream = new FileOutputStream("/moto.jpg");
    outStream.flush();
    outStream.write(b);
    outStream.close();
    }

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    Re: image -> byte array -> image

    Please help me....
    I want to save image from RGB data


    this my script for get byte array from image :

    int[] MyRGB=new int[w*h];
    byte[] byteMyRGB=new byte[w*h*4];
    int lnw=0;

    image.getRGB(MyRGB, 0, w, 0, 0, w, h);

    for(int t=0; t<MyRGB.length; t++)
    {
    int nwa = ((MyRGB[t] & 0xff000000) >> 24); // alpha channel!
    int nwm = ((MyRGB[t] & 0xff0000) >> 16); // red channel!
    int nwh = ((MyRGB[t] & 0x0000ff00) >> 8); // green channel!
    int nwb = (MyRGB[t] & 0x000000ff); // blue channel !

    byteMyRGB[lnw] = (byte)nwb;
    byteMyRGB[lnw+1] = (byte)nwh;
    byteMyRGB[lnw+2] = (byte)nwm;
    byteMyRGB[lnw+3] = (byte)nwa;

    MyRGB[t] = (0xff000000|nwa<<24|nwm<<16|nwh<<8|nwb);

    lnw += 4;
    }


    this my script for saving image :

    try
    {
    FileConnection fc = (FileConnection)Connector.open("file://localhost/" + currDirName + imgName, Connector.READ_WRITE);
    if (fc.exists())
    {
    System.out.println("image doesn't exist");
    }
    fc.create();
    OutputStream os=fc.openOutputStream();
    os.write(byteMyRGB, 0, byteMyRGB.length);
    os.flush();
    os.close();
    fc.close();
    }
    catch(IOException ex)
    {
    ex.printStackTrace();
    }

    but if i save byteMyRGB[] to filesystem
    the image specification in Windows explorer is :

    image source : Size = 4KB
    Type = JPEG
    Dimension = 97 x 123



    MyImage : Size = 47KB
    Type = JPEG
    not have dimension and can't preview

  7. #7
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: image -> byte array -> image

    Welcome to Forum Nokia!

    That's because you're not writing data in JPEG format.

    Check the wiki for how to encode an image as PNG.

    Graham.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    20

    Re: image -> byte array -> image

    Hi.

    I wrote a post about reading an image an convert it to bytes so you can save it on RMS or encode it in Base64... whatever you want.

    http://www.java-n-me.com/2011/02/con...y-byte-in.html


    Hope this help.

Similar Threads

  1. DOJA appli -> Create Image from Byte Array
    By teamore in forum Mobile Java General
    Replies: 2
    Last Post: 2004-10-29, 11:14
  2. Has Nokia broken 7650 J2ME in v4.39?
    By LongSteve in forum Mobile Java General
    Replies: 6
    Last Post: 2003-06-03, 17:05
  3. Replies: 5
    Last Post: 2003-02-07, 11:25
  4. Loading images from byte array on 6130i
    By enlightment in forum Mobile Java General
    Replies: 2
    Last Post: 2002-06-05, 19:44
  5. 7650 and MS Bluetooth Transceiver
    By damnedi in forum Bluetooth Technology
    Replies: 1
    Last Post: 1970-01-01, 02:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×