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

时间:2019-04-29 05:05:26

标签: camera-api

我正在尝试制作相机应用程序,无法找到使用Camera.Parameters更改相机亮度和对比度的方法。 所以我的问题是如何添加“亮度和对比度”功能以增加/减少亮度/对比度。例如,如果我增加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;
    }
}

1 个答案:

答案 0 :(得分:1)

Camera.Parameters中没有方法可以实现这一目标。您必须为该类阅读此documentation才能检查可用的参数,并且在API 21及更高版本中不推荐使用该类。