Calculate filesize of resized image (for example JPEG) before resize
How can I calculate file size of resized image (for example JPEG) befo开发者_StackOverflow中文版re I resize it?
I need to send HTTP request Content-length, then I will resize image, else nginx get me time out response.
zen0n, if you absolutely have to do this, using Andrew's suggestion with a minor modification is the only way to do it (do the entire resize operation, outputting the bytes to a ByteArrayOutputStream so you can count them before hand).
The modification I would use is doing this server-side with a simple image-resizing library like imgscalr, pass in the BufferedImage that represents the loaded image, and get back a resized BufferedImage that represents the new-sized image. Then pump it through ImageIO.write and output it to a BufferedArrayOutputStream.
Then set your "Content-Length" header and stream the image contents back.
Because converting raw image bytes to a JPG introduces a compression and encoding sequence of events, there is no accurate way to pre-predict the exact file length before hand and then use ImageIO.write to stream the bytes back to the client. You'll have to resize the image completely first (in memory) using the technique mentioned above, THEN write the data back to the client.
To do it on the client side, use a free floating or embedded applet or a frame.
Use ImageIO.write(Image,String,InputStream)
to serialize the image to a ByteArrayOutputStream
.
The size of the resized image can then be found by, baos.toByteArray().length
.
If the applet or frame is launched using Java Web Start, it will have access to the JNLP API services. Then the FileOpenService can be used to obtain images from the local file system of the client, in a sand-boxed app. A sand-boxed app. can only upload the images to the server it came from.
Here is my demo. of the JNLP file services.
This is my solution:
public class IOSUtil {
private static int FILEBUFFERSIZE = 1024;
public static int input2OutputStream(InputStream is, OutputStream os)
throws IOException {
byte[] bytes = new byte[FILEBUFFERSIZE];
int bytesRead;
int allBytes = 0;
while ((bytesRead = is.read(bytes)) != -1) {
os.write(bytes, 0, bytesRead);
allBytes+=bytesRead;
}
is.close();
os.close();
return allBytes;
}
public IOSUtil() {
super();
}
}
................
protected InputStream setLength(InputStream is) throws IOException
{
PipedInputStream in = new PipedInputStream();
PipedOutputStream pout = new PipedOutputStream(in);
length = IOSUtil.input2OutputStream(is, pout);
return in;
}
@Override
protected InputStream getInputStreamContent(CMObject cmObject) throws CMException {
try {
Integer cacheLength = new Integer(0);
if((cacheLength = (Integer) getCacheService().getCache(getCacheService().createKeyForImageResize(cmObject.getCode(), ImageProcessor.getWidth(width, height), height))) == null)
{
return setLength(resizeImage(cmObject));
} else
{
length = cacheLength;
return resizeImage(cmObject);
}
} catch (IOException e) {
length = super.getLength(cmObject);
log.error("Error resize image", e);
}
return super.getInputStreamContent(cmObject);
}
private InputStream resizeImage(CMObject cmObject)
{
.......
}
精彩评论