Android自定义相机setFocusAreas和setMeteringAreas无法在Samsung Devices

时间:2015-07-30 07:51:42

标签: android camera samsung-mobile setfocus metering

也许这个问题与另一个问题重复,但其他人我还没有找到解决方案。我正在尝试编写一个自定义的Android摄像头,相机在我的设备(HTC One)上工作正常。我尝试了一些不同的设备,并在其中一个中工作。但我在Samsung设备上遇到了问题。请勿关闭设备上的自动对焦功能。因此,设备无法改变焦点区域。那是我的触摸事件。

@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;
}

也许需要这段代码......

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;
    }

1 个答案:

答案 0 :(得分:0)

我在三星Galaxy S3中遇到过同样的问题。 我发现的解决方案(即使它很脏)是在所需的解决方案之前设置另一种聚焦模式。

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);
}