Skip to content

Commit 223d4c0

Browse files
committed
metadata of motion photo
1 parent d93a159 commit 223d4c0

File tree

7 files changed

+184
-2
lines changed

7 files changed

+184
-2
lines changed

app/src/main/java/com/hss01248/imageloaderdemo/MainActivity.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.hss01248.bigimageviewpager.LargeImageViewer;
4141
import com.hss01248.bigimageviewpager.MyLargeImageView;
4242
import com.hss01248.fileoperation.FileDeleteUtil;
43+
import com.hss01248.fullscreendialog.FullScreenDialogUtil;
4344
import com.hss01248.glide.aop.file.AddByteUtil;
4445
import com.hss01248.glide.aop.file.DirOperationUtil;
4546
import com.hss01248.image.ImageLoader;
@@ -52,6 +53,7 @@
5253
import com.hss01248.img.compressor.ImageDirCompressor;
5354
import com.hss01248.img.compressor.UiForDirCompress;
5455
import com.hss01248.media.metadata.ExifUtil;
56+
import com.hss01248.motion_photos.MotionPhotoUtil;
5557
import com.hss01248.ui.pop.list.PopList;
5658
import com.hss01248.viewholder_media.FileTreeViewHolder;
5759
import com.hss01248.webviewspider.SpiderWebviewActivity;
@@ -660,6 +662,31 @@ public void viewDir(View view) {
660662
FileTreeViewHolder.viewDirInActivity(Environment.getExternalStorageDirectory().getAbsolutePath());
661663
}
662664

665+
public void motionPhoto(View view) {
666+
ImgDataSeletor.startPickOneWitchDialog(this, new TakeOnePhotoListener() {
667+
@Override
668+
public void onSuccess(String path) {
669+
ToastUtils.showShort(path);
670+
LargeImageViewer.showOne(path);
671+
Map<String, Object> metadata = MotionPhotoUtil.metadata(path);
672+
// Gson gson = new GsonBuilder().setPrettyPrinting().create();
673+
//String json = gson.toJson(metadata);
674+
FullScreenDialogUtil.showMap("meta",metadata);
675+
676+
}
677+
678+
@Override
679+
public void onFail(String path, String msg) {
680+
681+
}
682+
683+
@Override
684+
public void onCancel() {
685+
686+
}
687+
});
688+
}
689+
663690

664691

