部分借鉴于:https://blog.csdn.net/weixin_45639314/article/details/142210634
目录
一、需求介绍
二、UVC介绍
三、解析
四、补丁修改
1、预览的限制主要存在于hal层和framework层
2、添加所需要的分辨率:
3、hal层修改
4、frameworks
5、备用方案
一、需求介绍
这个问题是碰到了一个客户,他的需求是在Android11 rk3566上需要支持1080p以上的usb摄像头支持,而在我们Android11系统原生的相机中可以打开的最大分辨率也是1080p(即2.1百万像素)。
而我们客户需要支持2560*1440(2k-四百万像素),和最大3840*2610(4k-800万像素)。
二、UVC介绍
UVC(USB Video Class)是一种 USB 设备类标准,允许通过 USB 连接的视频设备(如摄像头、网络摄像头和其他视频捕捉设备)与计算机或其他主机设备进行通信。UVC 使得视频设备的使用变得更加简单和通用,因为它不需要特定的驱动程序,主机操作系统通常可以直接识别和使用这些设备。
特点:
1、即插即用:
UVC 设备可以在连接到主机时自动识别,无需安装额外的驱动程序。这使得用户能够快速方便地使用视频设备。
2、跨平台支持:
UVC 设备通常可以在多种操作系统上工作,包括 Windows、macOS 和 Linux。这种跨平台的兼容性使得 UVC 成为视频设备的标准选择。
3、视频格式支持:
UVC 支持多种视频格式和分辨率,包括 MJPEG、YUY2、H.264 等。设备可以根据主机的能力和应用程序的需求选择合适的格式。
4、控制功能:
UVC 设备通常支持多种控制功能,例如亮度、对比度、饱和度、焦距等。这些控制可以通过 USB 接口进行调整。
5、流媒体支持:
UVC 设备可以用于实时视频流传输,适用于视频会议、直播、监控等应用场景。
三、解析
1、v4l2命令的使用
//列出所有设视频设备
v4l2-ctl --list-devices
//获取特定设备的支持格式
v4l2-ctl --device=/dev/video23 --list-formats
//获取设备支持的分辨率
v4l2-ctl -d /dev/video23 --list-framesizes=YUYV
2、查看打开的摄像头的各种信息
dumpsys media.camera
四、补丁修改
1、预览的限制主要存在于hal层和framework层
关于摄像头部分的源码目录:
#SDK 接口
frameworks/base/core/java/android/hardware/Camera.java
frameworks/base/core/jni/android_hardware_Camera.cpp
#上层 Camera 服务
frameworks/av/camera/
# HAL层
hardware/rockchip/camera
hardware/interfaces/camera/
# 配置文件,对应USB和CSI之类的摄像头配置
# 包含了支持分辨率,闪光灯等等的一些特性。
device/rockchip/common/external_camera_config.xml
hardware/rockchip/camera/etc/camera/
2、添加所需要的分辨率:
diff --git a/device/rockchip/common/external_camera_config.xml b/device/rockchip/common/external_camera_config.xml
index d377826..d5ddd9d 100755
--- a/external_camera_config.xml
+++ b/external_camera_config.xml
@@ -60,13 +60,18 @@
<Limit width="1600" height="1200" fpsBound="15.0" />
<Limit width="1920" height="1080" fpsBound="30.0" />
<Limit width="1920" height="1080" fpsBound="15.0" />
+ <Limit width="2560" height="1440" fpsBound="30.0" />
+ <Limit width="2560" height="1440" fpsBound="15.0" />
<Limit width="2592" height="1944" fpsBound="30.0" />
<Limit width="2592" height="1944" fpsBound="15.0" />
<Limit width="2592" height="1944" fpsBound="10.0" />
<Limit width="2592" height="1944" fpsBound="5.0" />
+ <Limit width="3840" height="2160" fpsBound="30.0" />
+ <Limit width="3840" height="2160" fpsBound="15.0" />
<!-- image size larger than the last entry will not be supported-->
</FpsList>
<!-- orientation -->
- <Orientation degree="90"/>
+ <!-- <Orientation degree="90"/> 这里调整的是摄像头的旋转方向 -->
+ <Orientation degree="0"/> <!-- for qipai camera -->
</Device>
</ExternalCamera>
3、hal层修改
源码路径:hardware/interfaces/camera/device/3.4/default/RgaCropScale.cpp
diff --git a/hardware/interfaces/camera/device/3.4/default/RgaCropScale.cpp b/hardware/interfaces/camera/device/3.4/default/RgaCropScale.cpp
index 55a2c3d08d..d3eb278093 100644
--- a/hardware/interfaces/camera/device/3.4/default/RgaCropScale.cpp
+++ b/hardware/interfaces/camera/device/3.4/default/RgaCropScale.cpp
@@ -21,21 +21,21 @@
namespace android {
namespace camera2 {
-#if (defined(TARGET_RK32) || defined(TARGET_RK3368))
+//#if (defined(TARGET_RK32) || defined(TARGET_RK3368))
#define RGA_VER (2.0)
#define RGA_ACTIVE_W (4096)
#define RGA_VIRTUAL_W (4096)
#define RGA_ACTIVE_H (4096)
#define RGA_VIRTUAL_H (4096)
-#else
-#define RGA_VER (1.0)
-#define RGA_ACTIVE_W (2048)
-#define RGA_VIRTUAL_W (4096)
-#define RGA_ACTIVE_H (2048)
-#define RGA_VIRTUAL_H (2048)
+//#else
+//#define RGA_VER (1.0)
+//#define RGA_ACTIVE_W (2048)
+//#define RGA_VIRTUAL_W (4096)
+//#define RGA_ACTIVE_H (2048)
+//#define RGA_VIRTUAL_H (2048)
-#endif
+//#endif
int RgaCropScale::CropScaleNV12Or21(struct Params* in, struct Params* out)
4、frameworks
源码路径:frameworks/av/services/camera/libcameraservice/api1/client2/Parameters.h
上层接口解除1080P的限制。
diff --git a/frameworks/av/services/camera/libcameraservice/api1/client2/Parameters.h b/frameworks/av/services/camera/libcameraservice/api1/client2/Parameters.h
index 3a709c9791..163d060b81 100644
--- a/frameworks/av/services/camera/libcameraservice/api1/client2/Parameters.h
+++ b/frameworks/av/services/camera/libcameraservice/api1/client2/Parameters.h
@@ -199,11 +199,11 @@ struct Parameters {
// Max preview size allowed
// This is set to a 1:1 value to allow for any aspect ratio that has
// a max long side of 1920 pixels
- static const unsigned int MAX_PREVIEW_WIDTH = 1920;
- static const unsigned int MAX_PREVIEW_HEIGHT = 1920;
+ static const unsigned int MAX_PREVIEW_WIDTH = 4656;
+ static const unsigned int MAX_PREVIEW_HEIGHT = 3496;
// Initial max preview/recording size bound
- static const int MAX_INITIAL_PREVIEW_WIDTH = 1920;
- static const int MAX_INITIAL_PREVIEW_HEIGHT = 1080;
+ static const int MAX_INITIAL_PREVIEW_WIDTH = 4656;
+ static const int MAX_INITIAL_PREVIEW_HEIGHT = 3496;
// Aspect ratio tolerance
static const CONSTEXPR float ASPECT_RATIO_TOLERANCE = 0.001;
// Threshold for slow jpeg mode
到这里,系统相机—设置—分辨率与画质,应该就可以看到对应的最大的分辨率了。
5、备用方案
如果以上修改未能生效,可参考以下修改(该部分有经RK厂商修改):
hardware/interfaces/camera
From 75e1d29219f929404f3b42b994ac36dde19b0c82 Mon Sep 17 00:00:00 2001
From: Wang Panzhenzhuan <randy.wang@rock-chips.com>
Date: Tue, 19 Jan 2021 21:26:03 +0800
Subject: [PATCH 1/4] Camera: fix loss resolution issues
Signed-off-by: Wang Panzhenzhuan <randy.wang@rock-chips.com>
Change-Id: I01f614eec54168ab34e0c7376296a64804af9a1a
---
.../3.4/default/ExternalCameraDevice.cpp | 75 ++++++++++++++++---
.../3.4/default/ExternalCameraUtils.cpp | 0
.../ExternalCameraUtils.h | 1 +
3 files changed, 65 insertions(+), 11 deletions(-)
mode change 100644 => 100755 camera/device/3.4/default/ExternalCameraUtils.cpp
mode change 100644 => 100755 camera/device/3.4/default/include/ext_device_v3_4_impl/ExternalCameraUtils.h
diff --git a/camera/device/3.4/default/ExternalCameraDevice.cpp b/camera/device/3.4/default/ExternalCameraDevice.cpp
index d196e4b4f..882698fd3 100755
--- a/camera/device/3.4/default/ExternalCameraDevice.cpp
+++ b/camera/device/3.4/default/ExternalCameraDevice.cpp
@@ -338,6 +338,7 @@ status_t ExternalCameraDevice::initDefaultCharsKeys(
// android.jpeg
const int32_t jpegAvailableThumbnailSizes[] = {0, 0,
+ 160, 120,
176, 144,
240, 144,
256, 144,
@@ -587,15 +588,24 @@ status_t ExternalCameraDevice::initOutputCharskeysByFormat(
return UNKNOWN_ERROR;
}
+ ALOGV("inputfourcc:%c%c%c%c",
+ fourcc & 0xFF,
+ (fourcc >> 8) & 0xFF,
+ (fourcc >> 16) & 0xFF,
+ (fourcc >> 24) & 0xFF);
+
std::vector<int32_t> streamConfigurations;
std::vector<int64_t> minFrameDurations;
std::vector<int64_t> stallDurations;
for (const auto& supportedFormat : mSupportedFormats) {
+#if 0
+ // wpzz add don't need skip now.
if (supportedFormat.fourcc != fourcc) {
// Skip 4CCs not meant for the halFormats
continue;
}
+#endif
for (const auto& format : halFormats) {
streamConfigurations.push_back(format);
streamConfigurations.push_back(supportedFormat.width);
@@ -633,6 +643,13 @@ status_t ExternalCameraDevice::initOutputCharskeysByFormat(
stallDurations.push_back(supportedFormat.height);
stallDurations.push_back(stall_duration);
}
+ ALOGV("supportedFormat:%c%c%c%c, w %d, h %d, minFrameDuration(%lld)",
+ supportedFormat.fourcc & 0xFF,
+ (supportedFormat.fourcc >> 8) & 0xFF,
+ (supportedFormat.fourcc >> 16) & 0xFF,
+ (supportedFormat.fourcc >> 24) & 0xFF,
+ supportedFormat.width, supportedFormat.height, minFrameDuration);
+
}
UPDATE(streamConfiguration, streamConfigurations.data(), streamConfigurations.size());
@@ -667,6 +684,8 @@ bool ExternalCameraDevice::calculateMinFps(
fpsRanges.push_back(framerate);
}
minFps /= 2;
+ if (0 == minFps)
+ minFps = 1;
int64_t maxFrameDuration = 1000000000LL / minFps;
UPDATE(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, fpsRanges.data(),
@@ -713,26 +732,24 @@ status_t ExternalCameraDevice::initOutputCharsKeys(
}
}
- if (hasDepth) {
- initOutputCharskeysByFormat(metadata, V4L2_PIX_FMT_Z16, halDepthFormats,
- ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS_OUTPUT,
- ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS,
- ANDROID_DEPTH_AVAILABLE_DEPTH_MIN_FRAME_DURATIONS,
- ANDROID_DEPTH_AVAILABLE_DEPTH_STALL_DURATIONS);
- }
if (hasColor) {
initOutputCharskeysByFormat(metadata, V4L2_PIX_FMT_MJPEG, halFormats,
ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT,
ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
ANDROID_SCALER_AVAILABLE_STALL_DURATIONS);
- }
- if (hasColor_yuv) {
+ } else if (hasColor_yuv) {
initOutputCharskeysByFormat(metadata, V4L2_PIX_FMT_YUYV, halFormats,
ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT,
ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
ANDROID_SCALER_AVAILABLE_STALL_DURATIONS);
+ } else if (hasDepth) {
+ initOutputCharskeysByFormat(metadata, V4L2_PIX_FMT_Z16, halDepthFormats,
+ ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS_OUTPUT,
+ ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS,
+ ANDROID_DEPTH_AVAILABLE_DEPTH_MIN_FRAME_DURATIONS,
+ ANDROID_DEPTH_AVAILABLE_DEPTH_STALL_DURATIONS);
}
calculateMinFps(metadata);
@@ -765,7 +782,7 @@ status_t ExternalCameraDevice::initOutputCharsKeys(
void ExternalCameraDevice::getFrameRateList(
int fd, double fpsUpperBound, SupportedV4L2Format* format) {
format->frameRates.clear();
-
+ format->maxFramerate = 1.0f;
v4l2_frmivalenum frameInterval {
.pixel_format = format->fourcc,
.width = format->width,
@@ -773,6 +790,13 @@ void ExternalCameraDevice::getFrameRateList(
.index = 0
};
+ ALOGV("format:%c%c%c%c, w %d, h %d, fpsUpperBound %f",
+ frameInterval.pixel_format & 0xFF,
+ (frameInterval.pixel_format >> 8) & 0xFF,
+ (frameInterval.pixel_format >> 16) & 0xFF,
+ (frameInterval.pixel_format >> 24) & 0xFF,
+ frameInterval.width, frameInterval.height, fpsUpperBound);
+
for (frameInterval.index = 0;
TEMP_FAILURE_RETRY(ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &frameInterval)) == 0;
++frameInterval.index) {
@@ -782,6 +806,9 @@ void ExternalCameraDevice::getFrameRateList(
frameInterval.discrete.numerator,
frameInterval.discrete.denominator};
double framerate = fr.getDouble();
+ if (framerate > format->maxFramerate) {
+ format->maxFramerate = framerate;
+ }
if (framerate > fpsUpperBound) {
continue;
}
@@ -837,7 +864,7 @@ void ExternalCameraDevice::trimSupportedFormats(
const auto& maxSize = sortedFmts[sortedFmts.size() - 1];
float maxSizeAr = ASPECT_RATIO(maxSize);
-
+#if 0 //该位置确认自己的camera调用的是哪一个接口
// Remove formats that has aspect ratio not croppable from largest size
std::vector<SupportedV4L2Format> out;
for (const auto& fmt : sortedFmts) {
@@ -855,6 +882,15 @@ void ExternalCameraDevice::trimSupportedFormats(
maxSize.width, maxSize.height);
}
}
+#else
+ std::vector<SupportedV4L2Format> out;
+ //all enum format added to SupportedFormat
+ ALOGD("%s(%d): don't care ratio of horizontally or vertical ",__FUNCTION__, __LINE__);
+
+ for (const auto& fmt : sortedFmts) {
+ out.push_back(fmt);
+ }
+#endif
sortedFmts = out;
}
@@ -1007,6 +1043,23 @@ void ExternalCameraDevice::initSupportedFormatsLocked(int fd) {
mCroppingType = VERTICAL;
}
}
+ /* mSupportedFormats has been sorted by size
+ remove the same size format */
+ std::vector<SupportedV4L2Format> tmp;
+ for (int i = 0; i < mSupportedFormats.size(); ) {
+ if ((mSupportedFormats[i+1].width == mSupportedFormats[i].width) &&
+ (mSupportedFormats[i+1].height == mSupportedFormats[i].height)) {
+ if (mSupportedFormats[i+1].maxFramerate > mSupportedFormats[i].maxFramerate)
+ tmp.push_back(mSupportedFormats[i+1]);
+ else
+ tmp.push_back(mSupportedFormats[i]);
+ i = i + 2;
+ } else {
+ tmp.push_back(mSupportedFormats[i]);
+ i++;
+ }
+ }
+ mSupportedFormats = tmp;
}
sp<ExternalCameraDeviceSession> ExternalCameraDevice::createSession(
diff --git a/camera/device/3.4/default/ExternalCameraUtils.cpp b/camera/device/3.4/default/ExternalCameraUtils.cpp
old mode 100644
new mode 100755
diff --git a/camera/device/3.4/default/include/ext_device_v3_4_impl/ExternalCameraUtils.h b/camera/device/3.4/default/include/ext_device_v3_4_impl/ExternalCameraUtils.h
old mode 100644
new mode 100755
index 341c62218..669a2bf68
--- a/camera/device/3.4/default/include/ext_device_v3_4_impl/ExternalCameraUtils.h
+++ b/camera/device/3.4/default/include/ext_device_v3_4_impl/ExternalCameraUtils.h
@@ -110,6 +110,7 @@ struct SupportedV4L2Format {
uint32_t durationDenominator; // frame duration denominator. Ex: 30
double getDouble() const; // FrameRate in double. Ex: 30.0
};
+ double maxFramerate; //该补丁若代码中无对应的地方,可修改同级文件ExternalCameraUtils_3.4.h 的相应位置是一样的
std::vector<FrameRate> frameRates;
};
--
2.17.1
此外,修改分辨率问题也可参考如下:
//显示更多拍照分辨率的 改应用代码里这个地方。
private static List<Size> pickUpToThree(List<Size> sizes) {
List<Size> result = new ArrayList<Size>();
Size largest = sizes.get(0);
if (largest.width() != 1920 || largest.height() != 1088)
result.add(largest);
Size lastSize = largest;
for (Size size : sizes) {
if (size != null && size.width() == 1920 && size.height() == 1088)
continue;
+ result.add(size);
- double targetArea = Math.pow(.5, result.size()) * area(largest);
+ /*double targetArea = Math.pow(.5, result.size()) * area(largest);
if (area(size) < targetArea) {
// This candidate is smaller than half the mega pixels of the
// last one. Let's see whether the previous size, or this size
// is closer to the desired target.
if (!result.contains(lastSize)
&& (targetArea - area(lastSize) < area(size) - targetArea)) {
result.add(lastSize);
} else {
result.add(size);
}
}
lastSize = size;
if (result.size() == 3) {
break;
}
}
// If we have less than three, we can add the smallest size.
if (result.size() < 3 && !result.contains(lastSize)) {
result.add(lastSize);
- }
+ }*/
return result;
}