优化系统相机拍照

nx2024TEMP
Matthew 9 months ago
parent 0dbb47800c
commit 61841e9d6f

@ -346,8 +346,8 @@ public class MicroPhotoService extends Service {
cameraAdb.setCallback(new Runnable() { cameraAdb.setCallback(new Runnable() {
@Override @Override
public void run() { public void run() {
String targetPath = cameraAdb.getTargetPath(); List<String> targetPaths = cameraAdb.getTargetPaths();
if (!TextUtils.isEmpty(targetPath)) { for (String targetPath : targetPaths) {
mService.sendExternalPhoto(mService.mNativeHandle, targetPath); mService.sendExternalPhoto(mService.mNativeHandle, targetPath);
} }
} }

@ -13,7 +13,9 @@ import com.xypower.mpapp.v2.Camera2VideoActivity;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import dadb.AdbKeyPair; import dadb.AdbKeyPair;
@ -27,11 +29,11 @@ public class CameraAdb {
private String mAppPath; private String mAppPath;
private AdbKeyPair mAdbKeyPair; private AdbKeyPair mAdbKeyPair;
private String mDeviceIp = "127.0.0.1"; private String mDeviceIp = "127.0.0.1";
private String mTargetPath; private List<String> mTargetPaths = new ArrayList<>();
private Runnable mRunnable; private Runnable mRunnable;
public String getTargetPath() { public List<String> getTargetPaths() {
return mTargetPath; return mTargetPaths;
} }
public void setCallback(Runnable runnable) { public void setCallback(Runnable runnable) {
@ -91,22 +93,28 @@ public class CameraAdb {
takePhoto(false); takePhoto(false);
long takingTime = System.currentTimeMillis() / 1000; long takingTime = System.currentTimeMillis() / 1000;
sleep(1500); sleep(1500);
movePhoto(false, requestTime, takingTime); String path = movePhoto(false, requestTime, takingTime);
if (!TextUtils.isEmpty(path)) {
mTargetPaths.add(path);
}
sleep(100); sleep(100);
SysApi.forceStopApp(mContext, "com.mediatek.camera"); SysApi.forceStopApp(mContext, "com.mediatek.camera");
sleep(200); sleep(1000);
requestTime = System.currentTimeMillis() / 1000; requestTime = System.currentTimeMillis() / 1000;
takePhoto(true); takePhoto(true);
takingTime = System.currentTimeMillis() / 1000; takingTime = System.currentTimeMillis() / 1000;
sleep(250);
String path = movePhoto(true, requestTime, takingTime);
sleep(200); sleep(200);
SysApi.forceStopApp(mContext, "com.mediatek.camera"); SysApi.forceStopApp(mContext, "com.mediatek.camera");
sleep(250);
path = movePhoto(true, requestTime, takingTime);
if (!TextUtils.isEmpty(path)) {
mTargetPaths.add(path);
}
if (mRunnable != null) { if (mRunnable != null) {
mRunnable.run(); mRunnable.run();
} }
@ -117,33 +125,36 @@ public class CameraAdb {
String targetPath = null; String targetPath = null;
String photoPath = mAppPath + "photos/"; String photoPath = mAppPath + "photos/";
String photoFile = "IMG_" + Long.toHexString(requestTime).toUpperCase() + "_" String photoFile = "IMG_" + Long.toHexString(requestTime).toUpperCase() + "_"
+ (frontCamera ? "2" : "1") + "_FF_0_" + Long.toHexString(requestTime).toUpperCase() + (frontCamera ? "2" : "1") + "_FE_0_" + Long.toHexString(requestTime).toUpperCase()
+ "_" + Long.toHexString(takingTime).toUpperCase() + ".jpg"; + "_" + Long.toHexString(takingTime).toUpperCase() + ".jpg";
File cameraPath = new File("/sdcard/DCIM/Camera/"); File cameraPath = new File("/sdcard/DCIM/Camera/");
Optional<File> opFile = Arrays.stream(cameraPath.listFiles(File::isFile)) boolean res = false;
for (int idx = 0; idx < 10; idx++) {
Optional<File> opFile = Arrays.stream(cameraPath.listFiles(File::isFile))
.max((f1, f2) -> Long.compare(f1.lastModified(), f2.lastModified())); .max((f1, f2) -> Long.compare(f1.lastModified(), f2.lastModified()));
boolean res = false; if (opFile.isPresent()) {
if (opFile.isPresent()) { File targetFile = new File(new File(photoPath), photoFile);
File targetFile = new File(new File(photoPath), photoFile); try {
try { File srcFile = opFile.get();
File srcFile = opFile.get();
for (int idx = 0; idx < 10; idx++) {
res = srcFile.renameTo(targetFile); res = srcFile.renameTo(targetFile);
if (res) { if (res) {
targetPath = targetFile.getAbsolutePath(); targetPath = targetFile.getAbsolutePath();
break; break;
} }
sleep(200); } catch (Exception e) {
e.printStackTrace();
} }
} catch (Exception e) {
e.printStackTrace();
}
if (!res) {
Log.e(TAG, "Failed to copy photo from Camera");
} }
sleep(200);
}
if (!res) {
Log.e(TAG, "Failed to copy photo from Camera");
} }
return targetPath; return targetPath;
@ -158,7 +169,7 @@ public class CameraAdb {
} }
public void takePhoto(final boolean frontCamera) { public void takePhoto(final boolean frontCamera) {
Dadb adb = Dadb.discover(mDeviceIp, mAdbKeyPair); Dadb adb = Dadb.create(mDeviceIp, 5555, mAdbKeyPair);
if (adb == null) { if (adb == null) {
return; return;

Loading…
Cancel
Save