Compressing Images

edited November 2013 in Questions about Code

I am looking for a good way to compress images, fast(er) I have this, but it needs to be faster as I am streaming video over a network

byte[] compressImg(PImage img,int w, int h, float quality){
    byte[] output;
    BufferedImage bimg = new BufferedImage(w,h, BufferedImage.TYPE_INT_RGB );
    bimg.setRGB( 0, 0, w,h, img.pixels, 0, img.width);
    Iterator iterator=ImageIO.getImageWritersByFormatName("jpg");
    ImageWriter writer=(ImageWriter);
    ImageWriteParam p=writer.getDefaultWriteParam();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageOutputStream ios=null;
      ios = ImageIO.createImageOutputStream(bos);
      writer.write(null,new IIOImage(bimg,null,null),p);
    }catch(Exception e){
    output = new byte[bos.toByteArray().length];
    output = bos.toByteArray();
    return output;  

btw i want to output it to a byte array in the end, but any help on the image compression part would be awesome


  • or maybe just some help on optimization of the above

  • is this just way past what I should be doing with processing?

  • Answer ✓

    Image compression, if you need it to be efficient, is quite complex and time-consuming. There are simpler algorithms, like RLE, but they are efficient only with simple images (B&W, flat colors, etc.).

  • I think I am going to end up streaming the video. hopefully the above code helps someone. From what I have gathered, gstreamer is the most used solution. So I will dig into that

Sign In or Register to comment.