AUSBC - Android USB Camera Engine

repository·master·Indexed 25 days ago

https://github.com/jiangdongguo/androidusbcamera

A flexible UVC camera engine for Android that supports multiple USB cameras, high-resolution previews, and media capture (photo, video, audio) without requiring system permissions on OTG-supported devices. The library includes components for OpenGL ES filters via AbstractEffect, multi-road camera support through MultiCameraFragment, and integrates libuvc, libjpeg-turbo 1.5.0, and RapidJSON.

Tokens
25K
Snippets
51
Records
137
Agent score
83%

What's inside AUSBC

  1. Overview of RapidJSON stream types

    master

    RapidJSON provides several specialized stream classes to optimize JSON parsing and generation:

    • Memory Streams: Simple streams for handling data in memory.
    • File Streams: Reduces memory footprint by reading/writing directly to the file system (FileReadStream for input, FileWriteStream for output).
    • Encoded Streams: Handles conversion between byte streams and character streams (e.g., EncodedInputStream, EncodedOutputStream, AutoUTFInputStream, AutoUTFOutputStream).
    • Custom Streams: Users can implement their own stream interfaces for specialized requirements.
  2. Overview of JsonCpp

    master
    JsonCpp is a simple API for manipulating JSON values and handling serialization and unserialization to strings. It supports representing integers, real numbers, strings, ordered sequences, and name/value pairs. A key feature is its ability to preserve existing comments during the unserialization/serialization process, making it suitable for storing user input files. It also provides precise error reports during parsing.
  3. Choose between DOM and SAX API styles

    master

    RapidJSON provides two primary API styles depending on your performance and ease-of-use requirements:

    1. DOM (Document Object Model): Uses rapidjson::GenericDocument. It parses JSON into a tree structure for easy manipulation and stringification. It is easier to use but has more memory overhead than SAX.
    2. SAX (Simple API for XML): Uses rapidjson::GenericReader (event-based parser) and rapidjson::Writer (generator). This is a sequential access API that is faster and more memory-efficient than DOM.
  4. Open multi-road cameras

    master

    To support multiple USB cameras simultaneously, extend MultiCameraFragment or MultiCameraActivity and implement the camera connection callbacks.

    class DemoMultiCameraFragment : MultiCameraFragment(), ICameraStateCallBack {
    
        override fun onCameraConnected(camera: MultiCameraClient.Camera) {
            camera.openCamera(textureView, getCameraRequest())
            camera.setCameraStateCallBack(this)
        }
    
        override fun onCameraState(
            self: MultiCameraClient.Camera, 
            code: ICameraStateCallBack.State, 
            msg: String?)
        {
            when (code) {
                ICameraStateCallBack.State.OPENED -> handleCameraOpened()
                ICameraStateCallBack.State.CLOSED -> handleCameraClosed()
                ICameraStateCallBack.State.ERROR -> handleCameraError()
            }
        }
    
        override fun getRootView(inflater: LayoutInflater, container: ViewGroup?): View {
            return rootView
        }
    }
  5. Install AUSBC via JitPack

    master

    To use the AUSBC library, add the JitPack repository to your project's root build.gradle or settings.gradle and then add the dependency to your app-level build.gradle.

    // 1. Add to root build.gradle or settings.gradle
    allprojects {
        repositories {
            google()
            jcenter()
            maven { url "https://jitpack.io" }
        }
    }
    
    // 2. Add to app/build.gradle
    dependencies {
        implementation 'com.github.jiangdongguo.AndroidUSBCamera:libausbc:latest_tag'
    }
  6. Build libjpeg-turbo for Android

    master

    Building for Android requires the Android NDK and autotools. You must set up the toolchain paths and sysroots based on your NDK installation and the target Android API level.

    General Recipe:

    1. Define NDK_PATH, BUILD_PLATFORM, TOOLCHAIN_VERSION, and ANDROID_VERSION.
    2. Set up HOST, SYSROOT, and ANDROID_CFLAGS (e.g., for ARMv7 or ARMv8).
    3. Export toolchain binaries (CC, AR, AS, etc.) to the environment.
    4. Run configure with --host and appropriate CFLAGS/LDFLAGS.
    5. Run make.

    Note: For Android 4.0.x (API < 16), remove -fPIE from CFLAGS and -pie from LDFLAGS.

    # Example setup snippet for 64-bit ARMv8
    HOST=aarch64-linux-android
    SYSROOT=${NDK_PATH}/platforms/android-${ANDROID_VERSION}/arch-arm64
    ANDROID_CFLAGS="--sysroot=${SYSROOT}"
    
    # ... (export toolchain binaries) ...
    
    cd {build_directory}
    sh {source_directory}/configure --host=${HOST} \
      CFLAGS="${ANDROID_INCLUDES} ${ANDROID_CFLAGS} -O3 -fPIE" \
      CPPFLAGS="${ANDROID_INCLUDES} ${ANDROID_CFLAGS}" \
      LDFLAGS="${ANDROID_CFLAGS} -pie" --with-simd
    make
  7. Build JsonCpp using Scons

    master

    JsonCpp uses Scons as its build system and requires Python to be installed. To build, follow these steps:

    1. Download the scons-local distribution and unzip it in the directory containing the README.txt file (ensure scons.py is at the same level as the README).
    2. Run the build command using the following syntax: python scons.py platform=PLTFRM [TARGET]

    Available Platforms (PLTFRM):

    • suncc: Sun C++ (Solaris)
    • vacpp: Visual Age C++ (AIX)
    • mingw
    • msvc6: Microsoft Visual Studio 6 (SP5-6)
    • msvc70: Microsoft Visual Studio 2002
    • msvc71: Microsoft Visual Studio 2003
    • msvc80: Microsoft Visual Studio 2005
    • linux-gcc: Gnu C++ (Linux/Mac OS X)

    Available Targets (TARGET):

    • check: Builds the library and runs unit tests.
  8. Use DOM as a SAX Event Publisher

    master

    In RapidJSON, the Value::Accept() method is used to publish SAX events about a DOM value to a handler. This decouples the Value (the data) from the Writer (the handler).

    This pattern allows you to create custom handlers to transform a DOM into other formats, such as converting JSON DOM into XML. To stringify a DOM using a Writer, you call Accept() on the DOM value.

    // Stringifying a DOM with a Writer
    Writer<StringBuffer> writer(buffer);
    d.Accept(writer);
  9. Implement custom OpenGL ES filters

    master

    You can add custom visual effects by extending AbstractEffect. You must provide the vertex and fragment shader resource IDs.

    class EffectBlackWhite(ctx: Context) : AbstractEffect(ctx) {
        override fun getId(): Int = ID
        override fun getClassifyId(): Int = CameraEffect.CLASSIFY_ID_FILTER
        override fun getVertexSourceId(): Int = R.raw.base_vertex
        override fun getFragmentSourceId(): Int = R.raw.effect_blackw_fragment
    
        companion object {
            const val ID = 100
        }
    }
    
    // Usage
    addRenderEffect(effect)
    removeRenderEffect(effect)
    updateRenderEffect(classifyId, effect)
  10. Run JsonCpp tests manually

    master

    Navigate to the test directory to run various test suites manually.

    Reader/Writer Tests: Run these using runjsontests.py and provide the path to the test executable: python runjsontests.py "path to jsontest.exe"

    To use the JSONChecker test suite: python runjsontests.py --with-json-checker "path to jsontest.exe"

    Unit Tests (Value tests): Run these using rununittests.py: python rununittests.py "path to test_lib_json.exe"

    To run unit tests with Valgrind: python rununittests.py --valgrind "path to test_lib_json.exe"

    cd test
    python runjsontests.py "path to jsontest.exe"