-1

we are doing project on image compresion decompression using java.we have already encoded image using base64 encoder after than applied below code.But this java code is not working for image. it works properly for char string but not for image.

StringBuffer dest = new StringBuffer();
for (int i = 0; i < source.length(); i++) 
{    
    int runLength = 1;       
    while (i + 1 < source.length() && source.charAt(i) == source.charAt(i + 1)) 
    {
        runLength++;
        i++;
    }
    dest.append(runLength);      
    dest.append(source.charAt(i));
}
return dest.toString();
zx485
  • 28,498
  • 28
  • 50
  • 59
  • _code is not working_ is not a valid question. Show us what you have tried so far for image compression. You can't expect code that works with string to work for image without making changes. See also [ask]. – zett42 Mar 15 '17 at 22:26

2 Answers2

0

you are converting image to string it should be byte instead

your looking for something like this

Java - Convert image to Base64

Community
  • 1
  • 1
0
public byte[] byteArray(BufferedImage image){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] imageInByte = null;
    try{
        ImageIO.write(image, "BMP", baos);
        baos.flush();
        imageInByte = baos.toByteArray();
        baos.close();
    }catch(IOException e){
        System.out.println(e.getMessage());
    }

    return imageInByte;
}
public String getRunLength(){
    StringBuffer dest = new StringBuffer();        
    for(int i =0; i < imageByteArray.length; i++){
        int runlength = 1;
        while(i+1 < imageByteArray.length && imageByteArray[i] == imageByteArray[i+1]){
            runlength++;
            i++;

        }     


        dest.append(runlength);  

        dest.append(imageByteArray[i]);

    }
    return dest.toString();
}