1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "exif_utils.h"
17 
18 #include "image_source.h"
19 #include "media_exif.h"
20 #include "media_log.h"
21 #include "medialibrary_errno.h"
22 #include "medialibrary_tracer.h"
23 
24 using namespace std;
25 
26 namespace OHOS {
27 namespace Media {
28 
29 const double TIMER_MULTIPLIER = 60.0;
30 
WriteGpsExifInfo(const string & path,double longitude,double latitude)31 int32_t ExifUtils::WriteGpsExifInfo(const string &path, double longitude, double latitude)
32 {
33     MediaLibraryTracer tracer;
34     tracer.Start("WriteGpsExifInfo");
35     uint32_t errorCode = 0;
36     SourceOptions opts;
37     std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(path, opts, errorCode);
38     if (imageSource == nullptr) {
39         MEDIA_ERR_LOG("imageSource is nullptr");
40         return E_ERR;
41     }
42 
43     uint32_t index = 0;
44     uint32_t ret = imageSource->ModifyImageProperty(index, PHOTO_DATA_IMAGE_GPS_LONGITUDE,
45         LocationValueToString(longitude), path);
46     if (ret != E_OK) {
47         MEDIA_ERR_LOG("modify image property longitude fail %{public}d", ret);
48     }
49 
50     ret = imageSource->ModifyImageProperty(index, PHOTO_DATA_IMAGE_GPS_LONGITUDE_REF,
51         longitude > 0.0 ? "E" : "W", path);
52     if (ret != E_OK) {
53         MEDIA_ERR_LOG("modify image property longitude ref fail %{public}d", ret);
54     }
55 
56     ret = imageSource->ModifyImageProperty(index, PHOTO_DATA_IMAGE_GPS_LATITUDE, LocationValueToString(latitude), path);
57     if (ret != E_OK) {
58         MEDIA_ERR_LOG("modify image property latitude fail %{public}d", ret);
59     }
60 
61     ret = imageSource->ModifyImageProperty(index, PHOTO_DATA_IMAGE_GPS_LATITUDE_REF, latitude > 0.0 ? "N" : "S", path);
62     if (ret != E_OK) {
63         MEDIA_ERR_LOG("modify image property latitude ref fail %{public}d", ret);
64     }
65 
66     return E_OK;
67 }
68 
LocationValueToString(double value)69 string ExifUtils::LocationValueToString(double value)
70 {
71     string result = "";
72     double positiveValue = value;
73     if (value < 0.0) {
74         positiveValue = 0.0 - value;
75     }
76 
77     int degrees = static_cast<int32_t>(positiveValue);
78     result = result + to_string(degrees) + ", ";
79     positiveValue -= (double)degrees;
80     positiveValue *= TIMER_MULTIPLIER;
81     int minutes = (int)positiveValue;
82     result = result + to_string(minutes) + ", ";
83     positiveValue -= (double)minutes;
84     positiveValue *= TIMER_MULTIPLIER;
85     result = result + to_string(positiveValue);
86     return result;
87 }
88 } // namespace Media
89 } // namespace OHOS