camera-api

Adjust Brightness, Contrast using Camera.Parameters


I were trying to make a camera application, I'm unable to find a way to change camera brightness, contrast using Camera.Parameters So my question is how to add Brightness and contrast feature to increase/decrease brightness/contrast. For example if I increase the seekbarit increase the brightness. if I decrease the seekbar it decrease the brightness. Please edit my code or put your seprate answer to help me.

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

Solution

  • there is no method in Camera.Parameters to achieve this. You have to read this documentation for this class to check the available parameters and this class is deprecated in API 21 and above.