简体   繁体   English

Android自定义相机setFocusAreas和setMeteringAreas在Samsung设备上不起作用

[英]Android custom camera setFocusAreas and setMeteringAreas not work on Samsung Devices

Maybe this question duplicate another question, but others i have not found a solution. 也许这个问题重复了另一个问题,但是其他我还没有找到解决方案。 I'm trying to write a custom android camera and the camera works fine on my device(HTC One).I tried some different devices, and works in one of them. 我正在尝试编写一个自定义的android相机,该相机在我的设备(HTC One)上运行良好。我尝试了一些不同的设备,并在其中一个设备上工作。 but I'm having problems on Samsung devices. 但我在Samsung设备上遇到问题。 Do not turn off the autofocus feature on the device. 不要关闭设备上的自动对焦功能。 Therefore, the device can not change the focus area. 因此,设备无法更改对焦区域。 That is my touch event. 那是我的触摸事件。

@Override
public boolean onTouchEvent(final MotionEvent event){

    Camera.Parameters cameraParameters = camera.getParameters();
    if (event.getAction() == MotionEvent.ACTION_UP){
        focusAreas.clear();
        meteringAreas.clear();
        Rect focusRect = calculateTapArea(event.getX(), event.getY(), 1f);
        Rect meteringRect = calculateTapArea(event.getX(), event.getY(), 1.5f);
        focusAreas.add(new Camera.Area(focusRect, 800));
        meteringAreas.add(new Camera.Area(meteringRect, 800));
        cameraParameters.setFocusAreas(focusAreas);
        cameraParameters.setMeteringAreas(meteringAreas);
        cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
        try{
            camera.setParameters(cameraParameters);

        } catch(Exception e){
            Log.e("Focus problem", e.toString());
            return false;
        }

        camera.autoFocus(new Camera.AutoFocusCallback() {                   
            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                camera.cancelAutoFocus();
                Camera.Parameters  params = camera.getParameters();
                if(params.getFocusMode() != Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE){
                    params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                    camera.setParameters(params);
                }
            }
        });

        focusSound = new MediaPlayer();
        showSquareFocus();
        try {
            AssetFileDescriptor descriptor = this.getApplicationContext().getAssets()
                    .openFd("focus.wav");
            focusSound.setDataSource(descriptor.getFileDescriptor(),
                    descriptor.getStartOffset(), descriptor.getLength());
            descriptor.close();
            focusSound.prepare();
            focusSound.setLooping(false);
            focusSound.start();
            focusSound.setVolume(10,10);
            focusSound.setOnCompletionListener(new OnCompletionListener(){
                public void onCompletion(MediaPlayer mp){
                    mp.release();
                  }
                });
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return true;
}

Maybe need this code... 可能需要此代码...

private Rect calculateTapArea(float x, float y, float coefficient) {
        int areaSize = Float.valueOf(FOCUS_AREA_SIZE * coefficient).intValue();
        int left = clamp((int) x - areaSize / 2, 0, width - areaSize);
        int top = clamp((int) y - areaSize / 2, 0, height - areaSize);
        RectF rectF = new RectF(left, top, left + areaSize, top + areaSize);
        matrix.mapRect(rectF);
        return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right), Math.round(rectF.bottom));
    }

    private int clamp(int x, int min, int max) {
        if (x > max) {
            return max;
        }
        if (x < min) {
            return min;
        }
        return x;
    }

I've experienced the same problem in a Samsung Galaxy S3. 我在三星Galaxy S3中遇到了相同的问题。 The solution I've found (even if its dirty) is to set an alternative focus mode before the desired one. 我发现的解决方案(即使脏了)是在所需的聚焦模式之前设置另一种聚焦模式。

private void setFocusMode(final Camera camera, final String newFocusMode)
{
    // Apply an alternative focus mode

    Camera.Parameters parameters = camera.getParameters();
    final List<String> focusModes = parameters.getSupportedFocusModes();

    if (focusModes.contains(Parameters.FOCUS_MODE_FIXED) && newFocusMode.compareToIgnoreCase(Parameters.FOCUS_MODE_FIXED) == false)
    {
        parameters.setFocusMode(Parameters.FOCUS_MODE_FIXED);
    }
    else if (focusModes.contains(Parameters.FOCUS_MODE_INFINITY) && newFocusMode.compareToIgnoreCase(Parameters.FOCUS_MODE_INFINITY) == false)
    {
        parameters.setFocusMode(Parameters.FOCUS_MODE_INFINITY);
    }
    else if (focusModes.contains(Parameters.FOCUS_MODE_MACRO) && newFocusMode.compareToIgnoreCase(Parameters.FOCUS_MODE_MACRO) == false)
    {
        parameters.setFocusMode(Parameters.FOCUS_MODE_MACRO);
    }
    else if (focusModes.contains(Parameters.FOCUS_MODE_AUTO) && newFocusMode.compareToIgnoreCase(Parameters.FOCUS_MODE_AUTO) == false)
    {
        parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
    }

    camera.setParameters(parameters);

    // Now apply the desired focus mode

    parameters = camera.getParameters();
    parameters.setFocusMode(newFocusMode);
    camera.setParameters(parameters);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM