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

投稿

ラベル(jpeg)が付いた投稿を表示しています

C#: Read Image Metadata Using BitmapMetadata Class

Introduction In this post, I will demonstrate how to read image metadata using BitmapMetadata class in C#. Before staring explanation, we should notice there are two namespaces that can handle image in C#. System.Drawing namespace: I tested in this post System.Windows.Media.Imaging In this post, we will use classes under System.Windows.Media.Imaging. Code for Reading Image Metadata Actually code itself is quite simple :) Please see code below. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows.Media.Imaging; using System.Diagnostics; namespace MetadataExample { public static class ReadImageMetadataExample { public static void a(string fileName) { var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); var img = BitmapFrame.Create(fs); var metadata = img.Metadata as BitmapMetadata; Debug.WriteLine("=== metadata ===...

Java: Available Tags for Extracting Metadata Using Apache Commons Imaging

Introduction In this Java: Read Image Metadata by Java Image IO post, I demonstrated the code snippet for reading image metadata only using standard java imageio library. The problem is the standard image library cannot read jpeg exif metadata as human readable format (they can be extracted as byte[] data). In this post, I will show you the code for reading jpeg exif metadata using Apache Commons Imaging library. In order to run the example code, you should download commons-imaging-1.0-SNAPSHOT.jar from commons-imaging . Basic of Read Exif Data Extracting jpeg exif metadata can be done by using findEXIFValue method in JpegImageMetadata class. The method takes TagInfo as an argument and return back TiffField . Available TagInfo static fields (constants) are mainly defined in the following class in org.apache.commons.imaging.formats.tiff.constants package: ExifTagConstants TiffTagConstants GpsTagConstants Available TagInfo in ExifTagConstants, TiffTagConstants, Gps...

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: 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...

C#: Save Image as JPEG or PNG format

Just a simple code tips for saving image as JPEG or PNG format in C#. Save image as JPEG with Quality parameter public static readonly ImageCodecInfo JPEG_CODEC = GetEncoderInfo("image/jpeg"); public static void SaveImageAsJpeg(this Image src, string fileName, int quality) { var eps = new EncoderParameters(1); var ep = new EncoderParameter(Encoder.Quality, quality); eps.Param[0] = ep; src.Save(fileName.ChangeExtension("jpg"), JPEG_CODEC, eps); } public static ImageCodecInfo GetEncoderInfo(string mineType) { return ImageCodecInfo.GetImageEncoders().First(enc => enc.MimeType == mineType); } Save image as PNG public static void SaveImageAsPng(this Image src, string fileName) { src.Save(fileName.ChangeExtension("png"), ImageFormat.Png); }

ActionScript 3.0: Load SWF, JPG, PNG

Hey guys! I wrote small utility method for loading swf, jpg, png etc. in ActionScript 3.0. public static function loadFromURL(url:String):Loader { var img:Loader = new Loader(); img.load(new URLRequest(url)); return img; } public static function loadFromFile(path:String):Loader { var img:Loader = new Loader(); img.loadBytes(IOUtils.readByteData(path)); return img; } public static function load(url:String, format:String):URLLoader { var loader:URLLoader = new URLLoader(); loader.dataFormat = format; loader.addEventListener(Event.COMPLETE, completeHandler); loader.load(new URLRequest(url)); function completeHandler(event:Event):void { loader.removeEventListener(Event.COMPLETE, completeHandler); //trace(event.target.data); } return loader; }

JavaでCMYK Color SpaceのJPEGを読み込む

Read CMYK JPEG Image CMYKのJPEG画像をJavaで読む方法ではまったので、Google先生で色々調べて見ました。 Problem reading JPEG image using ImageIO.read(File file) によるとImageIOで読めないJPEGファイルはほとんどCMYK Color Spaceの画像のようです。 私の場合もまず読めないJPEGがあることでCMYKのJPEGであることに気がつきました。 という訳で、CMYKのJPEGをどうやって読み込めばいいのということで色々調べてみました。 以下のstackoverflowによると、基本的にまずCMYKのColor Spaceで読み込んで、それからRGB系のColor Spaceに変換することでJavaでも読み込みができるようです。 Pure Java alternative to JAI ImageIO for detecting CMYK images How do I convert images between CMYK and RGB in ColdFusion (Java)? How to convert from CMYK to RGB in Java correctly? ただし、このCMYKのColor Spaceインスタンスを作るのが結構面倒です。 前述のstackoverflowによると、以下の方法があるようです。 CMYKのColorSpaceをSanselanライブラリを使って画像から抜き出す ICC_Profile iccProfile = Sanselan.getICCProfile(new File("filename.jpg")); ColorSpace cs = new ICC_ColorSpace(iccProfile); 自分でCMYKColorSpaceクラスを定義して、インスタンス化する iccプロファイルから、ColorSpaceインスタンスを生成する。ただしiccプロファイルはどこかから自前に用意する必要があります。 ICC_Profile iccProfileCYMK = ICC_Profile.getInstance(new FileInputStre...