スキップしてメイン コンテンツに移動

投稿

Java: Save BufferedImage as JPEG

Here is a way to save BufferedImage as JEPG using ImageWriter. public static void writeAsJpeg(BufferedImage image, float quality, File outputFile) throws IOException { ImageWriter writer = getImageWriter("jpg"); JPEGImageWriteParam iwp = new JPEGImageWriteParam(Locale.getDefault()); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(quality); try (ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile)){ writer.setOutput(ios); writer.write(null, new IIOImage(image,null,null),iwp); ios.flush(); } finally{ writer.dispose(); } } private static ImageWriter getImageWriter(String ext) { Iterator iter = ImageIO.getImageWritersByFormatName(ext); if (iter.hasNext()) { return iter.next(); } throw new IllegalStateException("Unsupported " + ext); }

ActionScript 3.0: Download Resource and Save as File Asynchronously

Here is a utility class for downloading resource and saving as file asynchronously . package utils.file { import flash.events.Event; import flash.events.EventDispatcher; import flash.events.ProgressEvent; import flash.filesystem.File; import flash.filesystem.FileStream; import flash.filesystem.FileMode; import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.net.URLRequest; public class AsyncFileSaveDownloader extends EventDispatcher { public function AsyncFileSaveDownloader() { } public function download(url:String, path:String):void { var loader:URLLoader = new URLLoader(); loader.dataFormat = URLLoaderDataFormat.BINARY; loader.addEventListener(Event.COMPLETE, completeDownload); loader.load(new URLRequest(url)); function completeDownload(cevt:Event):void { l

Java File Copy - Guava, Java7, Java Legacy, FileChannel, CommonsIO

There are a lot of way to copy file in Java. I have wrote sample codes for them. I think Java 7 way will be the normal & standard way soon!! import static com.dukesoftware.utils.common.ExceptionUtils.throwNotImplementedException; import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.net.URI; import java.nio.channels.FileChannel; import java.nio.file.Paths; import org.apache.commons.io.FileUtils; import com.google.common.io.Closeables; import com.google.common.io.Files; public enum Copier { GUAVA{ @Override public void copy(File src, File dest) throws IOException { Files.copy(src, dest); } }, JAVA7{ @

Java: Auto Resize Image Canvas (Swing)

I wrote an image canvas class which can be automatically re-size image so that it fits the window. The code itself is pretty simple. package com.dukesoftware.utils.swing.others; import java.awt.Graphics; import java.awt.Image; import javax.swing.JPanel; public class AutoResizeImageCanvas extends JPanel{ private Image img; public void setImage(Image img){ this.img = img; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); final int panelWidth = getWidth(); final int panelHeight = getHeight(); g.fillRect(0, 0, panelWidth, panelHeight); if(img != null){ final int imgWidth = img.getWidth(null); final int imgHeight = img.getHeight(null); final double rW = (double)panelWidth / imgWidth; final double rH = (double)panelHeight / imgHeight; int newWidth; int newHeight; if(rW < rH){

Ant Build Script for ActionScript 3.0 (AIR Application)

This is my ant build script used for building app. You know FlashDevelop can build swf however it only compiles minimum classes directly used in the program. So I always run ant build script in order to compile all ".as" and ".mxml" source files If you would like to use my ant script, need some preparation.... Flex PMD: You can download Flex PMD, which is used in my ant script, from here . PMD xslt: I just downloaded official sourceforge PMD zip (the version is 5.0.0 when I wrote this article) and simply picked up some useful xslt from pmd-src-5.0.0/etc/xslt My project directory hierarchy is something like this... <?xml version="1.0" encoding="utf-8" ?> <project name="DukeSoftwareBuildAS3" default="all" basedir="."> <property name="root.dir" value=".." /> <property file="${root.dir}/proj.properties" /> <property file="build.prop