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

投稿

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); }