Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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.Bitmap;
10import android.graphics.BitmapFactory;
11import android.graphics.ImageFormat;
12import android.graphics.Matrix;
13import android.graphics.SurfaceTexture;
14import android.util.Log;
15import java.lang.Math;
16import java.io.ByteArrayOutputStream;
17
18public class QtCameraListener implements Camera.ShutterCallback,
19 Camera.PictureCallback,
20 Camera.AutoFocusCallback,
21 Camera.PreviewCallback
22{
23 private static final String TAG = "Qt Camera";
24
25 private static final int BUFFER_POOL_SIZE = 2;
26
27 private int m_cameraId = -1;
28
29 private boolean m_notifyNewFrames = false;
30 private boolean m_notifyWhenFrameAvailable = false;
31 private byte[][] m_previewBuffers = null;
32 private byte[] m_lastPreviewBuffer = null;
33 private Camera.Size m_previewSize = null;
34 private int m_previewFormat = ImageFormat.NV21; // Default preview format on all devices
35 private int m_previewBytesPerLine = -1;
36 private int m_rotation = 0;
37
38 private QtCameraListener(int id)
39 {
40 m_cameraId = id;
41 }
42
43 public void notifyNewFrames(boolean notify)
44 {
45 m_notifyNewFrames = notify;
46 }
47
48 public void notifyWhenFrameAvailable(boolean notify)
49 {
50 m_notifyWhenFrameAvailable = notify;
51 }
52
53 public byte[] lastPreviewBuffer()
54 {
55 return m_lastPreviewBuffer;
56 }
57
58 public int previewWidth()
59 {
60 if (m_previewSize == null)
61 return -1;
62
63 return m_previewSize.width;
64 }
65
66 public int previewHeight()
67 {
68 if (m_previewSize == null)
69 return -1;
70
71 return m_previewSize.height;
72 }
73
74 public int previewFormat()
75 {
76 return m_previewFormat;
77 }
78
80 {
81 return m_previewBytesPerLine;
82 }
83
84 public void clearPreviewCallback(Camera camera)
85 {
86 camera.setPreviewCallbackWithBuffer(null);
87 }
88
89 public void setPhotoRotation(int rotation)
90 {
91 m_rotation = rotation;
92 }
93
94 public void setupPreviewCallback(Camera camera)
95 {
96 // Clear previous callback (also clears added buffers)
98 m_lastPreviewBuffer = null;
99
100 final Camera.Parameters params = camera.getParameters();
101 m_previewSize = params.getPreviewSize();
102 m_previewFormat = params.getPreviewFormat();
103
104 int bufferSizeNeeded = 0;
105 if (m_previewFormat == ImageFormat.YV12) {
106 // For YV12, bytes per line must be a multiple of 16
107 final int yStride = (int) Math.ceil(m_previewSize.width / 16.0) * 16;
108 final int uvStride = (int) Math.ceil((yStride / 2) / 16.0) * 16;
109 final int ySize = yStride * m_previewSize.height;
110 final int uvSize = uvStride * m_previewSize.height / 2;
111 bufferSizeNeeded = ySize + uvSize * 2;
112
113 m_previewBytesPerLine = yStride;
114
115 } else {
116 double bytesPerPixel = ImageFormat.getBitsPerPixel(m_previewFormat) / 8.0;
117 bufferSizeNeeded = (int) Math.ceil(bytesPerPixel * m_previewSize.width * m_previewSize.height);
118
119 // bytes per line are calculated only for the first plane
120 switch (m_previewFormat) {
121 case ImageFormat.NV21:
122 m_previewBytesPerLine = m_previewSize.width; // 1 byte per sample and tightly packed
123 break;
124 case ImageFormat.RGB_565:
125 case ImageFormat.YUY2:
126 m_previewBytesPerLine = m_previewSize.width * 2; // 2 bytes per pixel
127 break;
128 default:
129 m_previewBytesPerLine = -1;
130 break;
131 }
132 }
133
134 // We could keep the same buffers when they are already bigger than the required size
135 // but the Android doc says the size must match, so in doubt just replace them.
136 if (m_previewBuffers == null || m_previewBuffers[0].length != bufferSizeNeeded)
137 m_previewBuffers = new byte[BUFFER_POOL_SIZE][bufferSizeNeeded];
138
139 // Add callback and queue all buffers
140 camera.setPreviewCallbackWithBuffer(this);
141 for (byte[] buffer : m_previewBuffers)
142 camera.addCallbackBuffer(buffer);
143 }
144
145 @Override
146 public void onPreviewFrame(byte[] data, Camera camera)
147 {
148 // Re-enqueue the last buffer
149 if (m_lastPreviewBuffer != null)
150 camera.addCallbackBuffer(m_lastPreviewBuffer);
151
152 m_lastPreviewBuffer = data;
153
154 if (data != null) {
155 if (m_notifyWhenFrameAvailable) {
156 m_notifyWhenFrameAvailable = false;
157 notifyFrameAvailable(m_cameraId);
158 }
159 if (m_notifyNewFrames) {
160 notifyNewPreviewFrame(m_cameraId, data,
161 m_previewSize.width, m_previewSize.height,
162 m_previewFormat,
163 m_previewBytesPerLine);
164 }
165 }
166 }
167
168 @Override
169 public void onShutter()
170 {
171 notifyPictureExposed(m_cameraId);
172 }
173
174 @Override
175 public void onPictureTaken(byte[] data, Camera camera)
176 {
177 Camera.CameraInfo info = new Camera.CameraInfo();
178 Camera.getCameraInfo(m_cameraId, info);
179 Bitmap source = BitmapFactory.decodeByteArray(data, 0, data.length);
180 Matrix matrix = new Matrix();
181 matrix.postRotate(m_rotation);
182 if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
183 matrix.postScale(-1, 1, source.getWidth() / 2.0f, source.getHeight() / 2.0f);
184 }
185 Bitmap rotatedBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
186 source.getHeight(), matrix, true);
187 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
188 rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
189 byte[] byteArray = outputStream.toByteArray();
190 rotatedBitmap.recycle();
191 source.recycle();
192 notifyPictureCaptured(m_cameraId, byteArray);
193 }
194
195 @Override
196 public void onAutoFocus(boolean success, Camera camera)
197 {
198 notifyAutoFocusComplete(m_cameraId, success);
199 }
200
201 private static native void notifyAutoFocusComplete(int id, boolean success);
202 private static native void notifyPictureExposed(int id);
203 private static native void notifyPictureCaptured(int id, byte[] data);
204 private static native void notifyNewPreviewFrame(int id, byte[] data, int width, int height,
205 int pixelFormat, int bytesPerLine);
206 private static native void notifyFrameAvailable(int id);
207}
QCamera * camera
Definition camera.cpp:19
GLint GLsizei GLsizei height
GLenum GLuint GLenum GLsizei length
GLenum GLuint id
[7]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint buffer
GLint GLsizei width
GLsizei GLsizei GLchar * source
void ** params
GLuint GLenum matrix
QHostInfo info
[0]