{"id":526,"date":"2009-11-07T16:00:24","date_gmt":"2009-11-07T14:00:24","guid":{"rendered":"http:\/\/raftaman.net\/?p=526"},"modified":"2021-05-15T11:46:41","modified_gmt":"2021-05-15T09:46:41","slug":"quickly-zooming-a-bufferedimage","status":"publish","type":"post","link":"https:\/\/possiblelossofprecision.net\/?p=526","title":{"rendered":"Quickly zooming a BufferedImage"},"content":{"rendered":"<p>If you don&#8217;t need a scaled instance of your BufferedImage in the memory, you can use &#8220;on-the-fly scaling&#8221;. For example, this custom JComponent can be used to display a BufferedImage and zoom it with the <code>setScaleFactor(..)<\/code>-method<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.awt.Dimension;\r\nimport java.awt.Graphics;\r\nimport java.awt.Graphics2D;\r\nimport java.awt.RenderingHints;\r\nimport java.awt.image.BufferedImage;\r\nimport javax.swing.JComponent;\r\n\r\npublic class ZoomableImage extends JComponent {\r\n\r\n    private float scaleFactor;\r\n    \r\n    private BufferedImage originalImage;\r\n\r\n    public ZoomableImage() {\r\n        this.scaleFactor = 1;\r\n        this.setSize(0, 0);\r\n    }\r\n\r\n    public void setImage(BufferedImage image) {\r\n        this.originalImage = image;\r\n        this.setSize(image.getWidth(), image.getHeight());\r\n        \/\/setSize does repainting, no need to call repaint()\r\n        \/\/this.repaint();\r\n    }\r\n\r\n    public void setScaleFactor(float scaleFactor) {\r\n        this.scaleFactor = scaleFactor;\r\n        this.repaint();\r\n    }\r\n\r\n    @Override\r\n    public void paintComponent(Graphics g) {\r\n        if (this.originalImage != null) {\r\n            Graphics2D g2 = (Graphics2D) g;\r\n            int newW = (int) (originalImage.getWidth() * scaleFactor);\r\n            int newH = (int) (originalImage.getHeight() * scaleFactor);\r\n            this.setPreferredSize(new Dimension(newW, newH));\r\n            this.revalidate();\r\n            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,\r\n                    \/\/RenderingHints.VALUE_INTERPOLATION_BILINEAR);\r\n                    RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);\r\n                    \/\/RenderingHints.VALUE_INTERPOLATION_BICUBIC);\r\n            g2.drawImage(originalImage, 0, 0, newW, newH, null);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>For some benchmarking on the different RenderingHints values see Chris Campbell&#8217;s great article <a href=\"http:\/\/today.java.net\/pub\/a\/today\/2007\/04\/03\/perils-of-image-getscaledinstance.html\">The Perils of Image.getScaledInstance()<\/a><\/p>\n<p>Resources:<br \/>\n<a href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/2d\/advanced\/quality.html\">http:\/\/java.sun.com\/docs\/books\/tutorial\/2d\/advanced\/quality.html<\/a><br \/>\n<a href=\"http:\/\/today.java.net\/pub\/a\/today\/2007\/04\/03\/perils-of-image-getscaledinstance.html\">http:\/\/today.java.net\/pub\/a\/today\/2007\/04\/03\/perils-of-image-getscaledinstance.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you don&#8217;t need a scaled instance of your BufferedImage in the memory, you can use &#8220;on-the-fly scaling&#8221;. For example, this custom JComponent can be used to display a BufferedImage and zoom it with the setScaleFactor(..)-method import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import javax.swing.JComponent; public class ZoomableImage extends JComponent { private float scaleFactor; private BufferedImage originalImage;&#8230; <a href=\"https:\/\/possiblelossofprecision.net\/?p=526\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4],"class_list":["post-526","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-java"],"_links":{"self":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=526"}],"version-history":[{"count":6,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/526\/revisions"}],"predecessor-version":[{"id":603,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/526\/revisions\/603"}],"wp:attachment":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}