From 50dbd18645f49702575b84ab65f2e171e0c0d467 Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 3 Jul 2024 09:45:27 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E6=96=B9=E5=BC=8F=E6=8A=8A=E6=8B=8D=E7=85=A7?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E4=BC=A0=E5=85=A5=E5=88=B0JNI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/cpp/MicroPhoto.cpp | 33 ++++------ .../com/xypower/mpapp/BridgeActivity.java | 63 +++++++++++++++++-- .../com/xypower/mpapp/MicroPhotoService.java | 2 +- .../java/com/xypower/common/FileUtils.java | 25 ++++++++ 4 files changed, 97 insertions(+), 26 deletions(-) diff --git a/app/src/main/cpp/MicroPhoto.cpp b/app/src/main/cpp/MicroPhoto.cpp index 22fc3043..a62404d0 100644 --- a/app/src/main/cpp/MicroPhoto.cpp +++ b/app/src/main/cpp/MicroPhoto.cpp @@ -373,8 +373,7 @@ Java_com_xypower_mpapp_MicroPhotoService_notifyToTakePhoto( extern "C" JNIEXPORT jlong JNICALL Java_com_xypower_mpapp_MicroPhotoService_takePhoto( JNIEnv* env, - jclass cls, jint channel, jint preset, jint cameraId, jboolean usb, jstring path, - jstring leftTopOsd, jstring rightTopOsd, jstring rightBottomOsd, jstring leftBottomOsd) { + jclass cls, jint channel, jint preset, jboolean photoOrVideo, jstring configFilePath, jstring path) { if (channel < 1 || channel > 0xFF) { @@ -384,39 +383,31 @@ Java_com_xypower_mpapp_MicroPhotoService_takePhoto( JavaVM* vm = NULL; jint ret = env->GetJavaVM(&vm); + std::string configFilePathStr = jstring2string(env, configFilePath); + CFG_CHANNEL cfg; + IDevice::PHOTO_INFO photoInfo(channel, preset); + CTerminal::LoadChannelConfig(channel, configFilePathStr, cfg); + CTerminal::ConvertChannelConfigToPhotoInfo(cfg, photoOrVideo != JNI_FALSE, photoInfo); + CPhoneDevice* device = new CPhoneDevice(vm, NULL, "", NETID_UNSET, 0); // device->SetListener(pTerminal); - if (usb == JNI_TRUE) + if (photoInfo.usbCamera) { device->TurnOnOtg(NULL); } device->TurnOnCameraPower(NULL); - int32_t width = 1920; - int32_t height = 1080; - NdkCamera::CAMERA_PARAMS params = { 0 }; - - NdkCamera camera(0, 0, params); - int res = camera.selfTest(std::to_string(cameraId), width, height); - - // const IDevice::PHOTO_INFO& photoInfo, const vector& osds, const std::string& path - IDevice::PHOTO_INFO photoInfo(channel, preset); - photoInfo.usbCamera = (usb == JNI_TRUE) ? 1 : 0; - photoInfo.width = width; - photoInfo.height = height; - photoInfo.cameraId = cameraId; - std::vector osds; osds.resize(4); osds[1].alignment = IDevice::OSD_ALIGNMENT_TOP_RIGHT; osds[2].alignment = IDevice::OSD_ALIGNMENT_BOTTOM_RIGHT; osds[3].alignment = IDevice::OSD_ALIGNMENT_BOTTOM_LEFT; - osds[0].text = jstring2string(env, leftTopOsd); - osds[1].text = jstring2string(env, rightTopOsd); - osds[2].text = jstring2string(env, rightBottomOsd); - osds[3].text = jstring2string(env, leftBottomOsd); + osds[0].text = cfg.osd.leftTop; + osds[1].text = cfg.osd.rightTop; + osds[2].text = cfg.osd.rightBottom; + osds[3].text = cfg.osd.leftBottom); const char* pathStr = env->GetStringUTFChars(path, 0); diff --git a/app/src/main/java/com/xypower/mpapp/BridgeActivity.java b/app/src/main/java/com/xypower/mpapp/BridgeActivity.java index 27073bee..fa1941f8 100644 --- a/app/src/main/java/com/xypower/mpapp/BridgeActivity.java +++ b/app/src/main/java/com/xypower/mpapp/BridgeActivity.java @@ -1,19 +1,27 @@ package com.xypower.mpapp; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; + import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.text.TextUtils; import android.util.Base64; import com.xypower.common.FileUtils; +import com.xypower.common.JSONUtils; +import com.xypower.common.MicroPhotoContext; import com.xypower.mpapp.v2.Camera2VideoActivity; +import org.json.JSONObject; + import java.io.File; -import java.nio.charset.StandardCharsets; + +import java.nio.file.Files; public class BridgeActivity extends AppCompatActivity { @@ -92,14 +100,58 @@ public class BridgeActivity extends AppCompatActivity { String path = intent.getStringExtra("path"); int channel = intent.getIntExtra("channel", 1); int preset = intent.getIntExtra("preset", 0xFF); - boolean usb = intent.getBooleanExtra("usb", false); - int cameraId = intent.getIntExtra("cameraId", -1); + + int width = intent.getIntExtra("width", 0); + int height = intent.getIntExtra("height", 0); String leftTopOsd = intent.getStringExtra("leftTopOsd"); String rightTopOsd = intent.getStringExtra("rightTopOsd"); String rightBottomOsd = intent.getStringExtra("rightBottomOsd"); String leftBottomOsd = intent.getStringExtra("leftBottomOsd"); + String appPath = MicroPhotoContext.buildMpAppDir(getApplicationContext()); + File configFile = new File(appPath); + configFile = new File(configFile, "data/channels/" + Integer.toString(channel) + ".json"); + + File tmpConfigFile = new File(appPath); + tmpConfigFile = new File(tmpConfigFile, "tmp/" + Integer.toString(channel) + "-" + Long.toString(System.currentTimeMillis()) + ".json"); + + if (configFile.exists()) { + try { + + FileUtils.copyFile(configFile, tmpConfigFile); + + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + JSONObject configJson = JSONUtils.loadJson(tmpConfigFile.getAbsolutePath()); + try { + if (configJson == null) { + configJson = new JSONObject(); + } + if (width > 0) { + configJson.put("resolutionCX", width); + } + if (height > 0) { + configJson.put("resolutionCY", height); + } + + JSONObject osdJson = configJson.getJSONObject("osd"); + if (osdJson == null) { + osdJson = configJson.put("osd", new JSONObject()); + } + osdJson.put("leftTop", TextUtils.isEmpty(leftTopOsd) ? "" : leftTopOsd); + osdJson.put("rightTop", TextUtils.isEmpty(rightTopOsd) ? "" : rightTopOsd); + osdJson.put("rightBottom", TextUtils.isEmpty(rightBottomOsd) ? "" : rightBottomOsd); + osdJson.put("leftBottom", TextUtils.isEmpty(leftBottomOsd) ? "" : leftBottomOsd); + + JSONUtils.saveJson(tmpConfigFile.getAbsolutePath(), configJson); + } catch (Exception ex) { + ex.printStackTrace(); + } + File file = new File(path); if (file.exists()) { file.delete(); @@ -107,7 +159,10 @@ public class BridgeActivity extends AppCompatActivity { FileUtils.ensureParentDirectoryExisted(path); } - MicroPhotoService.takePhoto(channel, preset, cameraId, usb, path, leftTopOsd, rightTopOsd, rightBottomOsd, leftBottomOsd); + MicroPhotoService.takePhoto(channel, preset, true, tmpConfigFile.getAbsolutePath(), path); + if (tmpConfigFile.exists()) { + tmpConfigFile.delete(); + } } else if (TextUtils.equals(action, ACTION_RECORDING)) { String path = intent.getStringExtra("path"); diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 3d8dc896..b1adeb18 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -1161,7 +1161,7 @@ cellSignalStrengthGsm.getDbm(); protected native void updatePosition(long handler, double lon, double lat, double radius, long ts); protected native boolean uninit(long handler); protected native void recordingFinished(long handler, boolean result, String path, long videoId); - public static native long takePhoto(int channel, int preset, int cameraId, boolean usb, String path, String leftTopOsd, String rightTopOsd, String rightBottomOsd, String leftBottomOsd); + public static native long takePhoto(int channel, int preset, boolean photoOrVideo, String configFilePath, String path); public static native void releaseDeviceHandle(long deviceHandle); public static native void infoLog(String log); public static native void setOtgState(boolean enabled); diff --git a/common/src/main/java/com/xypower/common/FileUtils.java b/common/src/main/java/com/xypower/common/FileUtils.java index 0456b01d..81757fdc 100644 --- a/common/src/main/java/com/xypower/common/FileUtils.java +++ b/common/src/main/java/com/xypower/common/FileUtils.java @@ -8,7 +8,10 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Date; @@ -18,6 +21,28 @@ public class FileUtils { + public static void copyFile(File source, File dest) throws IOException { + InputStream input = null; + OutputStream output = null; + try { + input = new FileInputStream(source); + output = new FileOutputStream(dest); + byte[] buf = new byte[1024]; + int bytesRead; + while ((bytesRead = input.read(buf)) > 0) { + output.write(buf, 0, bytesRead); + } + } finally { + try { + if (input != null) input.close(); + } catch (Exception ex) { + } + try { + if (output != null) output.close(); + } catch (Exception ex) { + } + } + } public static boolean DeleteFilesInPath(String path, long seconds) { File pathFile = new File(path);