使用 Camera.Parameters 调整亮度、对比度

Adjust Brightness, Contrast using Camera.Parameters

我正在尝试制作一个相机应用程序,但我找不到使用 Camera.Parameters 更改相机亮度和对比度的方法 所以我的问题是如何将亮度和对比度功能添加到 increase/decrease brightness/contrast。例如,如果我增加 seekbarit 增加亮度。如果我减少搜索栏,它会降低亮度。 请编辑我的代码或单独回答以帮助我。

package com.example.beautymaker;

import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.hardware.Camera;
import android.hardware.camera2.CameraCharacteristics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.zomato.photofilters.imageprocessors.Filter;
import com.zomato.photofilters.imageprocessors.subfilters.BrightnessSubFilter;

import java.io.IOException;
import java.util.List;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        mHolder = getHolder();
        mHolder.addCallback(this);
        /*Camera.Parameters params = camera.getParameters();
        final int[] previewFpsRange = new int[2];
        params.getPreviewFpsRange(previewFpsRange);
        if (previewFpsRange[0] == previewFpsRange[1]) {
            final List<int[]> supportedFpsRanges = params.getSupportedPreviewFpsRange();
            for (int[] range : supportedFpsRanges) {
                if (range[0] != range[1]) {
                    params.setPreviewFpsRange(range[0], range[1]);
                    break;
                }
            }
        }*/



        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            // create the surface and start camera preview
            if (mCamera == null) {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();


            }
        } catch (IOException e) {
            Log.d(VIEW_LOG_TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void refreshCamera(Camera camera) {
        if (mHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }
        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
        }
        // set preview size and make any resize, rotate or
        // reformatting changes here
        // start preview with new settings
        setCamera;
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
        } catch (Exception e) {
            Log.d(VIEW_LOG_TAG, "Error starting camera preview: " + e.getMessage());
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.
        refreshCamera(mCamera);
    }

    public void setCamera(Camera camera) {
        //method to set a camera instance

        mCamera = camera;
        Camera.Parameters parameters = mCamera.getParameters();


//        parameters.setPreviewFpsRange(1500,3000);

        parameters.setExposureCompensation(parameters.getMaxExposureCompensation());

        if(parameters.isAutoExposureLockSupported())
        {
            parameters.setAutoExposureLock(false);
        }

//        parameters.setColorEffect(Camera.Parameters.WHITE_BALANCE_INCANDESCENT);
        parameters.getAutoExposureLock();
        parameters.set("iso",50);
//                parameters.setWhiteBalance();
        parameters.setAutoWhiteBalanceLock(true);
        parameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_SHADE);



        /*Filter filter = new Filter();
        filter.addSubFilter(new BrightnessSubFilter(parameters));*/

        mCamera.setParameters(parameters);



    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        // mCamera.release();

    }

    //for brightness
    public static Bitmap doBrightness(Bitmap src, int value) {
        // image size
        int width = src.getWidth();
        int height = src.getHeight();
        // create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
        // color information
        int A, R, G, B;
        int pixel;

        // scan through all pixels
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                // get pixel color
                pixel = src.getPixel(x, y);
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);

                // increase/decrease each channel
                R += value;
                if(R > 255) { R = 255; }
                else if(R < 0) { R = 0; }

                G += value;
                if(G > 255) { G = 255; }
                else if(G < 0) { G = 0; }

                B += value;
                if(B > 255) { B = 255; }
                else if(B < 0) { B = 0; }

                // apply new pixel color to output bitmap
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }

        // return final image
        return bmOut;
    }
}

Camera.Parameters 中没有实现此目的的方法。您必须阅读此 documentation 以了解此 class 以检查可用参数,此 class 在 API 21 及更高版本中已弃用。