gphoto / libgphoto2

The libgphoto2 camera access and control library.
GNU Lesser General Public License v2.1
1.06k stars 327 forks source link

Official Android port of this library? #335

Closed laheller closed 1 year ago

laheller commented 5 years ago

An unofficial but quite old port exists, maybe it can help to make an official Android port: https://github.com/JohnChu/Snoopy

msmeissn commented 5 years ago

(FWIW) I currently lack the android knowledge

laheller commented 5 years ago

@msmeissn @hfiguiere @ndim

Maybe this way it might work: https://github.com/libusb/libusb/tree/master/android

As you can see, they just prepared jni/*.mk files (basically a tiny build files for Android builds). Everything else is described in their README. Using that I was able to build libusb for Android with just one single command from cmdline.

I will try the same or similar on libgphoto2 and will let you know.

Best Regards,

Ladislav

laheller commented 5 years ago

@msmeissn @hfiguiere @ndim

Update: As I wrote in first comment, there is one old unofficial port. It has an already prepared Android.mk build file: https://github.com/JohnChu/Snoopy/blob/master/jni/libgphoto2/Android.mk

Using the above build file an Android NDK I was able to build it successfully!

Now I am trying to bring it somehow to this repository.

laheller commented 5 years ago

@msmeissn

Can u pls send me the list off all external libraries/dependencies of gphoto2/libgphoto2 ? I mean like libusb, libjpeg, etc.

Thank you.

Ladislav

msmeissn commented 5 years ago

libusb1 - mandatory for USB access libtool libltdl - mandatory to load camera drivers

libjpeg - optional, only used by some drivers libexif - optional, used for EXIF extraction purposes libxml2 - optional, for Olympus E series libgd - optional for picture frame support

laheller commented 5 years ago

@msmeissn

Porting to Android seems a little bit more complex, because of the libusb1 dependency and Android's security. The libusb1 library however exists for Android, but because of Android's security mechanism the user must grant permission for each connected USB device manually. When this permission is not granted, the application can't access the usb device at /dev/bus/usb/*. There is a libusb1 port for Android, which workarounds the above problem by providing 2 more functions:

Once the permission on attached USB device was granted by user, the app can open the device, get the file descriptor and pass it to the libusb_open2( ) function.

So to summarize, I think to successfully port libgphoto2 to Android the USB-related source code might be slightly modified.

BR,

Ladislav

ndim commented 5 years ago

Have you checked whether loading *.so files via libltdl works on Android? If not, you have a significant amount of rewriting to do, both for libgphoto2 loading camera drivers and libgphoto2_port loading the port driver.

laheller commented 5 years ago

Have you checked whether loading *.so files via libltdl works on Android? If not, you have a significant amount of rewriting to do, both for libgphoto2 loading camera drivers and libgphoto2_port loading the port driver.

@ndim Not yet, this is the next step. But the libltdl is part of android source here, so I expect that it will work: https://android.googlesource.com/toolchain/gcc/+/refs/heads/master/gcc-4.9/libjava/libltdl/

BR,

Ladislav

lzyang2000 commented 5 years ago

Have you checked whether loading *.so files via libltdl works on Android? If not, you have a significant amount of rewriting to do, both for libgphoto2 loading camera drivers and libgphoto2_port loading the port driver.

@ndim Not yet, this is the next step. But the libltdl is part of android source here, so I expect that it will work: https://android.googlesource.com/toolchain/gcc/+/refs/heads/master/gcc-4.9/libjava/libltdl/

BR,

Ladislav

hello, has any progress been made on the porting? Trying to hook my Ricoh camera onto an android phone. On linux systems it seems that the ghphoto library is the most painless way and I have gotten it working, but I can't find much info about it on Android. Thanks!

laheller commented 5 years ago

Have you checked whether loading *.so files via libltdl works on Android? If not, you have a significant amount of rewriting to do, both for libgphoto2 loading camera drivers and libgphoto2_port loading the port driver.

@ndim Not yet, this is the next step. But the libltdl is part of android source here, so I expect that it will work: https://android.googlesource.com/toolchain/gcc/+/refs/heads/master/gcc-4.9/libjava/libltdl/ BR, Ladislav

hello, has any progress been made on the porting? Trying to hook my Ricoh camera onto an android phone. On linux systems it seems that the ghphoto library is the most painless way and I have gotten it working, but I can't find much info about it on Android. Thanks!

Fighting with libltdl, which is required...

lzyang2000 commented 5 years ago

Have you checked whether loading *.so files via libltdl works on Android? If not, you have a significant amount of rewriting to do, both for libgphoto2 loading camera drivers and libgphoto2_port loading the port driver.

@ndim Not yet, this is the next step. But the libltdl is part of android source here, so I expect that it will work: https://android.googlesource.com/toolchain/gcc/+/refs/heads/master/gcc-4.9/libjava/libltdl/ BR, Ladislav

hello, has any progress been made on the porting? Trying to hook my Ricoh camera onto an android phone. On linux systems it seems that the ghphoto library is the most painless way and I have gotten it working, but I can't find much info about it on Android. Thanks!

Fighting with libltdl, which is required...

Sad. I resorted to switching the wifi when connecting to the camera. Wish you luck on your work!

laheller commented 5 years ago

An intermediate status report: I was able to build libltdl for Android. Now I am going to try to modify libgphoto2 to be able to work with the above mentioned usb library.

laheller commented 5 years ago

@ndim @msmeissn

Hello

Guys, maybe a dumb question but just to be sure: To get the libgphoto2.so shared library I need to put together and build all the *.c source files from this repository (except maybe the ones under examples folder) ?

BR,

Ladislav

ndim commented 5 years ago

No.

Not having found such a description, I have added it to HACKING.md.

It certainly is impossible to just link all camlibs and iolibs into one library. Every camlib uses the same camlib set of symbol names, and all iolibs use the iolib set of symbol names. The dynamic loading of the correct camlib and iolib will make sure that libgphoto2 and libgphoto2_port call the proper implementation.

laheller commented 5 years ago

No.

Not having found such a description, I have added it to HACKING.md.

It certainly is impossible to just link all camlibs and iolibs into one library. Every camlib uses the same camlib set of symbol names, and all iolibs use the iolib set of symbol names. The dynamic loading of the correct camlib and iolib will make sure that libgphoto2 and libgphoto2_port call the proper implementation.

@ndim thanks, now it is clear for me the whole structure!

ndim commented 5 years ago

@laheller There should not have been significant changes to the architecture graphics at http://gphoto.github.io/doc/manual/developer-docs.html

laheller commented 5 years ago

@ndim @msmeissn

Slowly, but it goes well. Now I was able to build libgphoto2-port library. What is sometimes really hard to solve is the autoconf/automake which is not supported in Android NDK build system, so I need to manually create/edit config.h headers for some libraries to build them successfully.

This problem however can be workarounded in some but not all cases. For example this way I was able to automatically create config.h for ltdl, usb1, usb-compat, but it did not work for gphoto2-port.

I'll keep you updated on any progress/issues.

laheller commented 4 years ago

Update

As I told above, it goes slowly. Currently I am fighting with camera libraries.

daedeloth commented 4 years ago

Going to follow this as well :-)

kiwileaks78 commented 4 years ago

Very interested too :)

Morreski commented 4 years ago

Hi, Very interested by the android port. Will follow this with great interest.

thebino commented 4 years ago

@laheller Could you share your fork or project? I started building my own android integration und would like to merge our efforts. You can find mine here: https://github.com/thebino/libgphoto2android

laheller commented 4 years ago

@laheller Could you share your fork or project? I started building my own android integration und would like to merge our efforts. You can find mine here: https://github.com/thebino/libgphoto2android

Once I finish the camlibs, I will publish/share everything. Unfortunately I am working on another high prio project therefore I have less time for libgphoto2.

thebino commented 4 years ago

@laheller Okay, I'm also fine with a work-in-progress to help with the cam libs. I also invited you already to my project if this helps

DiegoBM commented 4 years ago

Any new updates? Thank you for the great work!

laheller commented 4 years ago

Any new updates? Thank you for the great work!

Temporarily stopped due to coronavirus and some personal things.

But in the meantime a new library for Android developers was released, which is available at: https://github.com/rupiapps/ptplibrary

I asked the author and he states it is compatible with the most Canon, Nikon and Sony cameras.

DiegoBM commented 4 years ago

Thank you for the information @laheller, that's a fantastic hint. I hope you (all of you in fact) are staying safe

develop-mbb commented 4 years ago

link is not working https://github.com/rupiapps/ptplibrary

hfiguiere commented 4 years ago

it seems to have moved there: https://github.com/laheller/ptplibrary

develop-mbb commented 4 years ago

I would like to control my Canon DSLR over the webbrowser from everywhere. I plan to connect the DSLR over USB to a android smartphone. Smartphone should connect to the internet over LTE. Which existing project would be the best for my setup?

Nailik commented 4 years ago

@develop-mbb maybe teamviewer and just remote control the smartphone would be the easiest way.

laheller commented 4 years ago

I would like to control my Canon DSLR over the webbrowser from everywhere. I plan to connect the DSLR over USB to a android smartphone. Smartphone should connect to the internet over LTE. Which existing project would be the best for my setup?

I would buy a Raspberry Pi 4, install Raspbian, TeamViewer and gphoto2 on it. Then connect DSLR via USB cable to RPI. Finally remote control RPI over TeamViewer. Easiest solution.

develop-mbb commented 4 years ago

I would like to control my Canon DSLR over the webbrowser from everywhere. I plan to connect the DSLR over USB to a android smartphone. Smartphone should connect to the internet over LTE. Which existing project would be the best for my setup?

I would buy a Raspberry Pi 4, install Raspbian, TeamViewer and gphoto2 on it. Then connect DSLR via USB cable to RPI. Finally remote control RPI over TeamViewer. Easiest solution.

Do you have a suggestion for a LTE-module for the Raspberry Pi 4? The performance of the internet connection will be important, because I want to transfer all the Photo-files in RAW-format.

daedeloth commented 4 years ago

I have built a rather simple shell that exposes a photo:takePicture method over a websocket connection, I built it to achieve a similar setup (raspberry pi connected to DSLR) to create a photobooth.

There isn't much documentation as I've hacked the code together over a few days, but if you have experience with nodejs I bet you can get it up and running in a couple of hours.

Nodejs websocket - gphoto2 listener https://github.com/CatLabInteractive/gphoto2-socketio

Photobooth app (php - laravel) https://github.com/CatLabInteractive/catlab-photobooth

Photobooth online version: http://photobooth.catlab.eu/photobooth (I was using an NFC scanner to link photos to attendees)

laheller commented 4 years ago

I would like to control my Canon DSLR over the webbrowser from everywhere. I plan to connect the DSLR over USB to a android smartphone. Smartphone should connect to the internet over LTE. Which existing project would be the best for my setup?

I would buy a Raspberry Pi 4, install Raspbian, TeamViewer and gphoto2 on it. Then connect DSLR via USB cable to RPI. Finally remote control RPI over TeamViewer. Easiest solution.

Do you have a suggestion for a LTE-module for the Raspberry Pi 4? The performance of the internet connection will be important, because I want to transfer all the Photo-files in RAW-format.

Why an LTE module? Share your smartphone's internet connection and that's it.

develop-mbb commented 4 years ago

I would like to control my Canon DSLR over the webbrowser from everywhere. I plan to connect the DSLR over USB to a android smartphone. Smartphone should connect to the internet over LTE. Which existing project would be the best for my setup?

I would buy a Raspberry Pi 4, install Raspbian, TeamViewer and gphoto2 on it. Then connect DSLR via USB cable to RPI. Finally remote control RPI over TeamViewer. Easiest solution.

Do you have a suggestion for a LTE-module for the Raspberry Pi 4? The performance of the internet connection will be important, because I want to transfer all the Photo-files in RAW-format.

Why an LTE module? Share your smartphone's internet connection and that's it.

The DSLR should be at a different place than the controller. At the location of the camera there will be no WLAN, only LTE/3G.

I think I found a good existing solution which I could upgrade to my needs.

Check out the following link to GPhoto2 Server: https://github.com/mvmn/gp2srv

laheller commented 4 years ago

Hi!

Update after a long time: I was able to build everything for Android (for the following ABIs: arm, aarch64), except some camera libraries. In the meantime I figured out that we don't need to write any special Android build scripts (MK files), becaues all libgphoto2 related projects, including dependencies like libltdl, libusb are GNU Autotools project and their configure script can be relatively easily re-used for Android build. More info here: https://developer.android.com/ndk/guides/other_build_systems#autoconf

Now I am thinking about a magic script which:

  1. Installs all possibly required packages, including android-ndk for Linux (Ubuntu)
  2. Clones all required git repositories
  3. Creates the required script in each repository which calls the corresponding autotools/configure script
  4. Starts build for each repository (libtool/libltdl first, libusb next, etc.)
  5. Collects all the generated shared library (.so files) and moves them into one output folder.

Any ideas are appreciated for better automation of the above.

huangmingyou commented 4 years ago

Hi!

Update after a long time: I was able to build everything for Android (for the following ABIs: arm, aarch64), except some camera libraries. In the meantime I figured out that we don't need to write any special Android build scripts (MK files), becaues all libgphoto2 related projects, including dependencies like libltdl, libusb are GNU Autotools project and their configure script can be relatively easily re-used for Android build. More info here: https://developer.android.com/ndk/guides/other_build_systems#autoconf

Now I am thinking about a magic script which:

  1. Installs all possibly required packages, including android-ndk for Linux (Ubuntu)
  2. Clones all required git repositories
  3. Creates the required script in each repository which calls the corresponding autotools/configure script
  4. Starts build for each repository (libtool/libltdl first, libusb next, etc.)
  5. Collects all the generated shared library (.so files) and moves them into one output folder.

Any ideas are appreciated for better automation of the above.

can share some documents.

websocketing commented 3 years ago

你好!

经过很长时间的更新: 除了一些相机库之外,我能够为Android(针对以下ABI:arm,aarch64)构建所有内容。 同时,我发现我们不需要编写任何特殊的Android构建脚本(MK文件),因为所有与libgphoto2相关的项目(包括libltdl,libusb等依赖项)都是GNU Autotools项目,并且它们的configure脚本可以相对轻松地重新用于Android构建。此处提供更多信息:https : //developer.android.com/ndk/guides/other_build_systems#autoconf

现在,我正在考虑一个魔术脚本,其中:

  1. 安装所有可能需要的软件包,包括适用于Linux的android-ndk(Ubuntu)
  2. 克隆所有必需的git存储库
  3. 在每个存储库中创建所需的脚本,该脚本调用相应的autotools / configure脚本
  4. 为每个存储库启动构建(首先为libtool / libltdl,其次为libusb,等等)
  5. 收集所有生成的共享库(.so文件),并将它们移动到一个输出文件夹中。

为了更好地实现上述目的,任何想法都应受到赞赏。

Is Android running normally now? Can we share open source materials?

thebino commented 3 years ago

@websocketing Your "magic" script could be solved by a cmake script running inside a github workflow. I've done it years ago but sadly not saved the results. Some parts can still be found in my github archive.

websocketing commented 3 years ago

@websocketing您的“魔术”脚本可以通过在github工作流中运行的cmake脚本来解决。我几年前就做过,但可惜没有保存结果。某些部分仍然可以在我的github档案中找到。

You're right https://github.com/thebino/libgphoto2android Unfortunately, there is no way to compile this open source. I hope you can give me some help. Thank you

websocketing commented 3 years ago

@websocketing Your "magic" script could be solved by a cmake script running inside a github workflow. I've done it years ago but sadly not saved the results. Some parts can still be found in my github archive.

Can your Facebook tell me, I hope I can communicate with you online, if there are other contact information

laheller commented 3 years ago

Sharing some screenshots from the first tests after successful build of gphoto2 and libgphoto2 for Android from sources. Built using Android NDK tools on Ubuntu.

Some info:

All the shared libraries and the gphoto2 cli in the same folder at /data/local/tmp/gphoto2: Screenshot_20210523-002016_Terminal Emulator.jpg

Result of the command gphoto2 --auto-detect: Screenshot_20210523-002136_Terminal Emulator.jpg

Result of the command gphoto2 -a: Screenshot_20210523-002244_Terminal Emulator.jpg

laheller commented 3 years ago

Hi community!

Almost 3 years ago I opened this issue, now after a lot of learning and experimenting I can tell that build libgphoto2 for Android is possible, but not the easisest thing. It has a lot of dependencies and of course we need to build all of them before even start to build libgphoto2.

Since the minimal required dependencies for libgphoto2 are among others:

I am not going to describe the whole process, because this is not the right place for that. Instead I will provide a sample, how to build the first dependency - libltdl - from sources for Android.

First of all we need to install some required packages, which on Ubuntu can be done by: sudo apt install git make google-android-ndk-installer libtool help2man texi2html autoconf automake texinfo

The next steps are similar to when we build an autoconf project from sources on Ubuntu:

Probably the easisest is when we put everything together in a single shell script and just run it:

#! /bin/sh

# in this example we are going to clone and build libtool/libltdl for Android
# this is the most important dependency for libgphoto2 and libgphoto2_port
REPO_NAME=libtool
REPO_URL=https://git.savannah.gnu.org/git/libtool.git

git clone --recursive ${REPO_URL} ${REPO_NAME}
cd ./${REPO_NAME}

# needed for some autoconf projects, might not work for some others
if [ -f ./bootstrap ]; then
    ./bootstrap
else
    autoreconf -i
fi

# build will happen in this dir
mkdir _build
cd _build

# by default the Android NDK root is at /usr/lib/android-ndk
export ANDROID_NDK=/usr/lib/android-ndk

# valid values: arm-linux-androideabi-4.9, aarch64-linux-android-4.9, x86-4.9, x86_64-4.9
export TOOLCHAIN=aarch64-linux-android-4.9

# valid values: arm-linux-androideabi, aarch64-linux-android, i686-linux-android, x86_64-linux-android
export CROSS_COMPILE=aarch64-linux-android

# valid value: android-XX, where XX is 9-24
export API=android-24

# valid values: arch-arm, arch-arm64, arch-x86, arch-x86_64
export ARCH=arch-arm64

export ANDROID_PREFIX=${ANDROID_NDK}/toolchains/${TOOLCHAIN}/prebuilt/linux-x86_64
export SYSROOT=${ANDROID_NDK}/platforms/${API}/${ARCH}
export CROSS_PATH=${ANDROID_PREFIX}/bin/${CROSS_COMPILE}

# settings for compiler
export AR=${CROSS_PATH}-ar
export AS=${CROSS_PATH}-as
export CC=${CROSS_PATH}-gcc
export CPP=${CROSS_PATH}-cpp
export CXX=${CROSS_PATH}-g++
export LD=${CROSS_PATH}-ld
export NM=${CROSS_PATH}-nm
export OBJDUMP=${CROSS_PATH}-objdump
export RANLIB=${CROSS_PATH}-ranlib
export READELF=${CROSS_PATH}-readelf
export STRIP=${CROSS_PATH}-strip

# set whatever target dir for installation, but do NOT set to /usr
export PREFIX=$(pwd)/../_deploy

# additional settings for compiler
# we can also specify additional compiler flags
export CFLAGS="${CFLAGS} --sysroot=${SYSROOT} -I${SYSROOT}/usr/include -fPIE -std=c99"
export CPPFLAGS="${CFLAGS}"

# we can also specify additional linker flags
export LDFLAGS="${LDFLAGS} --sysroot=${SYSROOT} -L${SYSROOT}/usr/lib -pie"

# run "../configure --help" to see all available command line switches, sometimes they must be tweaked
# here is the main difference when we build a project from sources for Android platform
../configure --host=${CROSS_COMPILE} --with-sysroot=${SYSROOT} --prefix=${PREFIX}

make

make install

echo "Architecture of the built library:"
readelf -a ${PREFIX}/lib/libltdl.so | grep Machine:

echo "Finished!"

The above script clones the source repository into ./libtool subfolder, switches to that folder, builds the library from sources and finally installs it to ./libtool/_deploy. In this example it's built for Android aarch64 platform.

With no or only minimal changes on the above script we can build all dependencies and libgphoto2 itself. Mostly we need to change CFLAGS, LDFLAGS, add new enviroment variables and call the configure script with various switches to enable/disable some features for given library. Running simply ./configure --help before the build might give some more info.

Happy compiling/linking/installing!

@msmeissn @ndim - we can close this issue

BR,

Ladislav

fahrion2600 commented 3 years ago

Hi @laheller!

Thank you for your efforts! Using your most recent comment, I have finally been able to build libgphoto2 and gphoto2 for Android as well. It was indeed tricky, but your script provided the key (as I'm a newbie to NDK myself). For now, I'm building just libtool, libusb-1.0, libexif, and libpopt dependencies. The rest should hopefully be straightforward.

I hope you don't mind, but I expanded on your script and encapsulated it along with the cross-compiling tools into a container image. It makes it much easier for me to keep things clean on my dev machine. If anybody is interested, I uploaded the scripts here: https://github.com/fahrion2600/gphoto2-build-scripts/tree/main/android

Along the way, I found there was a problem in how the NDK compiler treats empty variadic arguments which affected the ptp2 driver. If anybody else hits it, I have a potential fix here: https://github.com/fahrion2600/libgphoto2/commit/914553d71f02515b3c12acdca760aef2c92d0591

I hope this helps someone!

laheller commented 3 years ago

@fahrion2600

Great job! Unfortunately still the biggest problem is for Android its weird USB security. I already started to study the libgphoto2's source code but I am quite newbie to C language so for now have no idea, how to change it to make it properly work on Android (without root). Anyway it is possible to do that, because the latest libusb1.0 fully supports the Android USB workflow (see the issue #683 ).

fahrion2600 commented 3 years ago

Agreed, I have not had success yet connecting via USB without root. I tried following the instructions here, such as setting LIBUSB_OPTION_NO_DEVICE_DISCOVERY before libusb_init. However, I suspect disabling device discovery prevents USB device listing, which libgphoto appears to rely on in several places.

I am keeping an eye on https://github.com/libusb/libusb/pull/874 which might be an alternative, as it supposedly supports device listing. However, it is not yet merged.

Phobetor commented 1 year ago

@laheller

Unfortunately still the biggest problem is for Android its weird USB security.

Not sure if my question is out of scope: Is it possible to use WIFI to connect to the camera with this library on android?

laheller commented 1 year ago

@laheller

Unfortunately still the biggest problem is for Android its weird USB security.

Not sure if my question is out of scope: Is it possible to use WIFI to connect to the camera with this library on android?

@Phobetor Yes, it should be possible. But I would wait for the official build: https://github.com/gphoto/libgphoto2/pull/918

laheller commented 1 year ago

Official Android support now exists, see https://github.com/gphoto/libgphoto2/pull/918

Closing this thread.