Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
QtCameraListener.java
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4package org.qtproject.qt.android.multimedia;
5
6import android.hardware.Camera;
7import android.hardware.Camera.CameraInfo;
8
9import android.graphics.ImageFormat;
10import android.graphics.SurfaceTexture;
11import android.util.Log;
12import java.lang.Math;
13
14public class QtCameraListener implements Camera.ShutterCallback,
15 Camera.PictureCallback,
16 Camera.AutoFocusCallback,
17 Camera.PreviewCallback
18{
19 private static final String TAG = "Qt Camera";
20
21 private static final int BUFFER_POOL_SIZE = 2;
22
23 private int m_cameraId = -1;
24
25 private boolean m_notifyNewFrames = false;
26 private boolean m_notifyWhenFrameAvailable = false;
27 private byte[][] m_previewBuffers = null;
28 private byte[] m_lastPreviewBuffer = null;
29 private Camera.Size m_previewSize = null;
30 private int m_previewFormat = ImageFormat.NV21; // Default preview format on all devices
31 private int m_previewBytesPerLine = -1;
32
33 private QtCameraListener(int id)
34 {
35 m_cameraId = id;
36 }
37
38 public void notifyNewFrames(boolean notify)
39 {
40 m_notifyNewFrames = notify;
41 }
42
43 public void notifyWhenFrameAvailable(boolean notify)
44 {
45 m_notifyWhenFrameAvailable = notify;
46 }
47
48 public byte[] lastPreviewBuffer()
49 {
50 return m_lastPreviewBuffer;
51 }
52
53 public int previewWidth()
54 {
55 if (m_previewSize == null)
56 return -1;
57
58 return m_previewSize.width;
59 }
60
61 public int previewHeight()
62 {
63 if (m_previewSize == null)
64 return -1;
65
66 return m_previewSize.height;
67 }
68
69 public int previewFormat()
70 {
71 return m_previewFormat;
72 }
73
75 {
76 return m_previewBytesPerLine;
77 }
78
79 public void clearPreviewCallback(Camera camera)
80 {
81 camera.setPreviewCallbackWithBuffer(null);
82 }
83
84 public void setupPreviewCallback(Camera camera)
85 {
86 // Clear previous callback (also clears added buffers)
88 m_lastPreviewBuffer = null;
89
90 final Camera.Parameters params = camera.getParameters();
91 m_previewSize = params.getPreviewSize();
92 m_previewFormat = params.getPreviewFormat();
93
94 int bufferSizeNeeded = 0;
95 if (m_previewFormat == ImageFormat.YV12) {
96 // For YV12, bytes per line must be a multiple of 16
97 final int yStride = (int) Math.ceil(m_previewSize.width / 16.0) * 16;
98 final int uvStride = (int) Math.ceil((yStride / 2) / 16.0) * 16;
99 final int ySize = yStride * m_previewSize.height;
100 final int uvSize = uvStride * m_previewSize.height / 2;
101 bufferSizeNeeded = ySize + uvSize * 2;
102
103 m_previewBytesPerLine = yStride;
104
105 } else {
106 double bytesPerPixel = ImageFormat.getBitsPerPixel(m_previewFormat) / 8.0;
107 bufferSizeNeeded = (int) Math.ceil(bytesPerPixel * m_previewSize.width * m_previewSize.height);
108
109 // bytes per line are calculated only for the first plane
110 switch (m_previewFormat) {
111 case ImageFormat.NV21:
112 m_previewBytesPerLine = m_previewSize.width; // 1 byte per sample and tightly packed
113 break;
114 case ImageFormat.RGB_565:
115 case ImageFormat.YUY2:
116 m_previewBytesPerLine = m_previewSize.width * 2; // 2 bytes per pixel
117 break;
118 default:
119 m_previewBytesPerLine = -1;
120 break;
121 }
122 }
123
124 // We could keep the same buffers when they are already bigger than the required size
125 // but the Android doc says the size must match, so in doubt just replace them.
126 if (m_previewBuffers == null || m_previewBuffers[0].length != bufferSizeNeeded)
127 m_previewBuffers = new byte[BUFFER_POOL_SIZE][bufferSizeNeeded];
128
129 // Add callback and queue all buffers
130 camera.setPreviewCallbackWithBuffer(this);
131 for (byte[] buffer : m_previewBuffers)
132 camera.addCallbackBuffer(buffer);
133 }
134
135 @Override
136 public void onPreviewFrame(byte[] data, Camera camera)
137 {
138 // Re-enqueue the last buffer
139 if (m_lastPreviewBuffer != null)
140 camera.addCallbackBuffer(m_lastPreviewBuffer);
141
142 m_lastPreviewBuffer = data;
143
144 if (data != null) {
145 if (m_notifyWhenFrameAvailable) {
146 m_notifyWhenFrameAvailable = false;
147 notifyFrameAvailable(m_cameraId);
148 }
149 if (m_notifyNewFrames) {
150 notifyNewPreviewFrame(m_cameraId, data,
151 m_previewSize.width, m_previewSize.height,
152 m_previewFormat,
153 m_previewBytesPerLine);
154 }
155 }
156 }
157
158 @Override
159 public void onShutter()
160 {
161 notifyPictureExposed(m_cameraId);
162 }
163
164 @Override
165 public void onPictureTaken(byte[] data, Camera camera)
166 {
167 notifyPictureCaptured(m_cameraId, data);
168 }
169
170 @Override
171 public void onAutoFocus(boolean success, Camera camera)
172 {
173 notifyAutoFocusComplete(m_cameraId, success);
174 }
175
176 private static native void notifyAutoFocusComplete(int id, boolean success);
177 private static native void notifyPictureExposed(int id);
178 private static native void notifyPictureCaptured(int id, byte[] data);
179 private static native void notifyNewPreviewFrame(int id, byte[] data, int width, int height,
180 int pixelFormat, int bytesPerLine);
181 private static native void notifyFrameAvailable(int id);
182}
QCamera * camera
Definition camera.cpp:19
GLint GLsizei GLsizei height
GLenum GLuint GLenum GLsizei length
GLenum GLuint id
[7]
GLenum GLuint buffer
GLint GLsizei width
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
void ** params