【android】ExifInterfaceで複数のプロパティを取得するときに気を付けること

前々回くらいにorientationの取得の方法を書きましたが、せっかくexifinterfaceを使えるように

なったので他のプロパティもとってみようということで緯度と経度の値も

取得してみることに。しかし、今度はorientationは取れているのに緯度経度の値がnull

になっていました。stack overflowにはiuputStreamを介すとexifの値が付かないということ

でしたが、それではなぜorientationが取れるという疑問しか浮かばず沼りました。

androidについているカメラで撮影してexifを見るとしっかりついているし・・・。

やはりonActivyResultを介さずにメンバ変数でUriを定義したので多少不安はあったのですが

それでもUriファイルからexifプロパティが抜けるなんてありえないですし。

正直あまり自信はなかったのですが、BitmapとUriを引数にしている関数にcontextを追加して

無理やり解決させました。

根拠がない中での修正という一番怖い手法で取得することに成功しましたが、晴れて双方

取得することができたので載せておきます。お疲れ様でした。

</pre>
<pre>public Bitmap load(Bitmap bitmap, Context context,Uri uri){
    try {
        InputStream in = getContentResolver().openInputStream(uri);
        ExifInterface ex = new ExifInterface(in);
        int orientation = ex.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
        in.close();
        push(ex);
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                return rotateImage(bitmap, 90);
            case ExifInterface.ORIENTATION_ROTATE_180:
                return rotateImage(bitmap, 180);
            case ExifInterface.ORIENTATION_ROTATE_270:
                return rotateImage(bitmap, 270);
            default:
                return bitmap;
        }

    }catch(Exception e){
        e.printStackTrace();
    }
    return bitmap;
}

</pre>
<pre>public void push(ExifInterface ex){
    String latitude = ex.getAttribute (ExifInterface.TAG_GPS_LATITUDE);
    String longitude = ex.getAttribute (ExifInterface.TAG_GPS_LONGITUDE);
    String latitudeRef = ex.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
    String longitudeRef = ex.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
    double replaceLati = ExifLatitudeToDegrees(latitudeRef,latitude);
    double replaceLong = ExifLongitudeToDegrees(longitudeRef,longitude);
    System.out.println(replaceLati);
    System.out.println(replaceLong);

}
private double ExifHourMinSecToDegrees(String exifhourminsec) {
    String hourminsec[] = exifhourminsec.split(",");
    String hour[] = hourminsec[0].split("/");
    String min[] = hourminsec[1].split("/");
    String sec[] = hourminsec[2].split("/");
    double dhour = (double)Integer.parseInt(hour[0]) / (double)Integer.parseInt(hour[1]);
    double dmin = (double)Integer.parseInt(min[0]) / (double)Integer.parseInt(min[1]);
    double dsec = (double)Integer.parseInt(sec[0]) / (double)Integer.parseInt(sec[1]);
    double degrees = dhour + dmin / 60.0 + dsec / 3600.0;
    return degrees;
}

private double ExifLatitudeToDegrees(String ref, String latitude) {
    return ref.equals("S") ? -1.0 : 1.0 * ExifHourMinSecToDegrees(latitude);
}</pre>
<pre>

P.S.これに加えて機種別の処理も加味しなければいけないし本当にカメラ周りは鬼畜すぎる・・・。

>株式会社シーポイントラボ

株式会社シーポイントラボ

TEL:053-543-9889
営業時間:9:00~18:00(月〜金)
住所:〒432-8003
   静岡県浜松市中央区和地山3-1-7
   浜松イノベーションキューブ 315
※ご来社の際はインターホンで「316」をお呼びください

CTR IMG