summaryrefslogtreecommitdiffstats
path: root/src/android/jar/src/org/qtproject/qt/android/multimedia/QtExifDataHandler.java
blob: c2699eb1de5e5e9dfaa55496fde4cf2b4b59575d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
package org.qtproject.qt.android.multimedia;

import android.hardware.camera2.CaptureResult;
import android.media.ExifInterface;
import android.os.Build;
import android.util.Log;

import java.io.IOException;

public class QtExifDataHandler {

    private int mFlashFired = 0;
    private long mExposureTime = 0L;
    private float mFocalLength = 0;
    private static String mModel = Build.MANUFACTURER + " " + Build.MODEL;

    public QtExifDataHandler(CaptureResult r)
    {
        Integer flash = r.get(CaptureResult.FLASH_STATE);
        if (flash != null && flash == CaptureResult.FLASH_STATE_FIRED)
            mFlashFired = 1;

        Long exposureTime = r.get(CaptureResult.SENSOR_EXPOSURE_TIME);
        if (exposureTime != null)
            mExposureTime = exposureTime/1000000000;
        mFocalLength = r.get(CaptureResult.LENS_FOCAL_LENGTH);
    }

    public void save(String path)
    {
        ExifInterface exif;
        try {
            exif = new ExifInterface(path);
        } catch ( IOException e ) {
            Log.e("QtExifDataHandler", "Cannot open file: " + path + "\n" + e);
            return;
        }
        exif.setAttribute(ExifInterface.TAG_FLASH, String.valueOf(mFlashFired));
        exif.setAttribute(ExifInterface.TAG_EXPOSURE_TIME, String.valueOf(mExposureTime));
        exif.setAttribute(ExifInterface.TAG_FOCAL_LENGTH, String.valueOf(mFocalLength));
        exif.setAttribute(ExifInterface.TAG_MODEL, mModel);

        try {
            exif.saveAttributes();
        } catch ( IOException e ) {
            Log.e("QtExifDataHandler", "Cannot save file: " + path + "\n" + e);
        }
    }
}