Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a buffer that i want to convert it to jpg60 and save it on memory to be sent on http response using Java language in less then 100-200ms.
after my tests i found that save it as jpg file on disk is faster then on memory.
my question is : i want to save the buffer on memory as jpg60 and send it to client side in less then 100-200ms???
Java
BufferedImage out = new BufferedImage(col, row, BufferedImage.TYPE_INT_RGB);

		int ii = 0, jj = 0;

		for (int i = 0; i < row; i--) {
			for (int j = 0; j < col; j++) {
				out.setRGB(i, j, m_outData[(i * col) + j]);
			}
		}

// ByteArrayOutputStream outStream = new ByteArrayOutputStream();

		try {

			// ImageOutputStream ios =
			// ImageIO.createImageOutputStream(outStream);
			// Iterator<ImageWriter> iter =
			// ImageIO.getImageWritersByFormatName("jpeg");
			// ImageWriter writer = iter.next();
			// ImageWriteParam iwp = writer.getDefaultWriteParam();
			//
			// iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
			// iwp.setCompressionQuality(0.60f);

			// writer.setOutput(new MemoryCacheImageOutputStream(outStream));
			// writer.write(null, new IIOImage(out, null, null), jpegParams);

			// // write to file on hard disk
			outPut = new File(prefix + "test.jpg");

			// writer.setOutput(outPut);
			// writer.write(null, new IIOImage(out, null, null), jpegParams);

			ImageIO.write(out, "jpg", outPut);

			// write as byte array to transfered to client
			// ImageIO.write(out, "jpg", outStream);
			// outStream.flush();

			// imageToByte = outStream.toByteArray();
			// ===========================================

			outPut = null;

		} catch (Exception e) {
			System.out.println("saveImage " + e.getMessage());
		}


What I have tried:

FileOutPutStream, ByteArrayOutPutStream
Posted
Updated 20-May-16 7:51am
Comments
Mehdi Gholam 19-May-16 10:59am    
I seriously doubt saving to disk is faster than memory, and sending to a client over http in less than 200ms will depend on your connection which is unreliable.
Sergey Alexandrovich Kryukov 19-May-16 13:15pm    
"JPEG in memory" is ambiguous. You could read JPEG file byte by byte into memory and call it "JPEG in memory". But you can also create an instance of a bitmap type and read the image from JPEG. Such bitmap would be uncompressed fully functional image, essentially, not JPEG. So, what do you really mean to do? What do you want to achieve, ultimately?
—SA

Your test is more than likely wrong somehow. Saving to a memory stream is always going to be faster than saving to disk.

What is wrong? I have no idea since the code you posted doesn't show how you tested this and how it was timed.
 
Share this answer
 
Comments
Hamza Halim 21-May-16 9:49am    
thank you for your solution but if you know a solution that make a fast conversion from int array to jpeg with 0.60f in quality and send it as byte array to client side
Quote:
after my tests i found that save it as jpg file on disk is faster then on memory.
Basically, It is impossible.
You don't measure the same thing.
your jpg file is first built in memory, and then wrote to disk. It is impossible the last operation which is extra makes the whole thing faster.

Advice: use a profiler to see where you spend time.
Make sure your code do the same thing for both memory and disk.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900