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

投稿

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