优化代码

serial
BlueMatthew 1 year ago
parent 1c4e44850d
commit d5d1345542

@ -92,7 +92,6 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved)
google_breakpad::ExceptionHandler eh(descriptor, NULL, DumpCallback, NULL, true, -1);
#endif
const char* className = "com/xypower/mpapp/MicroPhotoService";
jclass clazz = (env)->FindClass(className);
@ -127,33 +126,11 @@ bool GetJniEnv(JavaVM *vm, JNIEnv **env, bool& didAttachThread)
return get_env_result == JNI_OK;
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_xypower_mpapp_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
extern "C" JNIEXPORT jboolean JNICALL
Java_com_xypower_mpapp_MainActivity_takePhoto(
JNIEnv* env,
jobject pThis, jint channel, jint preset, jstring path, jstring fileName) {
// ANativeWindow *imgReaderAnw = ANativeWindow_fromSurface(env, surface);
/*
CCamera camera;
camera.initCamera(imgReaderAnw);
if (camera.isCameraReady())
{
camera.takePicture();
}
camera.closeCamera();
*/
if (channel < 1 || channel > 0xFF)
{
return JNI_FALSE;
@ -186,9 +163,11 @@ Java_com_xypower_mpapp_MicroPhotoService_init(
*/
net_handle_t nh = (net_handle_t)netHandle;
if (netHandle != NETID_UNSET) {
net_handle_t nh = (net_handle_t)netHandle;
android_setprocnetwork(nh);
android_setprocnetwork(nh);
}
char model[PROP_VALUE_MAX] = { 0 };
__system_property_get("ro.product.model", model);

@ -1,22 +1,5 @@
#include "TerminalDevice.h"
/*
* Copyright 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#define LOG_TAG "CameraTestHelpers"
#include <AndroidHelper.h>
#include "PhoneDevice.h"
#include <Client/Terminal.h>
@ -28,8 +11,7 @@
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
// #include <opencv2/objdetect.hpp>
// #include <opencv2/features2d.hpp>
#include <opencv2/core/types.hpp>
#include <opencv2/core/core.hpp>
@ -166,7 +148,7 @@ void CPhoneDevice::CPhoneCamera::on_error(const std::string& msg)
}
}
CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPath, unsigned int netId)
CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPath, unsigned int netId) : mCameraPowerCount(0), mOtgCount(0)
{
mCamera = NULL;
m_listener = NULL;
@ -687,20 +669,9 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector<
if (photoInfo.usbCamera)
{
jboolean otgOn = JNI_TRUE;
if(m_sysApiClass != NULL)
{
XYLOG(XYLOG_SEVERITY_INFO, "Turn on OTG for USB Camera");
env->CallStaticVoidMethod(m_sysApiClass, mTurnOtgMid, otgOn);
}
}
if(m_sysApiClass != NULL)
{
XYLOG(XYLOG_SEVERITY_INFO, "Set Cam3V3 Enabled ");
env->CallStaticVoidMethod(m_sysApiClass, mSetCam3V3EnableMid, JNI_TRUE);
TurnOnOtg(env);
}
TurnOnCameraPower(env);
if (didAttachThread)
{
@ -751,22 +722,16 @@ void CPhoneDevice::CloseCamera2(CPhoneDevice::CPhoneCamera* camera, bool turnOff
return;
}
if(m_sysApiClass != NULL)
if (turnOffOtg)
{
if (turnOffOtg)
{
XYLOG(XYLOG_SEVERITY_INFO, "Turn Off Otg");
env->CallStaticVoidMethod(m_sysApiClass, mTurnOtgMid, JNI_FALSE);
}
XYLOG(XYLOG_SEVERITY_INFO, "Set Cam3V3 Disabled ");
env->CallStaticVoidMethod(m_sysApiClass, mSetCam3V3EnableMid, JNI_FALSE);
TurnOffOtg(env);
}
TurnOffCameraPower(env);
if (didAttachThread)
{
m_vm->DetachCurrentThread();
}
}
void visualize(const char* filename, const ncnn::Mat& m)
@ -1008,4 +973,78 @@ void CPhoneDevice::UpdatePosition(double lon, double lat, time_t ts)
{
return m_listener->OnPositionDataArrived(lon, lat, ts);
}
}
void CPhoneDevice::TurnOnCameraPower(JNIEnv* env)
{
mCameraPowerLocker.lock();
if (mCameraPowerCount == 0)
{
if(m_sysApiClass != NULL)
{
#ifdef _DEBUG
XYLOG(XYLOG_SEVERITY_INFO, "Set Cam3V3 Enabled ");
#endif
env->CallStaticVoidMethod(m_sysApiClass, mSetCam3V3EnableMid, JNI_TRUE);
}
}
mCameraPowerCount++;
mCameraPowerLocker.unlock();
}
void CPhoneDevice::TurnOffCameraPower(JNIEnv* env)
{
mCameraPowerLocker.lock();
if (mCameraPowerCount > 0)
{
mCameraPowerCount--;
if (mCameraPowerCount == 0)
{
if(m_sysApiClass != NULL)
{
#ifdef _DEBUG
XYLOG(XYLOG_SEVERITY_INFO, "Set Cam3V3 Disabled ");
#endif
env->CallStaticVoidMethod(m_sysApiClass, mSetCam3V3EnableMid, JNI_FALSE);
}
}
}
mCameraPowerLocker.unlock();
}
void CPhoneDevice::TurnOnOtg(JNIEnv* env)
{
mCameraPowerLocker.lock();
if (mOtgCount == 0)
{
if(m_sysApiClass != NULL)
{
#ifdef _DEBUG
XYLOG(XYLOG_SEVERITY_INFO, "Turn On Otg");
#endif
env->CallStaticVoidMethod(m_sysApiClass, mTurnOtgMid, JNI_TRUE);
}
}
mOtgCount++;
mCameraPowerLocker.unlock();
}
void CPhoneDevice::TurnOffOtg(JNIEnv* env)
{
mCameraPowerLocker.lock();
if (mOtgCount > 0)
{
mOtgCount--;
if (mOtgCount == 0)
{
if(m_sysApiClass != NULL)
{
#ifdef _DEBUG
XYLOG(XYLOG_SEVERITY_INFO, "Turn Off Otg");
#endif
env->CallStaticVoidMethod(m_sysApiClass, mTurnOtgMid, JNI_FALSE);
}
}
}
mCameraPowerLocker.unlock();
}

@ -223,6 +223,12 @@ protected:
void CloseCamera2(CPhoneCamera* camera, bool turnOffOtg);
void TurnOnCameraPower(JNIEnv* env);
void TurnOffCameraPower(JNIEnv* env);
void TurnOnOtg(JNIEnv* env);
void TurnOffOtg(JNIEnv* env);
protected:
JavaVM* m_vm;
@ -262,6 +268,10 @@ protected:
time_t mHeartbeatStartTime;
unsigned int mHeartbeatDuration;
long mCameraPowerCount;
long mOtgCount;
std::mutex mCameraPowerLocker;
};

@ -33,6 +33,7 @@ import android.os.Bundle;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.text.method.ArrowKeyMovementMethod;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Display;
@ -40,6 +41,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.dev.devapi.api.SysApi;
@ -67,7 +69,6 @@ public class MainActivity extends AppCompatActivity {
}
private ActivityMainBinding binding;
private int defaultDataSubId;
private Handler mHandler = null;
private Messenger mMessenger = null;
@ -117,8 +118,7 @@ public class MainActivity extends AppCompatActivity {
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
// getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
ActionBar actionBar = getSupportActionBar();
@ -131,11 +131,6 @@ public class MainActivity extends AppCompatActivity {
binding.logs.setText("");
binding.logs.setMovementMethod(ScrollingMovementMethod.getInstance());
binding.logs.setScrollbarFadingEnabled(false);
// binding.logs.setMaxLines(16);
// binding.protocol.item
// Context appContext = getApplicationContext();
mHandler = new Handler(Looper.myLooper()) {
@Override
@ -191,11 +186,6 @@ public class MainActivity extends AppCompatActivity {
};
// mMessenger = new Messenger(new Handler());
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display defaultDisplay = windowManager.getDefaultDisplay();
int width = defaultDisplay.getWidth();
int height = defaultDisplay.getHeight();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
@ -208,11 +198,6 @@ public class MainActivity extends AppCompatActivity {
Log.d(TAG, "MainActivity: reboot=" + rebootFlag + " noDelay=" + noDelay);
String cmdid = "";
String server = "";
Integer port = new Integer(6891);
Integer protocol = new Integer(MicroPhotoContext.DEFAULT_PROTOCOL); // 0xFF00
MicroPhotoContext.AppConfig appConfig = getAppConfig();
binding.cmdid.setText(appConfig.cmdid);
binding.server.setText(appConfig.server);
@ -316,14 +301,15 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onClick(View view) {
defaultDataSubId = getDefaultDataSubId();
System.out.println(defaultDataSubId);
// defaultDataSubId = getDefaultDataSubId();
// System.out.println(defaultDataSubId);
}
});
binding.simchange2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*
if (defaultDataSubId == 0) {
setDefaultDataSubId(1);
} else {
@ -333,7 +319,7 @@ public class MainActivity extends AppCompatActivity {
setDefaultDataSubId(1);
}
}
*/
}
});
@ -422,41 +408,11 @@ public class MainActivity extends AppCompatActivity {
binding.video.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//方法1
// mediaRecorder = new MediaRecorder();
//
// //设置视频和音频的来源
// mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// //
// mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// //设置录制视频的编码格式
// mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
// //设置音频的编码格式
// mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
// //设置视频的帧率:每秒切换图片的次数
// mediaRecorder.setVideoFrameRate(20);
// //视频的分辨率
// mediaRecorder.setVideoSize(176, 144);
//
//// mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
//
// mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/111.mp4");
// try {
// mediaRecorder.prepare();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// mediaRecorder.start();
}
});
binding.video2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// mediaRecorder.stop();
}
});
@ -636,109 +592,6 @@ public class MainActivity extends AppCompatActivity {
}
String buildPhotoDir(int channel) {
File path = new File(Environment.getExternalStorageDirectory(), "com.xinyingpower.com/photos/");
if (!path.exists() && !path.mkdirs()) {
return null;
}
String p = path.getAbsolutePath();
if (!p.endsWith(File.separator)) {
p += File.separator;
}
return p;
}
String buildPhotoFileName(int channel, int preset) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "img_" + Integer.toString(channel) + "_" + Integer.toHexString(preset).toUpperCase() + "_" + date + ".jpg";
return photoFile;
}
private void takePhoto(int aa) {
System.out.println("Preparing to take photo");
Camera camera = null;
int cameraCount = 0;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
SystemClock.sleep(1000);
Camera.getCameraInfo(camIdx, cameraInfo);
try {
camera = Camera.open(camIdx);
} catch (RuntimeException e) {
System.out.println("Camera not available: " + camIdx);
camera = null;
//e.printStackTrace();
}
try {
if (null == camera) {
System.out.println("Could not get camera instance");
} else {
System.out.println("Got the camera, creating the dummy surface texture");
//SurfaceTexture dummySurfaceTextureF = new SurfaceTexture(0);
try {
//camera.setPreviewTexture(dummySurfaceTextureF);
camera.setPreviewTexture(new SurfaceTexture(0));
camera.startPreview();
} catch (Exception e) {
System.out.println("Could not set the surface preview texture");
e.printStackTrace();
}
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File path = getApplicationContext().getFilesDir();
// String appPath = path.getAbsolutePath();
File pictureFileDir = path;
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "PictureFront_" + "_" + date + ".jpg";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
File mainPicture = new File(filename);
// addImageFile(mainPicture);
try {
FileOutputStream fos = new FileOutputStream(mainPicture);
fos.write(data);
fos.close();
System.out.println("image saved");
} catch (Exception error) {
System.out.println("Image could not be saved");
}
camera.release();
}
});
}
} catch (Exception e) {
camera.release();
}
}
}
/**
* A native method that is implemented by the 'microphoto' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native boolean takePhoto(int channel, int preset, String path, String fileName);

@ -348,6 +348,7 @@
android:background="@drawable/textview_border"
android:orientation="horizontal"
android:padding="4dp"
android:lineSpacingMultiplier="1.25"
android:scrollbars="vertical"
android:singleLine="false"
android:text="Logs"

@ -324,14 +324,15 @@
android:layout_height="match_parent"
android:layout_margin="4dp"
android:orientation="horizontal"
android:padding="10dp"
android:padding="4dp"
android:lineSpacingMultiplier="1.25"
android:scrollbars="vertical"
android:text="Test\n\n\nTest"
android:text="Logs"
android:textColor="@color/black"
android:singleLine="false"
android:textIsSelectable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/takePhotoBtn4"
app:layout_constraintTop_toTopOf="parent" />

Loading…
Cancel
Save