From 1bede89a8c9b9b3f61ac2b4da3e01f17d1038959 Mon Sep 17 00:00:00 2001 From: BlueMatthew Date: Mon, 5 Feb 2024 14:48:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90=E7=9F=AD=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/cpp/PhoneDevice.cpp | 5 +- .../com/xypower/mpapp/MicroPhotoService.java | 3 +- .../xypower/mpapp/video/VideoActivity.java | 26 ++--------- .../xypower/mpapp/video/VideoFragment.java | 46 +++++++++++++------ 4 files changed, 42 insertions(+), 38 deletions(-) diff --git a/app/src/main/cpp/PhoneDevice.cpp b/app/src/main/cpp/PhoneDevice.cpp index 5154c396..c2ffe5e4 100644 --- a/app/src/main/cpp/PhoneDevice.cpp +++ b/app/src/main/cpp/PhoneDevice.cpp @@ -186,7 +186,7 @@ CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPa mRegisterHeartbeatMid = env->GetMethodID(classService, "registerHeartbeatTimer", "(I)V"); mUpdateTimeMid = env->GetMethodID(classService, "updateTime", "(J)Z"); mUpdateCaptureScheduleMid = env->GetMethodID(classService, "updateCaptureSchedule", "(J)Z"); - mStartRecordingMid = env->GetMethodID(classService, "startRecording", "(IJIIII)V"); + mStartRecordingMid = env->GetMethodID(classService, "startRecording", "(IJIIIII)V"); mRequestWakelockMid = env->GetMethodID(classService, "requestWakelock", "(Ljava/lang/String;J)V"); mReleaseWakelockMid = env->GetMethodID(classService, "releaseWakelock", "(Ljava/lang/String;)V"); @@ -848,7 +848,8 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< ALOGE("Failed to get JNI Env"); return false; } - env->CallVoidMethod(m_javaService, mStartRecordingMid, mPhotoInfo.cameraId, (unsigned long)mPhotoInfo.photoId, mPhotoInfo.duration, mPhotoInfo.width, mPhotoInfo.height, mPhotoInfo.duration); + int orientation = mPhotoInfo.orientation == 0 ? -1 : (mPhotoInfo.orientation - 1) * 90; + env->CallVoidMethod(m_javaService, mStartRecordingMid, mPhotoInfo.cameraId, (unsigned long)mPhotoInfo.photoId, mPhotoInfo.duration, mPhotoInfo.width, mPhotoInfo.height, mPhotoInfo.duration, orientation); if (didAttachThread) { diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 43a034f2..d8c222e5 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -476,7 +476,7 @@ public class MicroPhotoService extends Service { registerPhotoTimer(getApplicationContext(), scheduleTime, scheduleTime, timeout, schedules); } - public void startRecording(int cameraId, long videoId, int duration, int width, int height, int quality) { + public void startRecording(int cameraId, long videoId, int duration, int width, int height, int quality, int orientation) { Context context = getApplicationContext(); Intent intent = new Intent(this, VideoActivity.class); @@ -486,6 +486,7 @@ public class MicroPhotoService extends Service { intent.putExtra("width", width); intent.putExtra("height", height); intent.putExtra("quality", quality); + intent.putExtra("orientation", orientation); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); diff --git a/app/src/main/java/com/xypower/mpapp/video/VideoActivity.java b/app/src/main/java/com/xypower/mpapp/video/VideoActivity.java index 19139346..0739e8e0 100644 --- a/app/src/main/java/com/xypower/mpapp/video/VideoActivity.java +++ b/app/src/main/java/com/xypower/mpapp/video/VideoActivity.java @@ -16,28 +16,12 @@ public class VideoActivity extends AppCompatActivity { setContentView(R.layout.activity_video); if (null == savedInstanceState) { - Bundle bundle = new Bundle(); - Intent intent = getIntent(); - - int duration = intent.getIntExtra("duration", 0); - if (intent.hasExtra("cameraId")) { - bundle.putInt("cameraId", intent.getIntExtra("cameraId", 0)); - } - if (intent.hasExtra("videoId")) { - bundle.putLong("videoId", intent.getLongExtra("videoId", 0)); - } - - bundle.putInt("duration", duration); - String act = intent.getStringExtra("action"); - if (act != null) { - bundle.putString("action", act); - } - - bundle.putInt("width", intent.getIntExtra("width", 0)); - bundle.putInt("height", intent.getIntExtra("height", 0)); - + Intent intent = getIntent(); Fragment fragment = VideoFragment.newInstance(); - fragment.setArguments(bundle); + Bundle bundle = intent.getExtras(); + if (bundle != null) { + fragment.setArguments(bundle); + } getSupportFragmentManager().beginTransaction() .replace(R.id.container, fragment) .commit(); diff --git a/app/src/main/java/com/xypower/mpapp/video/VideoFragment.java b/app/src/main/java/com/xypower/mpapp/video/VideoFragment.java index 0f62297b..5331c3a6 100644 --- a/app/src/main/java/com/xypower/mpapp/video/VideoFragment.java +++ b/app/src/main/java/com/xypower/mpapp/video/VideoFragment.java @@ -161,6 +161,8 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med */ private Size mVideoSize; + private int mOrientation = -1; + /** * MediaRecorder */ @@ -280,12 +282,21 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med * @param choices The list of available sizes * @return The video size */ - private static Size chooseVideoSize(Size[] choices) { - for (Size size : choices) { - if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) { - return size; + private static Size chooseVideoSize(Size[] choices, Size expectedSize) { + if (expectedSize == null || expectedSize.getWidth() <= 0 || expectedSize.getHeight() <= 0) { + for (Size size : choices) { + if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) { + return size; + } + } + } else { + for (Size size : choices) { + if (size.equals(expectedSize)) { + return size; + } } } + Log.e(TAG, "Couldn't find any suitable video size"); return choices[choices.length - 1]; } @@ -342,6 +353,12 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med mCameraId = Integer.toString(argument.getInt("CameraId", 0)); mVideoId = argument.getLong("videoId", 0); mDuration = argument.getInt("duration", 0); + int width = argument.getInt("width", 0); + int height = argument.getInt("height", 0); + mOrientation = argument.getInt("orientation", -1); + if (width > 0 && height > 0) { + mVideoSize = new Size(width, height); + } } Log.i(TAG, "Recv recording request CameraId=" + mCameraId + " videoId=" + Long.toString(mVideoId)); @@ -484,7 +501,7 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med if (map == null) { throw new RuntimeException("Cannot get available preview/video sizes"); } - mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class)); + mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class), mVideoSize); mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), width, height, mVideoSize); @@ -627,19 +644,14 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med if (null == activity) { return; } - try { - // mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); - mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); - } catch (Exception ex) { - - } + mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) { mNextVideoAbsolutePath = getVideoFilePath(getActivity()); } mMediaRecorder.setOutputFile(mNextVideoAbsolutePath); - mMediaRecorder.setVideoEncodingBitRate(10000000); + mMediaRecorder.setVideoEncodingBitRate(1024 * 1024 * 8); mMediaRecorder.setVideoFrameRate(30); mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight()); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); @@ -647,10 +659,16 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med if (mDuration > 0) { mMediaRecorder.setMaxDuration(mDuration * 1000); } - int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); + int orientationAdjustment = 0; + if (mOrientation != -1) { + orientationAdjustment = mOrientation / 90; + } + int rotation = (activity.getWindowManager().getDefaultDisplay().getRotation() + orientationAdjustment) % 4; + int orientationHint = 0; switch (mSensorOrientation) { case SENSOR_ORIENTATION_DEFAULT_DEGREES: - mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation)); + orientationHint = DEFAULT_ORIENTATIONS.get(rotation); + mMediaRecorder.setOrientationHint(orientationHint); break; case SENSOR_ORIENTATION_INVERSE_DEGREES: mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));