Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColorEffect not visible on camera preview

I am trying to get a camera preview with a color effect applied to it, such as for example the NEGATIVE effect. There are no errors, and the preview is visible without problems, but independent of the ColorEffect I set - the camera preview remains unchanged. I tested if the effects I am trying to use are available to my phone by running params.getSupportedColorEffects() (also these effects also work in the built in photo app).

I have no idea what is wrong with the code - I am posting it below. Perhaps someone here has an idea what could make this work? Thanks in advance.

public class CustomCameraView extends SurfaceView{

Camera mCamera;
SurfaceHolder mHolder;

public CustomCameraView(Context context){
    super(context);
    mHolder = this.getHolder();
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mHolder.addCallback(mSurfaceHolderListener);
}

SurfaceHolder.Callback mSurfaceHolderListener = new SurfaceHolder.Callback() {
    public void surfaceCreated(SurfaceHolder holder) {
        mCamera=Camera.open();
        try {
            mCamera.setPreviewDisplay(mHolder);
        }
        catch (Exception e){ }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height)
    {
        Camera.Parameters params = mCamera.getParameters();
        params.setColorEffect(Camera.Parameters.EFFECT_NEGATIVE);
        mCamera.setParameters(params);
        mCamera.startPreview();
    }

    public void surfaceDestroyed(SurfaceHolder arg0)
    {
        mCamera.stopPreview();
        mCamera.release();   
    }
};
}

After some testing it turned out the problem could be related to the HTC Desire I was testing on (or maybe its OS version). The code works correctly on some other Samsung phones. I haven't figured out what could be the problem on the HTC.


UPDATE: I have managed to get the effects working, but truly by accident, and I still don't understand why. But I will give the answer here - perhaps someone will find it useful, or maybe will be able to explain why it happens this way:

I added the following line to the surfaceChanged method because I was trying to decrease the size of the preview:

previewHolder.setFixedSize(width, height-1); 

This had the result of making the selected effect visible.

When I changed this line to:

previewHolder.setFixedSize(width, height); 

the effect was not visible any more once again. So odd.... it works for set height being anything less than the received height parameter.

like image 597
morrwing Avatar asked Nov 14 '22 10:11

morrwing


1 Answers

I have been struggling with this as well. I found out that the HTC Desire its camera needs a strange order of executing the setParameters, setPreviewDisplay and startPreview for the color effect to work. The order is:

Camera.Parameters parameters = camera.getParameters();

//set the parameters

camera.setParameters(parameters);
camera.startPreview();
camera.setParameters(parameters);
camera.setPreviewDisplay(surfaceHolder);

Calling startPreview before setPreviewDisplay is documented in the Android SDK as a way of initializing the camera and the surfaceView in parallel.

Regarding your update about getting the effects to work by accident, the same happend to me! I assume for the same reason, some of my code got called twice in quick succesion (in my case due to a changing database object). This caused the method to (re)set the parameters and (re)start the preview to be called twice producing the desired result. After realising this and some more experimenting the above order seemed to work on both my HTC Desire and Acer Iconia A500 and I was quite happy with it.

However I have just received a comment for my application saying it produces corrupted images on the HTC Desire HD so I would recommend not using this order of camera initialization as a default but rather as a fix for the HTC Desire.

like image 181
Wessel Rossing Avatar answered Nov 16 '22 02:11

Wessel Rossing