Accessing the Android Camera in Qt
-
Hey All,
I'm trying to write an app with Qt for Android that grabs frames from the camera. I understand that there is some trickiness and that the best approach is to go either Java or OpenCV. I'm not sure how I'd incorporate Java into my code, and although I have most of OpenCV working I can't seem toget the native camera library to be recognized.
I think the reason for this is that the native camera is part of the OpenCV Manager, something that is loaded via a Java function. However, when looking at the Android .mk files used by OpenCV when compiling the sample programs I can see that it is loading the libnative_camera library. I tried adding that under ANDROID_EXTRA_LIBS as well as LIBS, but I got an undefined reference error:
Caused by: java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "_ZN7android6Camera10disconnectEv" referenced by "libnative_camera_r4.2.0.so"
I checked, and an objdump of the .so file in question shows the following:
00000000 DF UND 00000000 _ZN7android6Camera10disconnectEv
The UND implies that the function exists but is not defined. I hypothesize that it is defined within the OpenCV manager and called at runtime.
So, in closing, I need a way of using pure Qt to get at the Android camera. Java seems out of the question, OpenCV seems quite hairy (I may have to rebuild the Android OS, as seen here http://code.opencv.org/projects/opencv/wiki/Building_OpenCV4Android_from_trunk#Advanced-build-your-camera-wrapper).
is there a better way of doing this?
-
try $QTPATH\Examples\Qt-5.3\multimedia\declarative-camera
-
Wow! Err...Thank you very much. I was making things far too complicated...
Also thanks for prompting me to look at the examples, there's some pretty neat stuff in here. Qt is not to be underestimated.
-
Hey, I consider this answered, but as a follow up does anyone have some advice on porting the qml in that example to c++? I'm looking at the advice on the Camera overview page (http://qt-project.org/doc/qt-5/cameraoverview.html), but the code snippets there aren't enough to get my app up and grabbing frames. Here's what I've got:
@int main(int argc, char *argv[])
{
QApplication a(argc, argv);QCameraViewfinder * viewFinder; QCamera * camera;
/* This did not help
QByteArray cameraDevice;
foreach(const QByteArray &deviceName, QCamera::availableDevices()) {
if (cameraDevice.isEmpty())
cameraDevice = deviceName;
}
*/camera = new QCamera(); //(cameraDevice); viewFinder = new QCameraViewfinder; camera->setViewfinder(viewFinder); viewFinder->show(); camera->start(); return a.exec();
}@
I was getting an unable to connect error. That's gone away, but I'm still just seeing a blank screen. I feel like the code above is a bit too simple, but I don't know what other steps are required.
-
try $QTPATH\Examples\Qt-5.3\multimediawidgets\camera
-
At least now I don't have to feel completely crazy...the example loads but I see no video. When I press "Record" I get an error:
W/IMediaDeathNotifier(25594): media server died
W/CameraBase(25594): Camera service died!
W/CameraBase(25594): mediaserver's remote binder Camera object died
E/Camera (25594): Error 100'Under devices I can see the front facing camera being detected and if I toggle it I get an error. When I try and re-initialize it I see
W/CameraBase(28429): An error occurred while connecting to camera: 0
D/Qt Camera(28429): Fail to connect to camera service
W/Qt (28429): thread/qthread.cpp:425 (virtual QThread::~QThread()): QThread: Destroyed while thread is still runningIt could just be my device - I've heard there are some issues with the Nexus 7 camera, and I remember reading that the Front Facing camera is labelled 1 rather than 0, so it may be that. Thank you for the help. I'll play around with this example and maybe try it on another device if I can find one.
-
try to reboot your device first.
-
Hmm, I tried rebooting twice and made sure both times that camera was the only app running. I am deploying the app from Qt creator directly using Qt version 5.3, and as far as I know the example you suggested has not been modified.
-
I have a similar problem. I have wrote an application using declarative to watch my webcam. This application works on my pc but I obtain nothing in my mobile.
-
declarative actually worked out of the box for me, which is why I'm confused. The fairly simple QML code had no trouble finding the camera on my device, so I'm debating whether or not I should look at the source of the QML Camera object to see what's going on there.
I will also try and run the camera app on my desktop with a webcam just to verify that it works.
-
No luck on a Nexus 4...I must be doing something wrong. If I ever figure it out I'll report back.
edit: I assume nothing special needs to be done regarding Permissions in the Manifest; there is a comment in the generated file that seems to indicate Qt takes care of this based on the needs of your app.
-
https://bugreports.qt-project.org/browse/QTBUG-38942
bq. Using Qt Multimedia with QWidgets has never been really supported on Android. You'll have to stick to QML for now. We'll add support for QWidget in 5.4.0. https://codereview.qt-project.org/#change,85577 makes sure the device doesn't get into an undefined state when trying to take a picture without a valid viewport.
-
I see. Thank you very much for your help, Vincent. Perhaps I'll explore getting the VideoOutput from the QML Camera as a buffer somehow.
-
do u success?
i need it to
but i failed to convert the buffer to a QImage
[quote author="mynameisjohn" date="1401803722"]I see. Thank you very much for your help, Vincent. Perhaps I'll explore getting the VideoOutput from the QML Camera as a buffer somehow. [/quote] -
Hello!
How can I get video output from QML Camera?
I can get QMetaObject in C++ and get some properties of Camera, but there is no videoSurface or setViewfinder methods... I wonder, how QML Camera send data to VideoOutput??? -
Hello!
How can I get video output from QML Camera?
I can get QMetaObject in C++ and get some properties of Camera, but there is no videoSurface or setViewfinder methods... I wonder, how QML Camera send data to VideoOutput???