feat: revert super res

This commit is contained in:
Hinnka committed 2026-02-20 19:28:35 +08:00
1 parent 5172e8af8f
commit 085be19057
17 files changed
+227 -157

No files matched your search

+16 -12
View File
@@ -153,8 +153,9 @@ extern "C" {
*/
JNIEXPORT jlong JNICALL
Java_com_hinnka_mycamera_processor_MultiFrameStacker_createStackerNative(
JNIEnv *env, jobject /* this */, jint width, jint height) {
auto *stacker = new ImageStacker(width, height);
JNIEnv *env, jobject /* this */, jint width, jint height,
jboolean enableSuperRes) {
auto *stacker = new ImageStacker(width, height, enableSuperRes);
return reinterpret_cast<jlong>(stacker);
}
@@ -271,8 +272,9 @@ Java_com_hinnka_mycamera_processor_MultiFrameStacker_releaseStackerNative(
*/
JNIEXPORT jlong JNICALL
Java_com_hinnka_mycamera_processor_MultiFrameStacker_createVulkanStackerNative(
JNIEnv *env, jobject /* this */, jint width, jint height) {
auto *stacker = new VulkanImageStacker(width, height);
JNIEnv *env, jobject /* this */, jint width, jint height,
jboolean enableSuperRes) {
auto *stacker = new VulkanImageStacker(width, height, enableSuperRes);
return reinterpret_cast<jlong>(stacker);
}
@@ -330,8 +332,9 @@ Java_com_hinnka_mycamera_processor_MultiFrameStacker_releaseVulkanStackerNative(
*/
JNIEXPORT jlong JNICALL
Java_com_hinnka_mycamera_processor_MultiFrameStacker_createRawStackerNative(
JNIEnv *env, jobject /* this */, jint width, jint height) {
auto *stacker = new RawStacker(width, height);
JNIEnv *env, jobject /* this */, jint width, jint height,
jboolean enableSuperRes) {
auto *stacker = new RawStacker(width, height, enableSuperRes);
return reinterpret_cast<jlong>(stacker);
}
@@ -410,9 +413,10 @@ Java_com_hinnka_mycamera_processor_MultiFrameStacker_processRawStackWithBufferNa
*/
JNIEXPORT jlong JNICALL
Java_com_hinnka_mycamera_processor_MultiFrameStacker_createVulkanRawStackerNative(
JNIEnv *env, jobject, jint width, jint height, jfloatArray blackLevel,
jint whiteLevel, jfloatArray wbGains, jfloatArray noiseModel,
jfloatArray lensShadingMap, jint lscWidth, jint lscHeight) {
JNIEnv *env, jobject, jint width, jint height, jboolean enableSuperRes,
jfloatArray blackLevel, jint whiteLevel, jfloatArray wbGains,
jfloatArray noiseModel, jfloatArray lensShadingMap, jint lscWidth,
jint lscHeight) {
float bl[4] = {0, 0, 0, 0};
if (blackLevel) {
@@ -440,9 +444,9 @@ Java_com_hinnka_mycamera_processor_MultiFrameStacker_createVulkanRawStackerNativ
lsc = env->GetFloatArrayElements(lensShadingMap, nullptr);
}
auto *stacker =
new VulkanRawStacker(width, height, bl, (float)whiteLevel, wb, noise, lsc,
(uint32_t)lscWidth, (uint32_t)lscHeight);
auto *stacker = new VulkanRawStacker(width, height, enableSuperRes, bl,
(float)whiteLevel, wb, noise, lsc,
(uint32_t)lscWidth, (uint32_t)lscHeight);
if (lsc) {
env->ReleaseFloatArrayElements(lensShadingMap, lsc, 0);
+6 -4
View File
@@ -94,9 +94,11 @@ void main() {
if (outX >= pc.width || outY >= pc.height) return;
// 1. Fetch Kernel Parameters from Base Frame (Structure Tensor)
// Structure Tensor is computed at Half-Res (Plane Resolution)
uint planeX = outX / 2;
uint planeY = outY / 2;
// Structure Tensor is computed at Half-Res (Plane Resolution) of Input
uint nativeX = uint(float(outX) / pc.scale);
uint nativeY = uint(float(outY) / pc.scale);
uint planeX = nativeX / 2;
uint planeY = nativeY / 2;
planeX = clamp(planeX, 0, pc.planeWidth - 1);
planeY = clamp(planeY, 0, pc.planeHeight - 1);
@@ -126,7 +128,7 @@ void main() {
vec2 flowRaw = flow * 2.0; // In raw pixels
// Projected Center in Input Frame
vec2 centerPos = vec2(outX, outY) + flowRaw;
vec2 centerPos = vec2(float(outX) / pc.scale, float(outY) / pc.scale) + flowRaw;
// Window search in Current Frame (Bayer)
// 5x5 Window (radius 2) around the center to ensure multiple
+39 -39
View File
@@ -518,10 +518,10 @@ inline float sampleBicubicPlane(const std::vector<uint16_t> &data, int width,
// --- ImageStacker Implementation ---
ImageStacker::ImageStacker(int width, int height)
ImageStacker::ImageStacker(int width, int height, bool enableSuperRes)
: width(width), height(height), isFirstFrame(true) {
int scale = 1;
// Accumulator buffers are scaled (always 1x now)
scale = enableSuperRes ? 2 : 1;
// Accumulator buffers are scaled
int scaledW = width * scale;
int scaledH = height * scale;
int size = scaledW * scaledH;
@@ -677,15 +677,16 @@ void ImageStacker::processFrame(int index) {
referencePyramid = buildPyramid(staged.y8.data.data(), width, height, 4);
// Initialize accumulators (always 1x now)
int scaledW = width;
int scaledH = height;
// Initialize accumulators by upscaling the first frame
int scaledW = width * scale;
int scaledH = height * scale;
#pragma omp parallel for collapse(2) num_threads(4)
for (int y = 0; y < scaledH; ++y) {
for (int x = 0; x < scaledW; ++x) {
float srcX = (float)x;
float srcY = (float)y;
// Map scaled coordinates back to input coordinates
float srcX = (float)x / scale;
float srcY = (float)y / scale;
uint16_t val =
(uint16_t)sampleBicubic(staged.y, width, height, srcX, srcY);
@@ -703,8 +704,8 @@ void ImageStacker::processFrame(int index) {
#pragma omp parallel for collapse(2) num_threads(4)
for (int y = 0; y < scaledUVHeight; ++y) {
for (int x = 0; x < scaledUVWidth; ++x) {
float srcX = (float)x;
float srcY = (float)y;
float srcX = (float)x / scale;
float srcY = (float)y / scale;
uint16_t uVal = (uint16_t)sampleBicubic(staged.u, uvRefWidth,
uvRefHeight, srcX, srcY);
@@ -730,16 +731,16 @@ void ImageStacker::processFrame(int index) {
TileAlignment alignment =
computeTileAlignment(referencePyramid, currentPyramid, 64);
int scaledW = width;
int scaledH = height;
int scaledW = width * scale;
int scaledH = height * scale;
// Merge with Robust Weighting
// Y Plane - Iterate over Output Grid
#pragma omp parallel for collapse(2) num_threads(4)
for (int y = 0; y < scaledH; ++y) {
for (int x = 0; x < scaledW; ++x) {
float refX = (float)x;
float refY = (float)y;
float refX = (float)x / scale;
float refY = (float)y / scale;
Point offset = alignment.getOffset((int)refX, (int)refY);
float tx = refX + offset.x;
@@ -760,8 +761,8 @@ void ImageStacker::processFrame(int index) {
accumY[destIdx] += (int32_t)targetVal * weight;
weightY[destIdx] += weight;
if (weight > 128) {
int refIdx = y * width + x;
if (x % scale == 0 && y % scale == 0 && weight > 128) {
int refIdx = (y / scale) * width + (x / scale);
referenceY[refIdx] =
(uint16_t)((referenceY[refIdx] * 7 + targetVal) / 8);
}
@@ -788,8 +789,8 @@ void ImageStacker::processFrame(int index) {
#pragma omp parallel for collapse(2) num_threads(4)
for (int y = 0; y < scaledUVHeight; ++y) {
for (int x = 0; x < scaledUVWidth; ++x) {
float refUVX = (float)x;
float refUVY = (float)y;
float refUVX = (float)x / scale;
float refUVY = (float)y / scale;
Point offset = alignment.getOffset(refUVX * 2, refUVY * 2);
float tx = refUVX + offset.x / 2.0f;
@@ -815,8 +816,8 @@ void ImageStacker::processFrame(int index) {
accumV[destIdx] += (int32_t)targetV * weight;
weightV[destIdx] += weight;
if (weight > 128) {
int refIdx = y * uvRefWidth + x;
if (x % scale == 0 && y % scale == 0 && weight > 128) {
int refIdx = (y / scale) * uvRefWidth + (x / scale);
referenceU[refIdx] =
(uint16_t)((referenceU[refIdx] * 7 + targetU) / 8);
referenceV[refIdx] =
@@ -836,8 +837,8 @@ void ImageStacker::writeResult(uint32_t *outBitmap, int outWidth, int outHeight,
const char *outputPath, int *outFinalW,
int *outFinalH) {
// Use current scaled dimensions
int currentW = width;
int currentH = height;
int currentW = width * scale;
int currentH = height * scale;
int ySize = currentW * currentH;
int uvWidth = currentW / 2;
@@ -965,11 +966,10 @@ void ImageStacker::writeResult(uint32_t *outBitmap, int outWidth, int outHeight,
}
}
RawStacker::RawStacker(int width, int height)
: width(width), height(height), isFirstFrame(true) {
// Initialize accumulators at native 1x resolution (plane resolution is
// width/2 x height/2)
int planeSize = (width / 2) * (height / 2);
RawStacker::RawStacker(int width, int height, bool enableSuperRes)
: width(width), height(height), scale(enableSuperRes ? 2 : 1),
isFirstFrame(true) {
int planeSize = (width / 2 * scale) * (height / 2 * scale);
for (int i = 0; i < 4; ++i) {
accum[i].assign(planeSize, 0);
weight[i].assign(planeSize, 0);
@@ -1151,16 +1151,16 @@ void RawStacker::processFrame(int index) {
referencePyramid =
buildPyramid(staged.proxy.data.data(), proxyW, proxyH, 4);
int scaledW = proxyW;
int scaledH = proxyH;
int scaledW = proxyW * scale;
int scaledH = proxyH * scale;
for (int i = 0; i < 4; ++i) {
referencePlanes[i] = staged.planes[i];
#pragma omp parallel for collapse(2) num_threads(4)
for (int sy = 0; sy < scaledH; sy++) {
for (int sx = 0; sx < scaledW; sx++) {
float refX = (float)sx;
float refY = (float)sy;
float refX = (float)sx / scale;
float refY = (float)sy / scale;
float val =
sampleBicubicPlane(staged.planes[i], proxyW, proxyH, refX, refY);
@@ -1180,8 +1180,8 @@ void RawStacker::processFrame(int index) {
TileAlignment alignment =
computeTileAlignment(referencePyramid, currentPyramid, 48);
int scaledW = proxyW;
int scaledH = proxyH;
int scaledW = proxyW * scale;
int scaledH = proxyH * scale;
for (int i = 0; i < 4; ++i) {
const auto &srcPlane = staged.planes[i];
@@ -1189,8 +1189,8 @@ void RawStacker::processFrame(int index) {
#pragma omp parallel for collapse(2) num_threads(4)
for (int y = 0; y < scaledH; ++y) {
for (int x = 0; x < scaledW; ++x) {
float refX = (float)x;
float refY = (float)y;
float refX = (float)x / scale;
float refY = (float)y / scale;
Point offset = alignment.getOffset(refX, refY);
float tx = refX + offset.x;
@@ -1233,12 +1233,12 @@ void RawStacker::processFrame(int index) {
}
std::vector<uint16_t> RawStacker::process() {
int outW = width;
int outH = height;
int outW = width * scale;
int outH = height * scale;
std::vector<uint16_t> result(outW * outH);
int planeW = width / 2;
int planeH = height / 2;
int planeW = width / 2 * scale;
int planeH = height / 2 * scale;
#pragma omp parallel for collapse(2) num_threads(4)
for (int y = 0; y < planeH; ++y) {
+9 -4
View File
@@ -101,7 +101,7 @@ TileAlignment computeTileAlignment(const std::vector<GrayImage> &refPyramid,
class ImageStacker {
public:
ImageStacker(int width, int height);
ImageStacker(int width, int height, bool enableSuperRes = false);
~ImageStacker() = default;
// Add a frame to the stack. The first frame becomes the reference.
@@ -122,10 +122,13 @@ public:
const char *outputPath = nullptr, int *outFinalW = nullptr,
int *outFinalH = nullptr);
// Getter for the current scale
int getScale() const { return scale; }
private:
int width;
int height;
int scale; // Scaling factor (1 for normal, 2 for Super Res)
bool isFirstFrame;
int frameCount = 0;
@@ -160,7 +163,7 @@ struct StagedRawFrame {
class RawStacker {
public:
RawStacker(int width, int height);
RawStacker(int width, int height, bool enableSuperRes = false);
~RawStacker() = default;
// Add a raw frame to the stack
@@ -177,10 +180,12 @@ public:
// Returns a vector containing the stacked 16-bit Bayer data
std::vector<uint16_t> process();
int getScale() const { return scale; }
private:
int width;
int height;
int scale;
bool isFirstFrame;
int frameCount = 0;
float byteScale = 1.0f;
+7 -7
View File
@@ -18,14 +18,14 @@
} \
} while (0)
VulkanRawStacker::VulkanRawStacker(uint32_t w, uint32_t h,
VulkanRawStacker::VulkanRawStacker(uint32_t w, uint32_t h, bool enableSuperRes,
const float *blackLevel, float whiteLevel,
const float *wbGains,
const float *noiseModel,
const float *lensShadingMap,
uint32_t lscWidth, uint32_t lscHeight)
: width(w), height(h), mWhiteLevel(whiteLevel), mLscWidth(lscWidth),
mLscHeight(lscHeight) {
: width(w), height(h), mEnableSuperRes(enableSuperRes),
mWhiteLevel(whiteLevel), mLscWidth(lscWidth), mLscHeight(lscHeight) {
if (blackLevel)
memcpy(mBlackLevel, blackLevel, 4 * sizeof(float));
@@ -54,7 +54,7 @@ void VulkanRawStacker::initVulkanResources() {
VulkanManager &vm = VulkanManager::getInstance();
VkDevice device = vm.getDevice();
uint32_t scale = 1;
uint32_t scale = mEnableSuperRes ? 2 : 1;
uint32_t outW = width * scale;
uint32_t outH = height * scale;
@@ -674,7 +674,7 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
createPipelines();
}
uint32_t scale = 1;
uint32_t scale = mEnableSuperRes ? 2 : 1;
uint32_t outputW = width * scale;
uint32_t outputH = height * scale;
uint32_t inputW = width / 2;
@@ -853,8 +853,8 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
// SENSOR_NOISE_PROFILE gives (S, O) where Var(raw) = S * raw_value + O
// In UNORM space: value_unorm = raw_value / 65535.0f
// Var(unorm) = S * value_unorm / 65535.0f + O / (65535.0f * 65535.0f)
pc.noiseAlpha = mNoiseModel[0] / 65535.0f;
pc.noiseBeta = mNoiseModel[1] / (65535.0f * 65535.0f);
pc.noiseAlpha = mNoiseModel[0] / 65535.0f;
pc.noiseBeta = mNoiseModel[1] / (65535.0f * 65535.0f);
pc.baseNoise = pc.noiseBeta;
if (isRef) {
+4 -3
View File
@@ -15,9 +15,9 @@
*/
class VulkanRawStacker {
public:
VulkanRawStacker(uint32_t width, uint32_t height, const float *blackLevel,
float whiteLevel, const float *wbGains,
const float *noiseModel,
VulkanRawStacker(uint32_t width, uint32_t height, bool enableSuperRes,
const float *blackLevel, float whiteLevel,
const float *wbGains, const float *noiseModel,
const float *lensShadingMap = nullptr, uint32_t lscWidth = 0,
uint32_t lscHeight = 0);
~VulkanRawStacker();
@@ -30,6 +30,7 @@ public:
private:
uint32_t width, height;
bool mEnableSuperRes = false;
bool isFirstFrame = true;
int mCfaPattern = 0; // 0=RGGB, 1=GRBG, 2=GBRG, 3=BGGR
+14 -9
View File
@@ -16,11 +16,16 @@
} \
} while (0)
VulkanImageStacker::VulkanImageStacker(uint32_t w, uint32_t h)
: width(w), height(h) {
VulkanImageStacker::VulkanImageStacker(uint32_t w, uint32_t h, bool sr)
: width(w), height(h), enableSuperRes(sr) {
numTilesX = 1;
numTilesY = 1;
if (enableSuperRes) {
numTilesX = 2;
numTilesY = 2;
} else {
numTilesX = 1;
numTilesY = 1;
}
VulkanManager::getInstance().init();
initVulkanResources();
@@ -39,7 +44,7 @@ void VulkanImageStacker::initVulkanResources() {
VkDevice device = vm.getDevice();
// Create Accumulator Tiles
uint32_t scale = 1;
uint32_t scale = enableSuperRes ? 2 : 1;
uint32_t fullW = width * scale;
uint32_t fullH = height * scale;
@@ -794,7 +799,7 @@ bool VulkanImageStacker::processFrame(AHardwareBuffer *buffer) {
pc.t5 = transform[5];
pc.offsetX = offsetX;
pc.offsetY = offsetY;
uint32_t scale = 1;
uint32_t scale = enableSuperRes ? 2 : 1;
pc.scale = static_cast<float>(scale);
pc.width = width * scale;
pc.height = height * scale;
@@ -1105,9 +1110,9 @@ bool VulkanImageStacker::processStack(uint32_t *outBitmap, uint32_t outWidth,
return false;
}
uint32_t scale = 1;
uint32_t sensorW = width;
uint32_t sensorH = height;
uint32_t scale = enableSuperRes ? 2 : 1;
uint32_t sensorW = width * scale;
uint32_t sensorH = height * scale;
// Calculate the crop and rotation matrix
float transform[6] = {1, 0, 0, 0, 1, 0};
+2 -1
View File
@@ -7,7 +7,7 @@
class VulkanImageStacker {
public:
VulkanImageStacker(uint32_t width, uint32_t height);
VulkanImageStacker(uint32_t width, uint32_t height, bool enableSuperRes);
~VulkanImageStacker();
bool addFrame(AHardwareBuffer *buffer);
@@ -16,6 +16,7 @@ public:
private:
uint32_t width, height;
bool enableSuperRes;
bool isFirstFrame = true;
VulkanImage accumulator;
@@ -1816,6 +1816,10 @@ class Camera2Controller(private val context: Context) {
_state.value = _state.value.copy(useMultiFrame = useMultiFrame, multiFrameCount = multiFrameCount)
}
fun setUseSuperResolution(useSuperResolution: Boolean) {
_state.value = _state.value.copy(useSuperResolution = useSuperResolution)
}
fun setCapturingLivePhoto(enabled: Boolean) {
_state.value = _state.value.copy(isCapturingLivePhoto = enabled)
@@ -178,6 +178,7 @@ data class CameraState(
val isRawSupported: Boolean = false,
val useMultiFrame: Boolean = false,
val multiFrameCount: Int = 0,
val useSuperResolution: Boolean = false,
val useRaw: Boolean = false,
val useLivePhoto: Boolean = false,
// 是否正在拍摄 Live Photo (用于 UI 动画)
@@ -56,6 +56,7 @@ data class UserPreferences(
val defaultFocalLength: Float = 0f, // 默认焦段 (mm),0表示不设置
val useMultiFrame: Boolean = false, // 是否使用多帧合成
val multiFrameCount: Int = 8, // 多帧合成帧数
val useSuperResolution: Boolean = false, // 是否使用超分辨率
val photoQuality: Int = 95, // 照片质量: 90, 95, 100
val useLivePhoto: Boolean = false, // 是否启用 Live Photo (Motion Photo)
val backgroundImage: String = "camera_bg", // 背景图资源名或文件路径
@@ -109,6 +110,7 @@ class UserPreferencesRepository(private val context: Context) {
// 多帧合成 Key
private val USE_MULTI_FRAME = booleanPreferencesKey("use_multi_frame")
private val MULTI_FRAME_COUNT = intPreferencesKey("multi_frame_count")
private val USE_SUPER_RESOLUTION = booleanPreferencesKey("use_super_resolution")
private val PHOTO_QUALITY = intPreferencesKey("photo_quality")
private val USE_LIVE_PHOTO = booleanPreferencesKey("use_live_photo")
private val BACKGROUND_IMAGE = stringPreferencesKey("background_image")
@@ -154,6 +156,7 @@ class UserPreferencesRepository(private val context: Context) {
defaultFocalLength = preferences[DEFAULT_FOCAL_LENGTH] ?: 0f,
useMultiFrame = preferences[USE_MULTI_FRAME] ?: false,
multiFrameCount = preferences[MULTI_FRAME_COUNT] ?: 8,
useSuperResolution = preferences[USE_SUPER_RESOLUTION] ?: false,
photoQuality = preferences[PHOTO_QUALITY] ?: 95,
useLivePhoto = preferences[USE_LIVE_PHOTO] ?: false,
backgroundImage = preferences[BACKGROUND_IMAGE] ?: "camera_bg",
@@ -449,6 +452,15 @@ class UserPreferencesRepository(private val context: Context) {
}
}
/**
* 保存是否使用超分辨率
*/
suspend fun saveUseSuperResolution(enabled: Boolean) {
context.dataStore.edit { preferences ->
preferences[USE_SUPER_RESOLUTION] = enabled
}
}
/**
* 保存照片质量
*/
@@ -286,6 +286,7 @@ object PhotoManager {
captureResult: CaptureResult?,
thumbnail: Bitmap?,
useLivePhoto: Boolean,
useSuperResolution: Boolean
) = withContext(Dispatchers.IO) {
try {
val photoId = UUID.randomUUID().toString()
@@ -295,7 +296,11 @@ object PhotoManager {
val thumbnailFile = File(photoDir, THUMBNAIL_FILE)
val metadataFile = File(photoDir, METADATA_FILE)
val cropRegion = captureResult?.get(CaptureResult.SCALER_CROP_REGION)
var cropRegion = captureResult?.get(CaptureResult.SCALER_CROP_REGION)
if (useSuperResolution && cropRegion != null) {
cropRegion =
Rect(cropRegion.left * 2, cropRegion.top * 2, cropRegion.right * 2, cropRegion.bottom * 2)
}
val dimensions =
BitmapUtils.calculateProcessedRect(metadata.width, metadata.height, metadata.ratio, cropRegion, metadata.rotation)
@@ -564,6 +569,7 @@ object PhotoManager {
noiseReductionValue: Float,
chromaNoiseReductionValue: Float,
photoQuality: Int = 95,
useSuperResolution: Boolean = false,
useGpuAcceleration: Boolean = true,
) = withContext(Dispatchers.IO) {
try {
@@ -579,6 +585,7 @@ object PhotoManager {
rotation,
aspectRatio,
yuvFile.absolutePath,
useSuperResolution,
useGpuAcceleration
) ?: return@withContext
// Save Original (Stacked Result)
@@ -621,6 +628,7 @@ object PhotoManager {
noiseReductionValue: Float,
chromaNoiseReductionValue: Float,
photoQuality: Int = 95,
useSuperResolution: Boolean = false,
useGpuAcceleration: Boolean = true,
exposureBias: Float? = null,
droMode: MeteringSystem.DROMode = MeteringSystem.DROMode.OFF
@@ -645,6 +653,7 @@ object PhotoManager {
val byteBuffer = MultiFrameStacker.processBurstRaw(
images, characteristics,
useSuperResolution,
useGpuAcceleration,
masterBlackLevel = rawMetadata.blackLevel,
whiteLevel = rawMetadata.whiteLevel.toInt(),
@@ -658,8 +667,8 @@ object PhotoManager {
val result: Bitmap = run {
// Construct metadata for Linear RGB
val linearMetadata = rawMetadata.copy(
width = firstImageWidth,
height = firstImageHeight,
width = firstImageWidth * if (useSuperResolution) 2 else 1,
height = firstImageHeight * if (useSuperResolution) 2 else 1,
cfaPattern = RawMetadata.CFA_LINEAR_RGB,
blackLevel = floatArrayOf(0f, 0f, 0f, 0f),
whiteLevel = 65535f,
@@ -670,9 +679,9 @@ object PhotoManager {
val bitmap = RawDemosaicProcessor.getInstance().process(
context,
byteBuffer,
firstImageWidth,
firstImageHeight,
firstImageWidth * 6, // 3 channels * 2 bytes
firstImageWidth * if (useSuperResolution) 2 else 1,
firstImageHeight * if (useSuperResolution) 2 else 1,
firstImageWidth * (if (useSuperResolution) 2 else 1) * 6, // 3 channels * 2 bytes
linearMetadata,
aspectRatio,
metadata.cropRegion,
@@ -730,6 +739,7 @@ object PhotoManager {
noiseReductionValue: Float,
chromaNoiseReductionValue: Float,
photoQuality: Int = 95,
useSuperResolution: Boolean = false,
useGpuAcceleration: Boolean = true,
exposureBias: Float? = null,
droMode: MeteringSystem.DROMode = MeteringSystem.DROMode.OFF
@@ -748,6 +758,7 @@ object PhotoManager {
noiseReductionValue,
chromaNoiseReductionValue,
photoQuality,
useSuperResolution,
useGpuAcceleration
)
}
@@ -767,6 +778,7 @@ object PhotoManager {
noiseReductionValue,
chromaNoiseReductionValue,
photoQuality,
useSuperResolution,
useGpuAcceleration,
exposureBias,
droMode
@@ -40,6 +40,7 @@ object MultiFrameStacker {
rotation: Int,
aspectRatio: AspectRatio?,
outputPath: String? = null,
enableSuperResolution: Boolean = false,
useVulkan: Boolean = true
): Bitmap? {
if (images.isEmpty()) return null
@@ -47,14 +48,15 @@ object MultiFrameStacker {
val width = images[0].width
val height = images[0].height
val scale = if (enableSuperResolution) 2 else 1
val startTime = System.currentTimeMillis()
if (useVulkan) {
PLog.i(
TAG,
"Starting Vulkan stacking process for ${images.size} frames ($width x $height)"
"Starting Vulkan stacking process for ${images.size} frames ($width x $height). SR=$enableSuperResolution"
)
val stackerPtr = createVulkanStackerNative(width, height)
val stackerPtr = createVulkanStackerNative(width, height, enableSuperResolution)
if (stackerPtr != 0L) {
try {
for (image in images) {
@@ -70,8 +72,8 @@ object MultiFrameStacker {
PLog.d(TAG, "Stack frames processed")
val dimensions = BitmapUtils.calculateProcessedRect(width, height, aspectRatio, null, rotation)
val targetW = dimensions.width()
val targetH = dimensions.height()
val targetW = dimensions.width() * scale
val targetH = dimensions.height() * scale
val previewBitmap = createBitmap(targetW, targetH)
processVulkanStackNative(stackerPtr, previewBitmap, rotation)
@@ -89,9 +91,9 @@ object MultiFrameStacker {
// Fallback or legacy path
PLog.i(
TAG,
"Starting legacy stacking process for ${images.size} frames ($width x $height)."
"Starting legacy stacking process for ${images.size} frames ($width x $height). SR=$enableSuperResolution"
)
val stackerPtr = createStackerNative(width, height)
val stackerPtr = createStackerNative(width, height, enableSuperResolution)
if (stackerPtr == 0L) return null
try {
@@ -115,8 +117,8 @@ object MultiFrameStacker {
clearStagedFramesNative(stackerPtr)
val dimensions = BitmapUtils.calculateProcessedRect(width, height, aspectRatio, null, rotation)
val targetW = dimensions.width()
val targetH = dimensions.height()
val targetW = dimensions.width() * scale
val targetH = dimensions.height() * scale
val previewBitmap = createBitmap(targetW, targetH)
processStackNative(
@@ -138,6 +140,7 @@ object MultiFrameStacker {
fun processBurstRaw(
images: List<SafeImage>,
characteristics: CameraCharacteristics,
enableSuperResolution: Boolean = false,
useVulkan: Boolean = true,
masterBlackLevel: FloatArray = floatArrayOf(0f, 0f, 0f, 0f),
whiteLevel: Int = 1023,
@@ -153,13 +156,13 @@ object MultiFrameStacker {
PLog.d(
TAG,
"Starting RAW stacking for ${images.size} frames. Pattern=$sensorCfa Vulkan=$useVulkan WL=$whiteLevel"
"Starting RAW stacking for ${images.size} frames. Pattern=$sensorCfa SR=$enableSuperResolution Vulkan=$useVulkan WL=$whiteLevel"
)
// Try Vulkan path
if (useVulkan) {
val vulkanStackerPtr = createVulkanRawStackerNative(
width, height,
width, height, enableSuperResolution,
masterBlackLevel, whiteLevel, whiteBalanceGains, noiseModel,
lensShading, lensShadingWidth, lensShadingHeight
)
@@ -176,7 +179,8 @@ object MultiFrameStacker {
}
// RGB output (3 channels * 16-bit = 6 bytes per pixel)
val stackedBuffer = ByteBuffer.allocateDirect(width * height * 6)
val scale = if (enableSuperResolution) 2 else 1
val stackedBuffer = ByteBuffer.allocateDirect(width * scale * height * scale * 6)
.order(ByteOrder.nativeOrder())
val success = processVulkanRawStackNative(vulkanStackerPtr, stackedBuffer)
@@ -198,7 +202,7 @@ object MultiFrameStacker {
// CPU fallback
PLog.i(TAG, "Using CPU RAW stacker")
var stackerPtr = createRawStackerNative(width, height)
var stackerPtr = createRawStackerNative(width, height, enableSuperResolution)
if (stackerPtr == 0L) {
PLog.e(TAG, "Failed to create CPU raw stacker")
return null
@@ -220,7 +224,8 @@ object MultiFrameStacker {
}
clearStagedRawFramesNative(stackerPtr)
val stackedBuffer = ByteBuffer.allocateDirect(width * height * 2)
val scale = if (enableSuperResolution) 2 else 1
val stackedBuffer = ByteBuffer.allocateDirect(width * scale * height * scale * 2)
.order(ByteOrder.nativeOrder())
processRawStackWithBufferNative(stackerPtr, stackedBuffer)
@@ -233,7 +238,7 @@ object MultiFrameStacker {
// --- Native Methods ---
private external fun createStackerNative(width: Int, height: Int): Long
private external fun createStackerNative(width: Int, height: Int, enableSuperRes: Boolean): Long
private external fun stageFrameNative(
stackerPtr: Long,
@@ -256,7 +261,7 @@ object MultiFrameStacker {
private external fun releaseStackerNative(stackerPtr: Long)
private external fun createVulkanStackerNative(width: Int, height: Int): Long
private external fun createVulkanStackerNative(width: Int, height: Int, enableSuperRes: Boolean): Long
private external fun addVulkanFrameNative(
stackerPtr: Long,
hardwareBuffer: android.hardware.HardwareBuffer
@@ -265,7 +270,7 @@ object MultiFrameStacker {
private external fun processVulkanStackNative(stackerPtr: Long, outBitmap: Bitmap?, rotation: Int): Boolean
private external fun releaseVulkanStackerNative(stackerPtr: Long)
private external fun createRawStackerNative(width: Int, height: Int): Long
private external fun createRawStackerNative(width: Int, height: Int, enableSuperRes: Boolean): Long
private external fun stageRawFrameNative(stackerPtr: Long, rawData: ByteBuffer, rowStride: Int, cfaPattern: Int)
private external fun processRawFrameNative(stackerPtr: Long, index: Int)
private external fun clearStagedRawFramesNative(stackerPtr: Long)
@@ -274,7 +279,7 @@ object MultiFrameStacker {
// Vulkan RAW Stacker
private external fun createVulkanRawStackerNative(
width: Int, height: Int,
width: Int, height: Int, enableSuperRes: Boolean,
blackLevel: FloatArray, whiteLevel: Int, wbGains: FloatArray, noiseModel: FloatArray,
lensShadingMap: FloatArray?, shadingMapWidth: Int, shadingMapHeight: Int
): Long
@@ -85,6 +85,7 @@ fun CameraScreen(
val categoryOrder by viewModel.categoryOrder.collectAsState(emptyList())
val useRaw by viewModel.useRaw.collectAsState()
val useMultiFrame by viewModel.useMultiFrame.collectAsState()
val useSuperResolution by viewModel.useSuperResolution.collectAsState()
val useLivePhoto by viewModel.useLivePhoto.collectAsState()
// 标记相机是否已打开
@@ -441,8 +442,18 @@ fun CameraScreen(
},
useMultiFrame = useMultiFrame,
onMultiFrameToggle = {
if (!it) {
viewModel.setUseSuperResolution(false)
}
viewModel.setUseMultiFrame(it)
},
useSuperResolution = useSuperResolution,
onSuperResolutionToggle = {
if (it) {
viewModel.setUseMultiFrame(true)
}
viewModel.setUseSuperResolution(it)
},
showGrid = state.showGrid,
onShowGridToggle = { viewModel.toggleGrid() }
)
@@ -39,6 +39,8 @@ fun CameraTopSheet(
onMoreSettingsClick: () -> Unit,
useMultiFrame: Boolean,
onMultiFrameToggle: (Boolean) -> Unit,
useSuperResolution: Boolean,
onSuperResolutionToggle: (Boolean) -> Unit,
showGrid: Boolean,
onShowGridToggle: (Boolean) -> Unit,
modifier: Modifier = Modifier
@@ -104,6 +106,13 @@ fun CameraTopSheet(
modifier = Modifier.weight(1f)
)
QuickSettingToggle(
title = stringResource(R.string.settings_use_super_resolution),
checked = useSuperResolution,
onCheckedChange = onSuperResolutionToggle,
modifier = Modifier.weight(1f)
)
// RAW Toggle (if supported)
if (isRawSupported) {
QuickSettingToggle(
@@ -67,7 +67,6 @@ fun SettingsScreen(
val noiseReduction by viewModel.noiseReduction.collectAsState(initial = 0f)
val chromaNoiseReduction by viewModel.chromaNoiseReduction.collectAsState(initial = 0f)
val defaultFocalLength by viewModel.defaultFocalLength.collectAsState(initial = 0f)
val useMultiFrame by viewModel.useMultiFrame.collectAsState()
val multiFrameCount by viewModel.multiFrameCount.collectAsState()
val useLivePhoto by viewModel.useLivePhoto.collectAsState()
val photoQuality by viewModel.photoQuality.collectAsState(initial = 95)
@@ -456,61 +455,42 @@ fun SettingsScreen(
modifier = Modifier.padding(vertical = 8.dp)
)
SwitchSettingItem(
title = stringResource(R.string.settings_use_multi_frame),
description = stringResource(R.string.settings_use_multi_frame_description),
checked = useMultiFrame,
onCheckedChange = { viewModel.setUseMultiFrame(it) }
QualityLevelSetting(
title = stringResource(R.string.settings_multi_frame_count),
description = stringResource(R.string.settings_multi_frame_count_description),
levels = listOf(
4 to "4",
8 to "8",
12 to "12",
16 to "16",
),
currentLevel = multiFrameCount,
onLevelSelected = { viewModel.setMultiFrameCount(it) }
)
if (useMultiFrame) {
HorizontalDivider(
color = Color.White.copy(alpha = 0.1f),
modifier = Modifier.padding(vertical = 12.dp)
)
HorizontalDivider(
color = Color.White.copy(alpha = 0.1f),
modifier = Modifier.padding(vertical = 8.dp)
)
QualityLevelSetting(
title = stringResource(R.string.settings_multi_frame_count),
description = stringResource(R.string.settings_multi_frame_count_description),
levels = listOf(
4 to "4",
8 to "8",
12 to "12",
16 to "16",
),
currentLevel = multiFrameCount,
onLevelSelected = { viewModel.setMultiFrameCount(it) }
)
SwitchSettingItem(
title = stringResource(R.string.settings_use_gpu_acceleration),
description = stringResource(R.string.settings_use_gpu_acceleration_description),
checked = useGpuAcceleration,
onCheckedChange = { viewModel.setUseGpuAcceleration(it) }
)
HorizontalDivider(
color = Color.White.copy(alpha = 0.1f),
modifier = Modifier.padding(vertical = 8.dp)
)
HorizontalDivider(
color = Color.White.copy(alpha = 0.1f),
modifier = Modifier.padding(vertical = 8.dp)
)
HorizontalDivider(
color = Color.White.copy(alpha = 0.1f),
modifier = Modifier.padding(vertical = 8.dp)
)
SwitchSettingItem(
title = stringResource(R.string.settings_use_gpu_acceleration),
description = stringResource(R.string.settings_use_gpu_acceleration_description),
checked = useGpuAcceleration,
onCheckedChange = { viewModel.setUseGpuAcceleration(it) }
)
HorizontalDivider(
color = Color.White.copy(alpha = 0.1f),
modifier = Modifier.padding(vertical = 8.dp)
)
SwitchSettingItem(
title = stringResource(R.string.settings_use_live_photo),
description = stringResource(R.string.settings_use_live_photo_description),
checked = useLivePhoto,
onCheckedChange = { viewModel.setUseLivePhoto(it) }
)
}
SwitchSettingItem(
title = stringResource(R.string.settings_use_live_photo),
description = stringResource(R.string.settings_use_live_photo_description),
checked = useLivePhoto,
onCheckedChange = { viewModel.setUseLivePhoto(it) }
)
HorizontalDivider(
color = Color.White.copy(alpha = 0.1f),
@@ -3,6 +3,7 @@ package com.hinnka.mycamera.viewmodel
import android.app.Application
import android.content.Context
import android.graphics.Bitmap
import android.graphics.ImageFormat
import android.graphics.SurfaceTexture
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CaptureResult
@@ -151,6 +152,9 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
val multiFrameCount: StateFlow<Int> = userPreferencesRepository.userPreferences
.map { it.multiFrameCount }
.stateIn(viewModelScope, SharingStarted.Eagerly, 8)
val useSuperResolution: StateFlow<Boolean> = userPreferencesRepository.userPreferences
.map { it.useSuperResolution }
.stateIn(viewModelScope, SharingStarted.Eagerly, false)
val useLivePhoto: StateFlow<Boolean> = userPreferencesRepository.userPreferences
.map { it.useLivePhoto }
.stateIn(viewModelScope, SharingStarted.Eagerly, false)
@@ -347,6 +351,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
cameraController.setShowGrid(prefs.showGrid)
cameraController.setUseMultiFrame(prefs.useMultiFrame, prefs.multiFrameCount)
cameraController.setUseSuperResolution(prefs.useSuperResolution)
cameraController.setUseLivePhoto(prefs.useLivePhoto)
} else {
// 如果没有任何偏好设置,使用配置文件中的默认 LUT(第一个)
@@ -921,6 +926,16 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
}
}
/**
* 设置是否使用超分辨率
*/
fun setUseSuperResolution(enabled: Boolean) {
cameraController.setUseSuperResolution(enabled)
viewModelScope.launch {
userPreferencesRepository.saveUseSuperResolution(enabled)
}
reopenCamera()
}
/**
* 设置是否启用 Live Photo
*/
@@ -1419,7 +1434,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
characteristics ?: return
val photoId =
PhotoManager.preparePhoto(context, metadata, captureResult, previewThumbnail, useLivePhoto.value)
PhotoManager.preparePhoto(context, metadata, captureResult, previewThumbnail, useLivePhoto.value, false)
if (photoId == null) {
PLog.e(TAG, "Failed to save image")
return
@@ -1498,6 +1513,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
// 应用方向偏移
val rotation = (baseRotation + orientationOffset) % 360
val useSuperRes = useSuperResolution.value
// 创建统一的 PhotoMetadata,包含编辑配置和拍摄信息
val metadata = PhotoMetadata(
lutId = lutIdToSave,
@@ -1506,8 +1523,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
sharpening = sharpeningValue,
noiseReduction = noiseReductionValue,
chromaNoiseReduction = chromaNoiseReductionValue,
width = images[0].width,
height = images[0].height,
width = images[0].width * (if (useSuperRes) 2 else 1),
height = images[0].height * (if (useSuperRes) 2 else 1),
ratio = aspectRatio,
rotation = rotation,
deviceModel = captureInfo.model,
@@ -1535,7 +1552,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
characteristics ?: return
val photoId = PhotoManager.preparePhoto(
context, metadata, captureResult, previewThumbnail,
useLivePhoto.value
useLivePhoto.value, useSuperRes
)
if (photoId == null) {
PLog.e(TAG, "Failed to save burst image")
@@ -1559,6 +1576,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
noiseReductionValue,
chromaNoiseReductionValue,
photoQualityValue,
useSuperResolution = useSuperRes,
useGpuAcceleration = useGpuAcceleration.value,
exposureBias = state.value.exposureBias,
droMode = droModeForProcessing