4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swiftはじめたい...その15 実践編 画像情報の取得

Last updated at Posted at 2016-02-11

実践編も始めます。

  • いままでSwiftの基礎的な文法を進めてきましたが、そろそろ実践もかねて動かしていきたいと思います。

画像から画像情報を取る!

  • いきなりCocoaのライブラリを使ってますが...
image_properties.swift
import Cocoa

let filepath = NSHomeDirectory() + "/Desktop/beardo.jpg"
let url: NSURL = NSURL(fileURLWithPath: filepath)
let imageSource = CGImageSourceCreateWithURL(url, nil)
if imageSource != nil {
    if let properties = CGImageSourceCopyPropertiesAtIndex(imageSource!, 0, nil) as Dictionary? {
        for k in properties.keys.sort({String($0) < String($1)}) {
            print( "## " + ( k as! String ) + " =\n" )
            print(properties[k]!)
            print("\n")
        }
        //print(properties)
    }
}

  • playgroundからでは画像がみつからなくて動きませんでした。
  • playgroundからfileを見ると、```/Users/[user]/Library/Containers/com.apple.dt.playground.stub.OSX.MyPlayground-XXXXXX/Data/Desktop/beardo.jpg

## swiftcでbuild

+ ということで、playgroundではなくswiftcでコンパイルした実行ファイルを作ります。
+ 実行ファイルの作り方は、「[Swiftはじめたい...その11 コマンドラインで実行ファイルをつくる。](http://qiita.com/qoAop/items/0e059fd217573d6ef11b)」も見てください。
+ ...といってもコンパイルはこれだけですが、

```shell:コンパイル
swiftc -o image_properties.out image_properties.swift

実行

  • この画像の情報を取得してみます!(投稿した画像は情報は削除してます。)

thumb.png

結果

  • こんな感じになりました。iPhoneで撮影した画像なんですが大量の情報が記録されてますね。
## ColorModel =
RGB

## DPIHeight =
72

## DPIWidth =
72

## Depth =
8

## Orientation =
6

## PixelHeight =
2448

## PixelWidth =
2448

## ProfileName =
sRGB IEC61966-2.1

## {Exif} =
{
    ApertureValue = "2.275007124536905";
    BrightnessValue = "0.6668529904974846";
    ColorSpace = 1;
    ComponentsConfiguration =     (
        1,
        2,
        3,
        0
    );
    DateTimeDigitized = "2016:02:11 16:18:24";
    DateTimeOriginal = "2016:02:11 16:18:24";
    ExifVersion =     (
        2,
        2,
        1
    );
    ExposureBiasValue = 0;
    ExposureMode = 0;
    ExposureProgram = 2;
    ExposureTime = "0.03333333333333333";
    FNumber = "2.2";
    Flash = 16;
    FlashPixVersion =     (
        1,
        0
    );
    FocalLenIn35mmFilm = 39;
    FocalLength = "4.15";
    ISOSpeedRatings =     (
        320
    );
    LensMake = Apple;
    LensModel = "iPhone 5s back camera 4.15mm f/2.2";
    LensSpecification =     (
        "4.15",
        "4.15",
        "2.2",
        "2.2"
    );
    MeteringMode = 3;
    PixelXDimension = 2448;
    PixelYDimension = 2448;
    SceneCaptureType = 0;
    SceneType = 1;
    SensingMethod = 2;
    ShutterSpeedValue = "4.90712145655681";
    SubjectArea =     (
        1492,
        1104,
        609,
        612
    );
    SubsecTimeDigitized = 412;
    SubsecTimeOriginal = 412;
    WhiteBalance = 0;
}

## {GPS} =
{
    Altitude = "37.00854092526691";
    AltitudeRef = 0;
    DateStamp = "2016:02:11";
    DestBearing = "66.18550724637682";
    DestBearingRef = T;
    HPositioningError = 65;
    ImgDirection = "66.18550724637682";
    ImgDirectionRef = T;
    Latitude = "35.xxxxx";
    LatitudeRef = N;
    Longitude = "139.xxxx";
    LongitudeRef = E;
    Speed = 0;
    SpeedRef = K;
    TimeStamp = "07:18:22";
}

## {MakerApple} =
{
    1 = 4;
    14 = 1;
    2 = <7b00〜〜〜3400>;
    20 = 4;
    3 =     {
        epoch = 0;
        flags = 1;
        timescale = 1000000000;
        value = 654444110310291;
    };
    4 = 1;
    5 = 155;
    6 = 153;
    7 = 1;
    8 =     (
        "0.009215862",
        "-0.7401129",
        "-0.683705"
    );
    9 = 275;
}

## {TIFF} =
{
    DateTime = "2016:02:11 16:18:24";
    Make = Apple;
    Model = "iPhone 5s";
    Orientation = 6;
    ResolutionUnit = 2;
    Software = "9.2.1";
    XResolution = 72;
    YResolution = 72;
}

  • サムネイル画像は情報を削ったのでこんな結果になりました。
## ColorModel =
RGB

## Depth =
8

## PixelHeight =
480

## PixelWidth =
480

## {PNG} =
{
    InterlaceType = 0;
}

まとめ

  • なんかCocoaのライブラリを使い出したらObjective-CでもSwiftでもあまり変わらないような...
  • 画像情報って大量に記録されてて怖いですね。
4
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?