Skip to content

Commit

Permalink
metadata of motion photo
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Oct 8, 2024
1 parent d93a159 commit 223d4c0
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 2 deletions.
27 changes: 27 additions & 0 deletions app/src/main/java/com/hss01248/imageloaderdemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.hss01248.bigimageviewpager.LargeImageViewer;
import com.hss01248.bigimageviewpager.MyLargeImageView;
import com.hss01248.fileoperation.FileDeleteUtil;
import com.hss01248.fullscreendialog.FullScreenDialogUtil;
import com.hss01248.glide.aop.file.AddByteUtil;
import com.hss01248.glide.aop.file.DirOperationUtil;
import com.hss01248.image.ImageLoader;
Expand All @@ -52,6 +53,7 @@
import com.hss01248.img.compressor.ImageDirCompressor;
import com.hss01248.img.compressor.UiForDirCompress;
import com.hss01248.media.metadata.ExifUtil;
import com.hss01248.motion_photos.MotionPhotoUtil;
import com.hss01248.ui.pop.list.PopList;
import com.hss01248.viewholder_media.FileTreeViewHolder;
import com.hss01248.webviewspider.SpiderWebviewActivity;
Expand Down Expand Up @@ -660,6 +662,31 @@ public void viewDir(View view) {
FileTreeViewHolder.viewDirInActivity(Environment.getExternalStorageDirectory().getAbsolutePath());
}

public void motionPhoto(View view) {
ImgDataSeletor.startPickOneWitchDialog(this, new TakeOnePhotoListener() {
@Override
public void onSuccess(String path) {
ToastUtils.showShort(path);
LargeImageViewer.showOne(path);
Map<String, Object> metadata = MotionPhotoUtil.metadata(path);
// Gson gson = new GsonBuilder().setPrettyPrinting().create();
//String json = gson.toJson(metadata);
FullScreenDialogUtil.showMap("meta",metadata);

}

@Override
public void onFail(String path, String msg) {

}

@Override
public void onCancel() {

}
});
}



/*Intent intent = new Intent(this,BigImageActy.class);
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
android:textAllCaps="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="motion photo"
android:onClick="motionPhoto"
android:textAllCaps="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:text="选图-360全景图test"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hss01248.motion_photos_android;

import android.media.MediaMetadataRetriever;
import android.net.Uri;

import androidx.exifinterface.media.ExifInterface;
Expand All @@ -13,7 +14,11 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -96,6 +101,62 @@ public String mp4CacheFile(String path) {
return file2.getAbsolutePath();
}

@Override
public Map<String, Object> metaOfImage(String fileOrUriPath) {
InputStream stream = null;
try {
stream = steam(fileOrUriPath);
if(stream ==null){
return null;
}
Map<String, Object> map = new TreeMap<>();
ExifInterface exifInterface = new ExifInterface(stream);
Field[] fields = ExifInterface.class.getDeclaredFields();
for (Field field : fields) {
if(field.getName().startsWith("TAG_")){
field.setAccessible(true);
String str = field.get(ExifInterface.class)+"";
map.put(str,exifInterface.getAttribute(str));

}
}
return map;
}catch (Throwable throwable){
throwable.printStackTrace();
return null;
}finally {
if(stream !=null){
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

@Override
public Map<String, Object> metaOfVideo(String fileOrUriPath) {

try {
Map<String, Object> map = new TreeMap<>();
MediaMetadataRetriever exifInterface = new MediaMetadataRetriever();
exifInterface.setDataSource(fileOrUriPath);
Field[] fields = MediaMetadataRetriever.class.getDeclaredFields();
for (Field field : fields) {
if(field.getName().startsWith("METADATA_KEY_")){
field.setAccessible(true);
int str = (int) field.get(ExifInterface.class);
map.put(field.getName().substring("METADATA_KEY_".length()).toLowerCase(),exifInterface.extractMetadata(str));
}
}
return map;
}catch (Throwable throwable){
throwable.printStackTrace();
return null;
}
}

public static File compressMp4File(String fileOrUriPath){

File dir = new File(Utils.getApp().getExternalCacheDir(),"motion-videos") ;
Expand Down
8 changes: 8 additions & 0 deletions motion-photos/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ plugins {
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

repositories {
mavenCentral()
}

dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hss01248.motion_photos;

import java.util.Map;

/**
* @Despciption todo
* @Author hss
Expand All @@ -13,4 +15,8 @@ public interface IMotion {
String readXmp(String fileOrUriPath) throws Throwable;

String mp4CacheFile(String fileOrUriPath);

Map<String,Object> metaOfImage(String fileOrUriPath);

Map<String,Object> metaOfVideo(String fileOrUriPath);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.hss01248.motion_photos;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
* @Despciption todo
Expand Down Expand Up @@ -33,7 +37,6 @@ public String readXmp(String filePath) throws Throwable{
String cmdarray[] = new String[]{"exiftool",filePath,"-xmp","-b"};
try {
Process exec = Runtime.getRuntime().exec(cmdarray);
OutputStream outputStream = exec.getOutputStream();
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
String line;
Expand All @@ -56,4 +59,44 @@ public String readXmp(String filePath) throws Throwable{
public String mp4CacheFile(String path) {
return path+".mp4";
}

@Override
public Map<String, Object> metaOfImage(String fileOrUriPath) {
File file = new File(fileOrUriPath);
if(!file.exists() ){
return null;
}
if(!file.isFile()){
return null;
}
if(file.length() ==0){
return null;
}
String cmdarray[] = new String[]{"exiftool",fileOrUriPath,"-j"};
try {
Process exec = Runtime.getRuntime().exec(cmdarray);
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append(System.lineSeparator());
}
reader.close();

// 将输出转换为字符串
String result = output.toString();
Gson gson = new Gson();
List<Map> map2 = gson.fromJson(result, new TypeToken<List<Map>>(){}.getType());
//System.out.println(result);
return map2.get(0);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
public Map<String, Object> metaOfVideo(String fileOrUriPath) {
return metaOfImage(fileOrUriPath);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.hss01248.motion_photos;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -21,6 +26,32 @@ public static void main(String[] args) {
String google = "/Users/hss/Documents/PXL_20240918_013738178.MP.jpg";
boolean isMotionImage = isMotionImage(xiaomi, true);
boolean is2 = isMotionImage(google, true);

Map<String, Object> metadata = metadata(xiaomi);
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
System.out.println(gson.toJson(metadata));

}

public static Map<String,Object> metadata(String fileOrUriPath){
boolean isMotion = isMotionImage(fileOrUriPath,true);
Map<String, Object> map = new TreeMap<>();
map.put("0-path",fileOrUriPath);
if(fileOrUriPath !=null && !fileOrUriPath.equals("")){
File file = new File(fileOrUriPath);
map.put("0-length",file.length());
map.put("0-lastModified",file.lastModified());
}

Map<String, Object> stringObjectMap = motion.metaOfImage(fileOrUriPath);
map.put("image",stringObjectMap);
if(isMotion){
String path = getMotionVideoPath(fileOrUriPath);
Map<String, Object> stringObjectMap1 = motion.metaOfVideo(path);
map.put("video",stringObjectMap1);
}
return map;

}


Expand Down

0 comments on commit 223d4c0

Please sign in to comment.