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