AlbumCameraRecorderX Documentation

repository·kotlin·Indexed 22 days ago

https://github.com/zhongjhatc/albumcamerarecorder

An efficient Android multimedia library for photo taking, video recording, audio recording, and album access. It features customizable UIs, dynamic watermarks, and modular components for image editing (ImageCustom), video compression (via ffmpeg), and grid displays (PhotoAdapterEntity). The library provides a centralized configuration via MultiMediaSetting to manage CameraSetting, AlbumSetting, and RecorderSetting.

Tokens
9K
Snippets
24
Records
30
Agent score
77%

What's inside AlbumCameraRecorderX

  1. Configure and launch multimedia functions

    kotlin

    You can configure camera, album, and audio recording settings independently and then combine them using MultiMediaSetting.

    • CameraSetting: Configure mime types (e.g., MimeType.ofAll()).
    • AlbumSetting: Configure mime types, enable/disable item counting, add custom filters (like GifSizeFilter), and enable original image selection with size limits.
    • RecorderSetting: Configure audio recording parameters.
    • MultiMediaSetting: The central configuration object. Use .from(context) to initialize, then chain .albumSetting(), .cameraSetting(), and .recorderSetting() to enable specific features. You can also configure the image engine (e.g., Glide4Engine) and set maximum selectable counts per media type.

    Use .forResult(requestLauncher) to launch the configured multimedia interface.

    // 1. Configure Camera
    CameraSetting cameraSetting = new CameraSetting();
    cameraSetting.mimeTypeSet(MimeType.ofAll());
    
    // 2. Configure Album
    AlbumSetting albumSetting = new AlbumSetting(false)
            .mimeTypeSet(MimeType.ofAll())
            .countable(true)
            .addFilter(new GifSizeFilter(320, 320, 5 * BaseFilter.K * BaseFilter.K))
            .originalEnable(true)
            .maxOriginalSize(10);
    
    // 3. Configure Recorder
    RecorderSetting recorderSetting = new RecorderSetting();
    
    // 4. Combine and Launch
    MultiMediaSetting mGlobalSetting = MultiMediaSetting.from(context)
            .choose(MimeType.ofAll())
            .albumSetting(albumSetting)
            .cameraSetting(cameraSetting)
            .recorderSetting(recorderSetting)
            .imageEngine(new Glide4Engine())
            .maxSelectablePerMediaType(null, MAX_IMAGE, MAX_VIDEO, MAX_AUDIO, imgCount, vidCount, audCount)
            .forResult(requestLauncherACR);
  2. Install AlbumCameraRecorderX

    kotlin

    To use AlbumCameraRecorderX, follow these three steps to add the repository, dependencies, and required Gradle configurations.

    1. Add JitPack Repository

    Add the JitPack repository to your allprojects block in your build file.

    2. Add Dependencies

    You can choose between a single combined library or individual modules depending on your needs.

    • Combined Library: Includes multilibrary, grid, and albumCameraRecorderCommon.
    • Individual Modules:
      • common: Public base library.
      • multilibrary: Core library for album, recording, and audio.
      • grid: For displaying media in a grid view.
      • imageedit: For image editing features.
      • videoedit: For video compression (requires ffmpeg, adds ~25MB).

    3. Gradle Configuration

    Ensure your gradle.properties includes the following to support AndroidX and Jetifier:

    android.enableJetifier=true
    android.useAndroidX=true
    // Step 1: Add JitPack
    allprojects {
        repositories {
            ...
            maven { url 'https://www.jitpack.io' }
        }
    }
    
    // Step 2: Add dependencies
    dependencies {
         // Combined library (recommended for most)
         implementation 'com.github.zhongjhATC.AlbumCameraRecorder:combined:2028'
    
         // OR individual modules
         implementation 'com.github.zhongjhATC.AlbumCameraRecorder:common:2028'
         implementation 'com.github.zhongjhATC.AlbumCameraRecorder:multilibrary:2028'
         implementation 'com.github.zhongjhATC.AlbumCameraRecorder:grid:2028'
         implementation 'com.github.zhongjhATC.AlbumCameraRecorder:imageedit:2028'
         implementation 'com.github.zhongjhATC.AlbumCameraRecorder:videoedit:2028'
    }
  3. Configure and Launch Multimedia Features

    kotlin

    AlbumCameraRecorderX uses a hierarchical configuration pattern. You define specific settings for the Camera, Album, and Recorder, then wrap them in a MultiMediaSetting object to launch the activity.

    Configuration Workflow

    1. CameraSetting: Configure mime types (e.g., MimeType.ofAll()).
    2. AlbumSetting: Configure mime types, selection limits, filters (like GifSizeFilter), and original image settings (originalEnable, maxOriginalSize).
    3. RecorderSetting: Configure audio recording parameters.
    4. MultiMediaSetting: The global entry point. Use .from(context) to initialize, then chain .albumSetting(), .cameraSetting(), and .recorderSetting() to enable specific features. You can also configure the image engine (e.g., Glide4Engine) and selection limits per media type.
    5. Launch: Call .forResult(requestLauncher) to start the activity and handle the result via ActivityResultLauncher.
    // 1. Configure Camera
    CameraSetting cameraSetting = new CameraSetting();
    cameraSetting.mimeTypeSet(MimeType.ofAll());
    
    // 2. Configure Album
    AlbumSetting albumSetting = new AlbumSetting(false)
            .mimeTypeSet(MimeType.ofAll())
            .countable(true)
            .addFilter(new GifSizeFilter(320, 320, 5 * BaseFilter.K * BaseFilter.K))
            .originalEnable(true)
            .maxOriginalSize(10);
    
    // 3. Configure Recorder
    RecorderSetting recorderSetting = new RecorderSetting();
    
    // 4. Global Configuration and Launch
    MultiMediaSetting mGlobalSetting = MultiMediaSetting.from(this)
            .choose(MimeType.ofAll())
            .albumSetting(albumSetting)
            .cameraSetting(cameraSetting)
            .recorderSetting(recorderSetting)
            .imageEngine(new Glide4Engine())
            .maxSelectablePerMediaType(null, MAX_IMAGE, MAX_VIDEO, MAX_AUDIO, alreadyImg, alreadyVid, alreadyAud)
            .forResult(requestLauncherACR);
    
    // 5. Handle Result
    protected final ActivityResultLauncher<Intent> requestLauncherACR = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(), 
        result -> {
            if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                List<LocalMedia> data = MultiMediaSetting.obtainLocalMediaResult(result.getData());
                // Use the data
            }
        }
    );
  4. Perform image cropping

    kotlin

    Cropping is a multi-step process involving the ImageClipWindow:

    1. Set mode = ImageMode.CLIP.
    2. Use onScroll and onTouch events to manipulate the crop area.
    3. Call clip(scrollX, scrollY) to finalize the crop. This returns an ImageHoming object containing the new scroll, scale, and rotation required to align the view with the new cropped area.

    To cancel a crop and return to the previous state, use toBackupClip() or resetClip().

    // Finalize the crop and get the new view state
    val homing = imageCustom.clip(currentScrollX, currentScrollY)
    // Use homing.x, homing.y, homing.scale, and homing.rotate to update your view
  5. Retrieve Selected Media Data

    kotlin

    When the multimedia activity returns a successful result, use the static method MultiMediaSetting.obtainLocalMediaResult(Intent data) to parse the intent and retrieve a list of LocalMedia objects. This list contains the selected images, videos, or audio files.

    List<LocalMedia> data = MultiMediaSetting.obtainLocalMediaResult(result.getData());
  6. Configure the PhotoAdapterEntity for GridView

    kotlin

    The PhotoAdapterEntity is a configuration object used to initialize a photo adapter within a GridView. It encapsulates all the necessary parameters to handle image loading, placeholders, media limits, and UI elements like masking and deletion icons. Because the adapter constructor requires many parameters, you should instantiate this entity and set its properties before passing it to the adapter.

    Configuration Properties

    PropertyTypeDescription
    imageEngineImageEngineThe engine used for loading images (compatible with various image loading libraries).
    placeholderDrawableThe drawable to display while an image is loading.
    isOperationBooleanFlag indicating if the adapter is in operation mode.
    maxMediaCountIntThe maximum number of media items (images/videos/audio) to display.
    maskingMaskingConfiguration for the masking layer.
    deleteColorIntThe color of the delete icon.
    deleteImageDrawable?An optional custom drawable for the delete icon.
    addDrawableDrawable?An optional drawable representing the 'add' resource.
    val entity = PhotoAdapterEntity().apply {
        imageEngine = myImageEngine
        placeholder = myPlaceholderDrawable
        isOperation = true
        maxMediaCount = 10
        masking = myMaskingConfig
        deleteColor = Color.RED
        // Optional properties
        deleteImage = myDeleteIcon
        addDrawable = myAddIcon
    }
  7. Library Module Reference

    kotlin

    The library is modularized. Depending on your requirements, you can import specific components:

    ModuleDescription
    combinedIncludes multilibrary, grid, and albumCameraRecorderCommon
    commonBase common library
    multilibraryCore library for album, camera, and audio recording
    gridSupplement for displaying content and upload progress
    imageeditImage editing features
    videoeditVideo compression (uses ffmpeg, ~25MB)
  8. Manage sticker lifecycle with ImageStickerHelper

    kotlin

    ImageStickerHelper is a generic helper class used to manage the lifecycle and behavior of sticker views. It implements ImageStickerPortrait and ImageStickerPortrait.Callback to provide unified management of sticker state changes (showing, hiding, removing, and dismissing).

    Requirements

    The generic type StickerView must satisfy two constraints:

    1. It must be a subclass of android.view.View.
    2. It must implement the ImageSticker interface.

    Core Functionality

    • Displaying: Use show() to set the sticker to a visible state. This triggers onShowing and invalidates the view.
    • Removing: Use remove() to attempt to remove the sticker. This delegates the decision to the registered callback via onRemove.
    • Dismissing: Use dismiss() to hide the sticker. This clears the calculated frame, invalidates the view, and triggers onDismiss.
    • State Tracking: Use isShowing() to check if the sticker is currently visible.
    • Boundary Calculation: Use getFrame() to retrieve the RectF representing the sticker's boundary in the view coordinate system. The frame is calculated using a Matrix that accounts for the view's position, size, and scale (pivot-based).
    // Example usage concept
    val stickerHelper = ImageStickerHelper(myStickerView)
    
    // Register a callback to handle lifecycle events
    stickerHelper.registerCallback(object : ImageStickerPortrait.Callback {
        override fun onShowing(stickerView: View) { /* Handle showing */ }
        override fun onDismiss(stickerView: View) { /* Handle dismissal */ }
        override fun onRemove(stickerView: View): Boolean = true // Return true to allow removal
    }
    )
    
    // Control the sticker
    stickerHelper.show()
    stickerHelper.dismiss()
    
    // Get the boundary for drawing or collision detection
    val frame = stickerHelper.getFrame()
  9. Use ImageClipWindow for image clipping UI

    kotlin

    The ImageClipWindow class manages the drawing, interaction, and state of an image cropping interface. It handles the visual representation of the crop frame (including grid lines, borders, and corner handles) and processes user touch interactions like dragging corners or scrolling the frame.

    Key responsibilities include:

    • State Management: Tracks if the user is currently clipping (isClipping), if the window is resetting (isResetting), or if a centering animation is playing (isHoming).
    • Visual Rendering: Provides an onDraw(canvas) method to render the crop area, grid lines, and corner handles.
    • Interaction: Identifies which corner handle is being touched via getAnchor(x, y) and updates the frame position via onScroll(anchor, dx, dy).
    // Example conceptual usage in a custom View
    class MyClipView(context: Context) : View(context) {
        private val clipWindow = ImageClipWindow()
    
        override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
            clipWindow.setClipWinSize(w.toFloat(), h.toFloat())
        }
    
        override fun onDraw(canvas: Canvas) {
            clipWindow.onDraw(canvas)
        }
    
        override fun onTouchEvent(event: MotionEvent): Boolean {
            val x = event.x
            val y = event.y
            // Logic to handle getAnchor and onScroll based on touch events
            return true
        }
    }
  10. Add doodles and mosaic paths

    kotlin

    To add drawing or mosaic effects, use addPath(path, sx, sy). This method transforms the path from view coordinates to the image's coordinate system based on the current scroll (sx, sy) and scale.

    • If the path's mode is ImageMode.DOODLE, it is added to the doodle collection.
    • If the path's mode is ImageMode.MOSAIC, it is added to the mosaic collection.

    To undo the last operation, use undoDoodle() or undoMosaic().

    // Add a doodle path
    imageCustom.addPath(myDoodlePath, scrollX, scrollY)
    
    // Undo last doodle
    imageCustom.undoDoodle()
    
    // Undo last mosaic
    imageCustom.undoMosaic()
  11. Transform image scale and rotation

    kotlin

    You can manipulate the image's visual state using:

    • rotate(rotate: Int): Rotates the image by the specified degree increment (typically 90-degree steps).
    • onScale(factor: Float, focusX: Float, focusY: Float): Scales the image around a specific focal point. The factor is relative to the current scale.
    • scale (property): Get or set the current scale relative to the image's original width.
    // Rotate the image by 90 degrees
    imageCustom.rotate(90)
    
    // Scale the image by a factor of 2.0 at a specific point
    imageCustom.onScale(2.0f, centerX, centerY)