665692
/*Intent intent = new Intent(this,BigImageActy.class);

app/src/main/res/layout/activity_main.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
android:textAllCaps="false"
5050
android:layout_width="match_parent"
5151
android:layout_height="wrap_content" />
52+
<Button
53+
android:text="motion photo"
54+
android:onClick="motionPhoto"
55+
android:textAllCaps="false"
56+
android:layout_width="match_parent"
57+
android:layout_height="wrap_content" />
5258

5359
<Button
5460
android:text="选图-360全景图test"

motion-photos-android/src/main/java/com/hss01248/motion_photos_android/AndroidMotionImpl.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.hss01248.motion_photos_android;
22

3+
import android.media.MediaMetadataRetriever;
34
import android.net.Uri;
45

56
import androidx.exifinterface.media.ExifInterface;
@@ -13,7 +14,11 @@
1314

1415
import java.io.File;
1516
import java.io.FileInputStream;
17+
import java.io.IOException;
1618
import java.io.InputStream;
19+
import java.lang.reflect.Field;
20+
import java.util.Map;
21+
import java.util.TreeMap;
1722
import java.util.concurrent.CountDownLatch;
1823
import java.util.concurrent.TimeUnit;
1924

@@ -96,6 +101,62 @@ public String mp4CacheFile(String path) {
96101
return file2.getAbsolutePath();
97102
}
98103

104+
@Override
105+
public Map<String, Object> metaOfImage(String fileOrUriPath) {
106+
InputStream stream = null;
107+
try {
108+
stream = steam(fileOrUriPath);
109+
if(stream ==null){
110+
return null;
111+
}
112+
Map<String, Object> map = new TreeMap<>();
113+
ExifInterface exifInterface = new ExifInterface(stream);
114+
Field[] fields = ExifInterface.class.getDeclaredFields();
115+
for (Field field : fields) {
116+
if(field.getName().startsWith("TAG_")){
117+
field.setAccessible(true);
118+
String str = field.get(ExifInterface.class)+"";
119+
map.put(str,exifInterface.getAttribute(str));
120+
121+
}
122+
}
123+
return map;
124+
}catch (Throwable throwable){
125+
throwable.printStackTrace();
126+
return null;
127+
}finally {
128+
if(stream !=null){
129+
try {
130+
stream.close();
131+
} catch (IOException e) {
132+
e.printStackTrace();
133+
}
134+
}
135+
}
136+
}
137+
138+
@Override
139+
public Map<String, Object> metaOfVideo(String fileOrUriPath) {
140+
141+
try {
142+
Map<String, Object> map = new TreeMap<>();
143+
MediaMetadataRetriever exifInterface = new MediaMetadataRetriever();
144+
exifInterface.setDataSource(fileOrUriPath);
145+
Field[] fields = MediaMetadataRetriever.class.getDeclaredFields();
146+
for (Field field : fields) {
147+
if(field.getName().startsWith("METADATA_KEY_")){
148+
field.setAccessible(true);
149+
int str = (int) field.get(ExifInterface.class);
150+
map.put(field.getName().substring("METADATA_KEY_".length()).toLowerCase(),exifInterface.extractMetadata(str));
151+
}
152+
}
153+
return map;
154+
}catch (Throwable throwable){
155+
throwable.printStackTrace();
156+
return null;
157+
}
158+
}
159+
99160
public static File compressMp4File(String fileOrUriPath){
100161

101162
File dir = new File(Utils.getApp().getExternalCacheDir(),"motion-videos") ;

motion-photos/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,12 @@ plugins {
55
java {
66
sourceCompatibility = JavaVersion.VERSION_1_8
77
targetCompatibility = JavaVersion.VERSION_1_8
8+
}
9+
10+
repositories {
11+
mavenCentral()
12+
}
13+
14+
dependencies {
15+
implementation 'com.google.code.gson:gson:2.8.5'
816
}

motion-photos/src/main/java/com/hss01248/motion_photos/IMotion.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.hss01248.motion_photos;
22

3+
import java.util.Map;
4+
35
/**
46
* @Despciption todo
57
* @Author hss
@@ -13,4 +15,8 @@ public interface IMotion {
1315
String readXmp(String fileOrUriPath) throws Throwable;
1416

1517
String mp4CacheFile(String fileOrUriPath);
18+
19+
Map<String,Object> metaOfImage(String fileOrUriPath);
20+
21+
Map<String,Object> metaOfVideo(String fileOrUriPath);
1622
}

motion-photos/src/main/java/com/hss01248/motion_photos/JavaMotion.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.hss01248.motion_photos;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.reflect.TypeToken;
5+
36
import java.io.BufferedReader;
47
import java.io.File;
58
import java.io.IOException;
69
import java.io.InputStreamReader;
7-
import java.io.OutputStream;
10+
import java.util.List;
11+
import java.util.Map;
812

913
/**
1014
* @Despciption todo
@@ -33,7 +37,6 @@ public String readXmp(String filePath) throws Throwable{
3337
String cmdarray[] = new String[]{"exiftool",filePath,"-xmp","-b"};
3438
try {
3539
Process exec = Runtime.getRuntime().exec(cmdarray);
36-
OutputStream outputStream = exec.getOutputStream();
3740
StringBuilder output = new StringBuilder();
3841
BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
3942
String line;
@@ -56,4 +59,44 @@ public String readXmp(String filePath) throws Throwable{
5659
public String mp4CacheFile(String path) {
5760
return path+".mp4";
5861
}
62+
63+
@Override
64+
public Map<String, Object> metaOfImage(String fileOrUriPath) {
65+
File file = new File(fileOrUriPath);
66+
if(!file.exists() ){
67+
return null;
68+
}
69+
if(!file.isFile()){
70+
return null;
71+
}
72+
if(file.length() ==0){
73+
return null;
74+
}
75+
String cmdarray[] = new String[]{"exiftool",fileOrUriPath,"-j"};
76+
try {
77+
Process exec = Runtime.getRuntime().exec(cmdarray);
78+
StringBuilder output = new StringBuilder();
79+
BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
80+
String line;
81+
while ((line = reader.readLine()) != null) {
82+
output.append(line).append(System.lineSeparator());
83+
}
84+
reader.close();
85+
86+
// 将输出转换为字符串
87+
String result = output.toString();
88+
Gson gson = new Gson();
89+
List<Map> map2 = gson.fromJson(result, new TypeToken<List<Map>>(){}.getType());
90+
//System.out.println(result);
91+
return map2.get(0);
92+
} catch (IOException e) {
93+
e.printStackTrace();
94+
}
95+
return null;
96+
}
97+
98+
@Override
99+
public Map<String, Object> metaOfVideo(String fileOrUriPath) {
100+
return metaOfImage(fileOrUriPath);
101+
}
59102
}

motion-photos/src/main/java/com/hss01248/motion_photos/MotionPhotoUtil.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.hss01248.motion_photos;
22

33

4+
import com.google.gson.Gson;
5+
import com.google.gson.GsonBuilder;
6+
47
import java.io.File;
58
import java.io.IOException;
69
import java.io.RandomAccessFile;
10+
import java.util.Map;
11+
import java.util.TreeMap;
712
import java.util.regex.Matcher;
813
import java.util.regex.Pattern;
914

@@ -21,6 +26,32 @@ public static void main(String[] args) {
2126
String google = "/Users/hss/Documents/PXL_20240918_013738178.MP.jpg";
2227
boolean isMotionImage = isMotionImage(xiaomi, true);
2328
boolean is2 = isMotionImage(google, true);
29+
30+
Map<String, Object> metadata = metadata(xiaomi);
31+
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
32+
System.out.println(gson.toJson(metadata));
33+
34+
}
35+
36+
public static Map<String,Object> metadata(String fileOrUriPath){
37+
boolean isMotion = isMotionImage(fileOrUriPath,true);
38+
Map<String, Object> map = new TreeMap<>();
39+
map.put("0-path",fileOrUriPath);
40+
if(fileOrUriPath !=null && !fileOrUriPath.equals("")){
41+
File file = new File(fileOrUriPath);
42+
map.put("0-length",file.length());
43+
map.put("0-lastModified",file.lastModified());
44+
}
45+
46+
Map<String, Object> stringObjectMap = motion.metaOfImage(fileOrUriPath);
47+
map.put("image",stringObjectMap);
48+
if(isMotion){
49+
String path = getMotionVideoPath(fileOrUriPath);
50+
Map<String, Object> stringObjectMap1 = motion.metaOfVideo(path);
51+
map.put("video",stringObjectMap1);
52+
}
53+
return map;
54+
2455
}
2556

2657

0 commit comments

Comments
 (0)