-5

I have an image in a byte[] variable and my goal is to convert it to JPG format and then create a BufferedImage variable from it.

Speed is very important here. Just to create a BufferedImage from the byte[] on a 500kb image takes 0.5 seconds.

One approach is (but very slow) is:

  1. create a BufferedImage from the image byte[]

  2. use ImageIO.write to convert the image to jpg and write it to disk

  3. read the image from disk and create a BufferedImage from it

Is there any faster way to do this, please?

edit:

The byte array contains the content of a valid PNG, JPG or GIF image i have read the HDD.

SuperHeroY
  • 169
  • 1
  • 5
  • 3
    Hint: `byte[]` is not a *format*. That is just an array of bytes. The *format* of an image is based on its content. You probably want to make your question more clear by avoiding such unclear language. In other words: you do not **convert* byte arrays into JPG. Probably your data is already in JPG format, isnt it? – GhostCat Jan 02 '17 at 08:02
  • @GhostCat I apologize, English is not my first language, can you please edit my question? – SuperHeroY Jan 02 '17 at 08:02
  • I cant because I dont know what you are after ;-) – GhostCat Jan 02 '17 at 08:03
  • @GhostCat convert my byte[] variable to a BufferedImage variable, while converting the image to jpg – SuperHeroY Jan 02 '17 at 08:04
  • Where is your byte[] coming from? – GhostCat Jan 02 '17 at 08:05
  • Since we have no idea what this byte array contains in the first place, no, we can't do that. Where does this byte array come from? What does it contain? – JB Nizet Jan 02 '17 at 08:05
  • @JB Nizet it contains the content of an image i got from the hdd. – SuperHeroY Jan 02 '17 at 08:06
  • In what format is it store on your disk ? ,jpg, .bmp, ... what is the extension of the file. Image is not a format and every format is stored differently so this means that this needs to be readed corresponding to the format of the file. – AxelH Jan 02 '17 at 08:15
  • @AxelH it can be any valid png, jpg or gif – SuperHeroY Jan 02 '17 at 08:18
  • Did you do research ? [GIF](http://stackoverflow.com/q/3717052/4391450), [PNG](http://stackoverflow.com/q/2290336/4391450), ... There is already planty of question about this. You just need to create a correct code to manage every case you need. – AxelH Jan 02 '17 at 08:21
  • @AxelH yes, but the problem is speed – SuperHeroY Jan 02 '17 at 08:21
  • 1
    Compare your code to existing solutions, see if you use the same. Since I don't see any code, hard to know if this is correct – AxelH Jan 02 '17 at 08:23

1 Answers1

0

There's a library called OpenCV. It's a C/C++ library but it also has support for Java through a JNI wrapper. I have to mention that OpenCV is specialized in computer vision and general image processing and not so heavily in compressing images and having rocket science methods to write images to disk.


A JPG image of 500 kB is probably much larger in raw data when you read it into memory.


Honestly, if I were you, I'd stick with the stuff that's already there in the Java API (such as ImageIO).

If you plan on processing loads of images, you could create a list of the files you wish to convert to a different format, design your application be multi-threaded and separate the tasks over multiple threads. That's really the way to win back time.

kevto
  • 529
  • 6
  • 15