You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MpPreview/app/src/main/cpp/MpPreview.cpp

191 lines
6.0 KiB
C++

6 months ago
#include <jni.h>
#include <string>
#include <vector>
// #include "ncnn/yolov5ncnn.h"
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
inline std::string jstring2string(JNIEnv *env, jstring jStr)
{
if (!jStr)
return "";
const jclass stringClass = env->GetObjectClass(jStr);
const jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
const jbyteArray stringJbytes = (jbyteArray) env->CallObjectMethod(jStr, getBytes, env->NewStringUTF("UTF-8"));
size_t length = (size_t) env->GetArrayLength(stringJbytes);
jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL);
std::string ret = std::string((char *)pBytes, length);
env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);
env->DeleteLocalRef(stringJbytes);
env->DeleteLocalRef(stringClass);
return ret;
}
bool makeHdr(std::vector<float>& times, std::vector<std::string>& paths, cv::Mat& rgb)
{
// Read images and exposure times
std::vector<cv::Mat> images;
for (auto it = paths.cbegin(); it != paths.cend(); ++it)
{
cv::Mat im = cv::imread((*it).c_str());
images.push_back(im);
}
// Align input images
// cout << "Aligning images ... " << endl;
cv::Ptr<cv::AlignMTB> alignMTB = cv::createAlignMTB();
#if 0
alignMTB->process(images, images);
#endif
// Obtain Camera Response Function (CRF)
// cout << "Calculating Camera Response Function (CRF) ... " << endl;
cv::Mat responseDebevec;
cv::Ptr<cv::CalibrateDebevec> calibrateDebevec = cv::createCalibrateDebevec();
calibrateDebevec->process(images, responseDebevec, times);
// Merge images into an HDR linear image
// cout << "Merging images into one HDR image ... ";
cv::Mat hdrDebevec;
cv::Ptr<cv::MergeDebevec> mergeDebevec = cv::createMergeDebevec();
mergeDebevec->process(images, hdrDebevec, times, responseDebevec);
// Save HDR image.
// imwrite((OUTPUT_DIR "hdrDebevec.hdr"), hdrDebevec);
// cout << "saved hdrDebevec.hdr " << endl;
{
std::vector<cv::Mat> empty;
empty.swap(images);
}
// Tonemap using Reinhard's method to obtain 24-bit color image
// cout << "Tonemaping using Reinhard's method ... ";
cv::Mat ldrReinhard;
cv::Ptr<cv::TonemapReinhard> tonemapReinhard = cv::createTonemapReinhard(1.5, 0, 0, 0);
tonemapReinhard->process(hdrDebevec, ldrReinhard);
hdrDebevec.release();
int type = ldrReinhard.type();
ldrReinhard = ldrReinhard * 255;
ldrReinhard.convertTo(rgb, CV_8U);
ldrReinhard.release();
return true;
}
extern "C"
JNIEXPORT jboolean JNICALL
6 months ago
Java_com_xypower_mppreview_Camera2RawFragment_makeHdr(
6 months ago
JNIEnv *env, jobject thiz, jlong exposureTime1, jstring path1, jlong exposureTime2,
jstring path2, jstring outputPath) {
std::vector<float> times;
std::vector<std::string> paths;
times.push_back((float)(exposureTime1 / 1000) / 1000000.0);
times.push_back((float)(exposureTime2 / 1000) / 1000000.0);
6 months ago
paths.push_back(jstring2string(env, path1));
paths.push_back(jstring2string(env, path2));
6 months ago
cv::Mat rgb;
if (makeHdr(times, paths, rgb))
{
6 months ago
std::string fileName = jstring2string(env, outputPath);
6 months ago
if (cv::imwrite(fileName.c_str(), rgb))
{
return JNI_TRUE;
}
}
return JNI_FALSE;
}
extern "C"
JNIEXPORT jboolean JNICALL
6 months ago
Java_com_xypower_mppreview_Camera2RawFragment_makeHdr2(
6 months ago
JNIEnv *env, jobject thiz, jlong exposureTime1, jstring path1, jlong exposureTime2,
jstring path2, jlong exposureTime3, jstring path3, jstring outputPath) {
std::vector<float> times;
std::vector<std::string> paths;
times.push_back((float)(exposureTime1 / 1000) / 1000000.0);
times.push_back((float)(exposureTime2 / 1000) / 1000000.0);
times.push_back((float)(exposureTime3 / 1000) / 1000000.0);
paths.push_back(jstring2string(env, path1));
paths.push_back(jstring2string(env, path2));
paths.push_back(jstring2string(env, path3));
cv::Mat rgb;
if (makeHdr(times, paths, rgb))
{
std::string fileName = jstring2string(env, outputPath);
if (cv::imwrite(fileName.c_str(), rgb))
{
return JNI_TRUE;
}
}
6 months ago
6 months ago
return JNI_FALSE;
}
//extern "C"
//JNIEXPORT void JNICALL
//Java_com_xypower_mppreview_Camera2RawFragment_makeHdr3(JNIEnv *env,
// jobject thiz,
// jlong img1_obj,
// jlong img2_obj,
// jlong img3_obj,
// jfloat time1,
// jfloat time2,
// jfloat time3,
// jlong hdrImg) {
// cv::Mat &mImg1 = *(cv::Mat *) img1_obj;
// cv::Mat &mImg2 = *(cv::Mat *) img2_obj;
// cv::Mat &mImg3 = *(cv::Mat *) img3_obj;
//
// vector<cv::Mat> images;
// vector<float> times;
// images.push_back(mImg1);
// images.push_back(mImg2);
// images.push_back(mImg3);
//
// //曝光时间列表
// static const float timesArray[] = { time1,time2,time3};
// times.assign(timesArray, timesArray + 3);
//
// //利用中值阈值图MTB进行对齐
// /*Ptr<AlignMTB> alignMTB = createAlignMTB();
// alignMTB->process(images, images);
//
// //恢复相机响应函数
// Mat responce;
// Ptr<CalibrateDebevec> calibratedebevec = createCalibrateDebevec();
// calibratedebevec->process(images, responce, times);*/
//
// //融合
// cv::Mat hdrMat;
// Ptr<MergeDebevec> mergedebevec = createMergeDebevec();
// mergedebevec->process(images, hdrMat, times);
//
// cv::Mat sdr;
// float gamma = 1.0f;
// Ptr<Tonemap> tonemap = createTonemap(gamma);
// tonemap->process(hdrMat, hdrMat);
//
// hdrMat = hdrMat * 255;
// hdrMat.convertTo(*(Mat *)hdrImg, CV_8UC3);
//
// //hdrImg = cv::normalize(images, None, 0, 255, cv::NORM_MINMAX, cv::CV_8UC3)
//
// /*Mat fusion;
// Ptr<MergeMertens> merge_mertens = createMergeMertens();
// merge_mertens->process(images, *(Mat *)hdrImg);*/
//}