Results 1 to 5 of 5
  1. #1
    dwicki is offline Member
    Join Date
    Jan 2013
    Location
    Bandung - West Java, Indonesia
    Posts
    3
    Rep Power
    0

    Question Convert image (.jpg, .png, .bmp) to Array io bytes and vice versa

    hi, i want to ask how to convert image to array of bytes without extract image format (if possible)
    i want to create encryption application for image.
    this is my plan how to do image encryption:
    1. open file image
    2. extract to array of bytes
    3. processing encryption/decryption
    4. redraw image


    thanks for all your help, i really appreciate this

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Convert image (.jpg, .png, .bmp) to Array io bytes and vice versa

    Please tell us: What have you tried and where are you stuck?

  3. #3
    dwicki is offline Member
    Join Date
    Jan 2013
    Location
    Bandung - West Java, Indonesia
    Posts
    3
    Rep Power
    0

    Default Re: Convert image (.jpg, .png, .bmp) to Array io bytes and vice versa

    Hi sir Fubarable,
    finaly i get working codes,

    this is the code that working fine to me
    Java Code:
    /**
     * @(#)SimpleConvertImage.java
     *
     *
     * @author 
     * @version 1.00 2013/1/15
     */
    
    
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.imageio.ImageIO;
    
     
     
    public class SimpleConvertImage {
    	public static void main(String[] args){
    		try {
     
    			byte[] imageInByte,xoredImage;
    			//BufferedImage originalImage = ImageIO.read(new File("c:/darksouls.jpg"));
     			BufferedImage originalImage = ImageIO.read(new File("d:/wq/java/convert image/grumpycat.jpg"));
    			
    			// convert BufferedImage to byte array
    			ByteArrayOutputStream baos = new ByteArrayOutputStream();
    			ImageIO.write(originalImage, "jpg", baos);
    			baos.flush();
    			imageInByte = baos.toByteArray();
    			baos.close();
    			xoredImage = new byte[imageInByte.length];
    		
    			// modified code
                            for (int i = 0; i< imageInByte.length;i++){
    				xoredImage[i] = imageInByte[i] ^ 0xFF;
    			}
    	                //end of modified code
     
    			// convert byte array back to BufferedImage
    			InputStream in = new ByteArrayInputStream(xoredImage);
    			BufferedImage bImageFromConvert = ImageIO.read(in);
     
    			ImageIO.write(bImageFromConvert, "jpg", new File("d:/wq/java/convert image/new-grumpycat.jpg"));
     
    		} catch (IOException e) {
    			System.out.println(e.getMessage());
    		}
    	}
    	
    
    	
    }
    but after i modified some code and test it,
    the modified code give me following error:

    SimpleConvertImage.java:41: error: possible loss of precision
    xoredImage[i] = imageInByte[i] ^ 0xFF;
    ^
    required: byte
    found: int

    why it found as int not byte ?

    sorry i was a newbie, so simple problem can make me stuck
    i'm using JCreator 4.50.010 on win 7

  4. #4
    dwicki is offline Member
    Join Date
    Jan 2013
    Location
    Bandung - West Java, Indonesia
    Posts
    3
    Rep Power
    0

    Default Re: Convert image (.jpg, .png, .bmp) to Array io bytes and vice versa

    i'm have fixed the modified code,
    the code should be like this
    Java Code:
    for (int i = 0; i< imageInByte.length;i++){
    				xoredImage[i] =(byte)( imageInByte[i] ^ 0xFF);
    				
    			}
    but it result error when i compile it,
    it said java.lang.IllegalArgumentException: image == null!

    does it because bits which describe image format included by modified code(byte xored with 0xFF)?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Convert image (.jpg, .png, .bmp) to Array io bytes and vice versa

    Quote Originally Posted by dwicki View Post
    but it result error when i compile it,
    it said java.lang.IllegalArgumentException: image == null!
    IllegalArgumentException is a RuntimeException, not a compile error. Do you know the difference between compiling and running/executing?

    When seeking help with any error or exception, always post the entire error text / stack trace.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. convert from roman numerals to integers and vice versa
    By number1cynic in forum New To Java
    Replies: 10
    Last Post: 01-18-2010, 11:54 PM
  2. Replies: 5
    Last Post: 04-16-2009, 06:30 PM
  3. converting image to binary code n vice versa
    By nupurashi in forum Advanced Java
    Replies: 4
    Last Post: 04-01-2009, 07:27 PM
  4. Postfix into prefix and vice versa
    By sfe23 in forum New To Java
    Replies: 9
    Last Post: 02-19-2009, 10:37 PM
  5. Replies: 5
    Last Post: 10-04-2008, 11:13 AM

Tags for this Thread

Posting Permissions

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