How to count your bytes & eat them too.
import java.awt.Image; import javax.imageio.ImageIO; import javax.swing.*; import java.net.URL; import java.io.*; class ImageInfo { public static void main(String[] args) throws Exception { URL url = new URL( "http://l1.yimg.com/t/frontpage/baba-ramdev-310511-60.jpg"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream is = url.openStream(); byte[] b = new byte[2^16]; int read = is.read(b); while (read>-1) { baos.write(b,0,read); read = is.read(b); } int countInBytes = baos.toByteArray().length; ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray()); Image image = ImageIO.read(bais); int width = image.getWidth(null); int height = image.getHeight(null); String imageInfo = width + "x" + height + " px, " + countInBytes + " bytes."; JOptionPane.showMessageDialog(null, new JLabel(imageInfo, new ImageIcon(image), SwingConstants.CENTER)); } }
Read full article from how to get the size of an image in java - Stack Overflow
No comments:
Post a Comment