1

也许这个问题重复了另一个问题,但其他问题我还没有找到解决方案。我正在尝试编写一个自定义的 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;
    }
4

1 回答 1

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);
}
于 2015-07-31T17:30:29.177 回答