{"id":71,"date":"2006-03-13T17:56:20","date_gmt":"2006-03-13T22:56:20","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/?p=71"},"modified":"2006-03-13T17:56:20","modified_gmt":"2006-03-13T22:56:20","slug":"scale-resize-images","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/03\/13\/scale-resize-images\/","title":{"rendered":"Scale Resize Images"},"content":{"rendered":"<p>I recently had to look into the image scaling code that we employ in our application because of a known <a href=\"http:\/\/bugs.sun.com\/bugdatabase\/view_bug.do?bug_id=4937376\">bug<\/a> with the JDK.  Here is the original code we used to resize image icons:<\/p>\n<pre>\nint hint = Image.SCALE_AREA_AVERAGING;\nicon = new ImageIcon(original.getScaledInstance(w,h, hint));\n<\/pre>\n<p>As stated, the above code occasionally will throw a ClassCastException because of a bug with the JDK.  The exception is thrown because of the use of the AreaAveragingScaleFilter class which is instantiated when you use the SCALE_AREA_AVERAGING hint.  A work around for this problem is to use the Image.SCALE_REPLICATE hint but which produces images with jaggies.<\/p>\n<p>You can use a different approach to creating image thumb nails as in the following code:<\/p>\n<pre>\nBufferedImage bi = new BufferedImage(w, h,\n   BufferedImage.TYPE_INT_ARGB);\nbi.getGraphics().drawImage(original, 0, 0, w, h, null);\nicon = new ImageIcon(bi);\n<\/pre>\n<p>You can also scale, rotate, and shear an image using affine transformations.  The following code resizes an image using the AffineTransform class.<\/p>\n<pre>\nAffineTransform tx = new AffineTransform();\ntx.scale(scalew, scaleh);\nAffineTransformOp op = new AffineTransformOp(tx,\n   AffineTransformOp.TYPE_BILINEAR);\nBufferedImage bi = op.filter(ogininal, null);\nicon = new ImageIcon(bi);\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I recently had to look into the image scaling code that we employ in our application because of a known bug with the JDK. Here is the original code we used to resize image icons: int hint = Image.SCALE_AREA_AVERAGING; icon = new ImageIcon(original.getScaledInstance(w,h, hint)); As stated, the above code occasionally will throw a ClassCastException because [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[15,19,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-19","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/71"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=71"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}