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

投稿

9月, 2012の投稿を表示しています

Java: How to Manipulate Image as Double Array (Read and Write)

Read Image as Double Array You might think "We can use Raster#getPixel method if we would like to grab pixels, can't we?". Yes that's true. The key problem is the low performance because getPixel method internally call SampleModel and convert raw value to proper pixel value every method call. So we should call Raster#getPixels method and grab all pixels one method call. I have done similar stuffs in C# and posted on this blog. On the other hand in Java, this is a bit messy because there are a lot of related classes - Raster, ImageIO, Reader, Writer, etc ;). The simplest way I did is: read image as BufferedImage by ImageIO call BufferedImage#getRaster method call Raster#getPixels method Here is an exmple code. public static TemporalImage readImagePixelDoubleArray(InputStream is, int bitPerPixel) throws IOException{ BufferedImage image = ImageIO.read(is); Raster raster = image.getRaster(); int x = raster.getMinX(); int y = raster.

Java: How to Load Classes at Runtime from Jar or Class Files

I wrote java program for loading classes at runtime from jar file or class files Hope this post will help somebody... package com.dukesoftware.utils.reflect; import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.net.URI; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class DynamicClassLoader { public static List<Class<?>> readAllClassesFormJarFile(File jarFilePath) throws IOException{ List<Class<?>> classes = new ArrayList<>(); URL[] urls = { new URL("jar:" + jarFilePath.toURI().toURL() + "!/") }; URLClassLoader loader = URLClassLoader.newInstance(urls); for(Enumeration<JarEntry> en = new JarFile(jarFilePath).entries(); en.hasMoreElements();){ final String name = en.nextElement().getName(

Java: How to Create Indexed PNG Using PNGJ Library

I'm working on Google App Engine for Java (GAEJ) now. Very excited about working on it because can develop Java Web App quite easliy & quickly! However I faced a limitation of image processing on GAEJ. On GAEJ, java.awt pakage.* is not supported. That means we cannot use BufferedImage etc!! As you know, Google provides com.google.appengine.api.images.* for image processing on GAEJ, something like below: ImagesService imagesService = ImagesServiceFactory.getImagesService(); Image srcImage = ImagesServiceFactory.makeImage(srcImageData); // some transformations. Transform crop = ImagesServiceFactory.makeCrop(0.3, 0, 1, 0.70); OutputSettings settings = new OutputSettings(OutputEncoding.PNG); // apply transform Image newImage = imagesService.applyTransform(crop, srcImage, settings); byte[] newImageData = newImage.getImageData(); It can read jpg, png, gif images accroding to the official document. And if transformation is applied, the output binary data becomes png forma

The Easiest Way To Getting XPath of Html Element

Some of you feel a bit annoying for writing XPath to specify html element in Selenium Test. I think the easiest way is using Chrome or using Firebug of Firefox. Chrome Right click html area you would like to get XPath, and select "Inspect element" Right click the html element and select "Copy XPath" in the opened area Firebug Right click html area you would like to get XPath, and select "Inspect Element with Firebug" Right click the html element and select "Copy XPath" in the opened area

C#: How to Read Image as Double Array (for Image Processing)

As you know I am really interested in image processing. For my first step, I wrote the program for reading image as double array in C#. The code is imperfect but I think this will help someone's understand.... Here is an example usage. var original = Bitmap.FromFile(@"C:\temp\test.jpg"); double[,] values = new Bitmap(original).To2DimDoubleArray(); // image processing part! // let's do something fun :D - filtering, binalizing, etc. // for now, removing R value from the image. for (int i = 0, len = values.Length/values.GetLength(0); i < len; i++ ) { //values[0, i] = 0; // a values[1, i] = 0; // r //values[2, i] = 0; // g //values[3, i] = 0; // b } values .ToBitmap(original.Width, original.Height, PixelFormat.Format32bppArgb) .SaveImageAsJpeg(@"c:\temp\test2.jpg", 75); The image processing result of above example program. The left image is original, the right image is the result image which is red value is removed.

C# Dictionary which Returns Default Value if Key is Missing

I have written Dictionary which returns default value if the key is missing in the dictionary. You simply pass lambda function for returning value when the key is missing in Dictionary to its constructor. using System; using System.Collections.Generic; namespace Utility.Data { public class DefaultDictionary<TKey, TValue> : Dictionary<TKey, TValue> { private readonly Func<TKey, TValue> defaulter; public DefaultDictionary(Func<TKey, TValue> defaulter) { this.defaulter = defaulter; } public TValue GetDefaultValueIfMissing(TKey key){ if (ContainsKey(key)) { return this[key]; } return defaulter(key); } } } This is how to use.... var dict = new DefaultDictionary<string, IList<string>>((key) => new List<string>()); // should return empty list var list = dict.GetDefaultValueIfMissing("key1&

C#: Reflection Tips

C# is a very powerful language. However for this "powerful" perspective, there are various way to achieve something and you might hover among which way to take (at least for me :D). In this post I will focus on "Reflection" and introduce some small code spinets. First we assume this trivial class is defined in Utility assembly. namespace Utility.Sample { public class Target { public string wrapByDoubleQuote(string text) { return "\"" + text + "\""; } } } Get Type from String and Instantiate by Activator // type from reflection Type type = Type.GetType("Utility.Sample.Target"); // instantiate object from Type Target target = Activator.CreateInstance(type) as Target; // invoke method normally Console.WriteLine(target.wrapByDoubleQuote("contents")); Instantiate Object from ConstructorInfo and Invoke Method by Reflection // type from reflectio

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

ActionScript 3.0: Resize Image

Main flow part code. package utils.tool { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.net.URLRequest; import flash.events.Event; import utils.ImageUtils; public class ImageResizer { private var maxW:int, maxH:int; private var smoothing:Boolean; private var saveFunction:Function; public function ImageResizer(maxW:int, maxH:int, smoothing:Boolean = false, type:String="jpg") { this.maxW = maxW; this.maxH = maxH; this.smoothing = smoothing; if (type === "png") { saveFunction = ImageUtils.saveBitmapDataAsPNGAsync; } else if(type === "jpg"){ saveFunction = ImageUtils.saveBitmapDataAsJPEGAsync; } else { throw new Error("Not Supported"); } }

ActionScript 3.0: Save BitmapData As JPEG or PNG

public static function saveBitmapDataAsJPEG(path:String, bitmapData:BitmapData, quality:Number=50.0):void { saveByteData(new JPEGEncoder(quality).encode(bitmapData), path); } public static function saveBitmapDataAsJPEGAsync(path:String, bitmapData:BitmapData, quality:Number=50.0):void { saveByteDataAsync(new JPEGEncoder(quality).encode(bitmapData), path); } public static function saveBitmapDataAsPNG(path:String, bitmapData:BitmapData):void { saveByteData(new PNGEncoder().encode(bitmapData), path); } public static function saveBitmapDataAsPNGAsync(path:String, bitmapData:BitmapData):void { saveByteDataAsync(new PNGEncoder().encode(bitmapData), path); } Here is my IO Utility methods. public static function saveByteData(data:ByteArray, path:String):void { try { var file:File = new File(path); var fs:FileStream = new FileStream(); fs.open(file, FileMode.WRITE); fs.writeBytes(data); fs.close(); } catch (err:IOError) { trace(err); } } public static function saveB

JavaのID3タグ解析ライブラリ

Javaでmp3ファイルのID3タグを解析するライブラリを色々と調べてみました。参考になれば幸いです。 以下に挙げていくコードは、特に意味のあることをしている訳ではありませんが、 どうやってタグの読み込み、書き換えを行えるかというサンプルとして参考にして頂ければと思います。 MyID3: a Java ID3 Tag Library import java.io.File; import java.io.IOException; import org.cmc.music.common.MusicMetadata; import org.cmc.music.myid3.MusicMetadataSet; import org.cmc.music.myid3.MyID3; /** * {@link http://www.fightingquaker.com/myid3/} */ public class MyID3Tester { public static void main(String[] args) throws IOException { File mp3File = new File("C:\\temp\\music.mp3"); MyID3 id3 = new MyID3(); MusicMetadataSet src_set = id3.read(mp3File); // read metadata if (src_set == null){ System.out.println("could not read data"); return; } // You can extract simplified information MusicMetadata metadata = src_set.getSimplified(); System.out.println(metadata.getArtist()); System.out.println(metadata.getAlbum()); // this doesn't work for me somehow :( System.out.println(metad

Java: Get Available Font on AWT environment

If you would like to check available font on Java environment, you should use this method. GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); The following code is utility class for Font. package com.dukesoftware.utils.common; import java.awt.Font; import java.awt.GraphicsEnvironment; import java.util.Arrays; public class FontUtils { public static void main(String[] args) { printAvailableFonts(); } public static void printAvailableFonts() { System.out.println(Arrays.toString(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames())); } public static boolean isAvailableFont(String font){ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); final String[] fontNames = ge.getAvailableFontFamilyNames(); for(String name: fontNames){ if(name.equals(font)){ return true; } } return false;