You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Alcatel 5033D设备CameraX分析用例无法达到720x1280分辨率问题求助

Fixing CameraX ImageAnalysis Resolution Fallback on Alcatel 5033D

Hey there, let's dig into this frustrating resolution issue with CameraX's ImageAnalysis on your Alcatel 5033D. It's great that you already confirmed the device supports 720x1280 via Camera2 and ImageCapture—so we know there's a way to make this work without a full Camera2 rewrite. Here are some targeted solutions to try:

1. Prioritize Aspect Ratio + Rotation Over Direct Resolution

CameraX's auto-resolution matching sometimes falls back to lower resolutions when you specify an exact size, especially on low-end devices. Instead, define the aspect ratio that matches your target (9:16 for 720x1280 portrait) and tie it to your display's rotation to ensure correct orientation:

private void initCameraAnalysis() { 
    Log.d(TAG, "initCameraAnalysis: "); 
    cameraAnalysis = new ImageAnalysis.Builder() 
        .setTargetAspectRatio(AspectRatio.RATIO_9_16) // Matches 720x1280 portrait
        .setTargetRotation(viewFinder.getDisplay().getRotation()) // Aligns with screen orientation
        .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) 
        .build(); 
    cameraAnalysis.setAnalyzer(cameraExecutor, new QualityAnalyzer()); 
}

This tells CameraX to pick the highest available resolution that fits your desired aspect ratio, which is more reliable than forcing an exact size directly.

2. Manually Select a Supported ImageAnalysis Resolution

CameraX might not be picking the right size automatically, so let's query the device's supported ImageAnalysis sizes explicitly and pick the one closest to 720x1280:

private void initCameraAnalysis() { 
    Log.d(TAG, "initCameraAnalysis: "); 

    CameraSelector cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA;
    CameraInfo cameraInfo = cameraProvider.getCameraInfo(cameraSelector);

    // Get all supported ImageAnalysis sizes
    Stream<Size> supportedSizes = cameraInfo.getStreamConfigurationMap()
        .getOutputSizes(ImageAnalysis.class);

    // Filter for portrait sizes (height > width) and find the closest to 720x1280
    Size targetSize = new Size(720, 1280);
    Size bestSize = supportedSizes
        .filter(size -> size.getHeight() > size.getWidth())
        .min(Comparator.comparingInt(size -> 
            Math.abs(size.getWidth() - targetSize.getWidth()) + 
            Math.abs(size.getHeight() - targetSize.getHeight())
        ))
        .orElse(targetSize);

    cameraAnalysis = new ImageAnalysis.Builder() 
        .setTargetResolution(bestSize) 
        .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) 
        .build(); 
    cameraAnalysis.setAnalyzer(cameraExecutor, new QualityAnalyzer()); 
}

This ensures you're only choosing a size the device explicitly supports for ImageAnalysis, eliminating any guesswork from CameraX's auto-selection.

3. Use Camera2 Interop to Force Camera2-Style Configuration

Since your colleague's Camera2 app works, we can leverage CameraX's Camera2Interop extension to directly set Camera2 capture parameters—this bypasses CameraX's higher-level logic and lets you replicate the working Camera2 setup:

First, add the Camera2 interop dependency if you haven't already (ensure it matches your CameraX version):

implementation "androidx.camera:camera-camera2:${camerax_version}"

Then adjust your ImageAnalysis initialization:

private void initCameraAnalysis() { 
    Log.d(TAG, "initCameraAnalysis: "); 

    ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
    Camera2Interop.Extender<ImageAnalysis.Builder> camera2Extender = new Camera2Interop.Extender<>(builder);

    Size targetSize = new Size(720, 1280);
    // Force the crop region to match our target resolution
    camera2Extender.setCaptureRequestOption(
        CaptureRequest.SCALER_CROP_REGION,
        new Rect(0, 0, targetSize.getWidth(), targetSize.getHeight())
    );

    // Set correct orientation to ensure portrait resolution is applied
    int jpegOrientation = getJpegOrientation(CameraSelector.DEFAULT_BACK_CAMERA, viewFinder.getDisplay());
    camera2Extender.setCaptureRequestOption(
        CaptureRequest.JPEG_ORIENTATION,
        jpegOrientation
    );

    cameraAnalysis = builder
        .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) 
        .build(); 
    cameraAnalysis.setAnalyzer(cameraExecutor, new QualityAnalyzer()); 
}

// Helper method to calculate correct JPEG orientation
private int getJpegOrientation(CameraSelector cameraSelector, Display display) {
    int deviceOrientation = display.getRotation();
    int jpegOrientation;

    switch (deviceOrientation) {
        case Surface.ROTATION_0:
            jpegOrientation = 90;
            break;
        case Surface.ROTATION_90:
            jpegOrientation = 0;
            break;
        case Surface.ROTATION_180:
            jpegOrientation = 270;
            break;
        case Surface.ROTATION_270:
            jpegOrientation = 180;
            break;
        default:
            jpegOrientation = 90;
    }

    // Adjust for front-facing camera if needed
    if (cameraSelector.getLensFacing() == CameraSelector.LENS_FACING_FRONT) {
        jpegOrientation = (360 - jpegOrientation) % 360;
    }

    return jpegOrientation;
}

This approach directly taps into Camera2's low-level controls, which should mirror the working configuration from your colleague's app. It's the most powerful fix here if the first two methods don't work.

Quick Check: Rule Out Use Case Conflicts

While you mentioned ImageCapture works, it's worth testing ImageAnalysis in isolation (temporarily remove ImageCapture/Preview use cases) to ensure there's no resource contention causing the resolution fallback. Low-end devices sometimes struggle to handle multiple high-resolution streams simultaneously.

Give these methods a try—start with the Camera2 Interop approach since it's the most direct way to replicate the working Camera2 behavior. You shouldn't need to rewrite the whole app!

内容的提问来源于stack exchange,提问作者Fran Marzoa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 20:34:07