我正在使用OnImageAvailableListener获取预览帧:
@Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
try {
image = reader.acquireLatestImage();
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
byte[] data = new byte[buffer.capacity()];
buffer.get(data);
//data.length=332803; width=3264; height=2448
Log.e(TAG, "data.length=" + data.length + "; width=" + image.getWidth() + "; height=" + image.getHeight());
//TODO data processing
} catch (Exception e) {
e.printStackTrace();
}
if (image != null) {
image.close();
}
}
Run Code Online (Sandbox Code Playgroud)
每次数据长度不同但图像宽度和高度相同.
主要问题:data.length对于3264x2448这样的分辨率来说太小了.
数据阵列的大小应为3264*2448 = 7,990,272,而不是300,000 - 600,000.
怎么了?
imageReader = ImageReader.newInstance(3264, …Run Code Online (Sandbox Code Playgroud) 我正在尝试从YUV_420_888图像获取Exif数据,但是它不起作用。我尝试了几种解决方案,例如将图像以jpeg格式保存到磁盘,将其转换为输入流,但似乎无济于事。
我使用Android camera2 API捕获YUV_420_888图像。然后在OnImageAvailableListener中获取图像,并尝试使用ExifInterface API读取其EXIF数据。但是它总是空的。我尝试了此链接中的所有方法以获得正确的字节数组。
这是我的代码:
@Override
public void onImageAvailable(ImageReader imageReader) {
if (!isRecording) {
return;
}
Image image = imageReader.acquireNextImage();
File file = Util.getImagePath(context);
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
outputStream.write(data);
//// This byte array I am making using all the approaches given in this link
/sf/ask/3081544371/
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
ExifInterface exifInterface = new …Run Code Online (Sandbox Code Playgroud) 我知道这个主题已经多次出现,但是没有一个解决方案实际上似乎有效,从适用于旧相机API的答案(YUV数据来自整齐的byte []数组),通过损坏的图像,到保存JPEG全部采用RenderScript的"绿色比例"(顺便提一下,这是我目前能做的最好的).
Camera2Basic示例的方式是它只是将Type.Builder的格式设置为JPEG.这个问题(如多篇文章中所讨论的)是它减慢了相机管道的速度.YUV_420_888的工作速度要快得多.
那么,有没有人设法执行正确的YUV_420_888 - > JPEG转换?
I want to be able to get the exact image for analysis step as I get in preview. I have preview use case:
val metrics = DisplayMetrics().also { binding.codeScannerView.display.getRealMetrics(it) }
val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio(screenAspectRatio)
}.build()
Run Code Online (Sandbox Code Playgroud)
And next to it I have analysis use case config:
val analyzerConfig = ImageAnalysisConfig.Builder().apply {
setTargetResolution(Size(metrics.heightPixels, metrics.widthPixels))
setTargetAspectRatio(screenAspectRatio)
val analyzerThread = HandlerThread(
"QrCodeReader").apply { start() }
setCallbackHandler(Handler(analyzerThread.looper))
setImageReaderMode(
ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
}.build()
Run Code Online (Sandbox Code Playgroud)
My preview is fullscreen so it's size is …