feat: HistogramView

This commit is contained in:
Hinnka committed 2026-01-02 15:54:37 +08:00
1 parent c38a6ef377
commit aa710ab215
4 files changed
+119 -2

No files matched your search

@@ -48,6 +48,7 @@ class Camera2Controller(private val context: Context) {
private var previewSurface: Surface? = null
private var imageReader: ImageReader? = null
private var previewImageReader: ImageReader? = null
private var cameraThread: HandlerThread? = null
private var cameraHandler: Handler? = null
@@ -175,6 +176,38 @@ class Camera2Controller(private val context: Context) {
}, cameraHandler)
}
// 创建用于直方图计算的预览 ImageReader (低分辨率 YUV)
previewImageReader = ImageReader.newInstance(320, 240, ImageFormat.YUV_420_888, 2).apply {
setOnImageAvailableListener({ reader ->
val image = reader.acquireLatestImage() ?: return@setOnImageAvailableListener
try {
val planes = image.planes
val buffer = planes[0].buffer // Y plane
val pixelStride = planes[0].pixelStride
val rowStride = planes[0].rowStride
val width = image.width
val height = image.height
val histogram = IntArray(256)
val rowBuffer = ByteArray(rowStride)
for (y in 0 until height) {
buffer.position(y * rowStride)
buffer.get(rowBuffer, 0, rowStride)
for (x in 0 until width) {
val value = rowBuffer[x * pixelStride].toInt() and 0xFF
histogram[value]++
}
}
_state.value = _state.value.copy(histogram = histogram)
} catch (e: Exception) {
Log.e(TAG, "Failed to calculate histogram", e)
} finally {
image.close()
}
}, cameraHandler)
}
Log.d(TAG, "Opening camera: $cameraId")
cameraManager.openCamera(cameraId, object : CameraDevice.StateCallback() {
@@ -226,7 +259,8 @@ class Camera2Controller(private val context: Context) {
set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON)
}
val surfaces = listOf(surface, reader.surface)
val surfaces = mutableListOf(surface, reader.surface)
previewImageReader?.surface?.let { surfaces.add(it) }
// Android 9+ 使用 SessionConfiguration
val outputConfigs = surfaces.map { OutputConfiguration(it) }
@@ -257,6 +291,7 @@ class Camera2Controller(private val context: Context) {
try {
// 开始预览
previewRequestBuilder?.let { builder ->
previewImageReader?.surface?.let { builder.addTarget(it) }
session.setRepeatingRequest(builder.build(), null, cameraHandler)
}
@@ -777,6 +812,9 @@ class Camera2Controller(private val context: Context) {
imageReader?.close()
imageReader = null
previewImageReader?.close()
previewImageReader = null
previewSurface = null
previewRequestBuilder = null
@@ -119,7 +119,10 @@ data class CameraState(
val currentLutName: String? = null,
val lutIntensity: Float = 1.0f,
val lutEnabled: Boolean = false,
val availableLuts: List<String> = emptyList()
val availableLuts: List<String> = emptyList(),
// 直方图数据 (256个灰度值)
val histogram: IntArray? = null
) {
/**
* 获取当前相机信息
@@ -31,6 +31,7 @@ import com.hinnka.mycamera.camera.LensType
import com.hinnka.mycamera.ui.components.BackCameraSelector
import com.hinnka.mycamera.ui.components.GalleryThumbnail
import com.hinnka.mycamera.ui.components.EditControlPanel
import com.hinnka.mycamera.ui.components.HistogramView
import com.hinnka.mycamera.viewmodel.CameraViewModel
import com.hinnka.mycamera.viewmodel.GalleryViewModel
import kotlin.math.abs
@@ -105,6 +106,15 @@ fun CameraScreen(
modifier = Modifier.fillMaxSize()
)
// 实时直方图
HistogramView(
histogram = state.histogram,
modifier = Modifier
.padding(16.dp)
.padding(top = 120.dp)
.align(Alignment.TopStart)
)
// 后置摄像头选择器(只有多个后置摄像头时显示)
val backCameras = viewModel.getBackCameras()
if (backCameras.size > 1 && state.currentLensType != LensType.FRONT) {
@@ -0,0 +1,66 @@
package com.hinnka.mycamera.ui.components
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.drawscope.Fill
import androidx.compose.ui.unit.dp
/**
* 直方图组件
*
* @param histogram 直方图数据 (256个灰度值)
* @param modifier 修饰符
*/
@Composable
fun HistogramView(
histogram: IntArray?,
modifier: Modifier = Modifier
) {
Box(
modifier = modifier
.width(120.dp)
.height(30.dp)
.clip(RoundedCornerShape(4.dp))
.background(Color.Black.copy(alpha = 0.4f))
.padding(4.dp)
) {
Canvas(modifier = Modifier.fillMaxSize()) {
if (histogram == null || histogram.isEmpty()) return@Canvas
val maxCount = histogram.maxOrNull() ?: 1
if (maxCount == 0) return@Canvas
val width = size.width
val height = size.height
val stepX = width / 256f
val path = Path().apply {
moveTo(0f, height)
for (i in histogram.indices) {
val x = i * stepX
val y = height - (histogram[i].toFloat() / maxCount * height)
lineTo(x, y)
}
lineTo(width, height)
close()
}
drawPath(
path = path,
color = Color.White.copy(alpha = 0.8f),
style = Fill
)
}
}
}