-->

Android Camera Effects Not Working

2019-06-11 02:27发布

问题:

I'm trying to build an image editing app for android and I've just started and already I'm having trouble. Well I can take a picture and save to the sd card. But i try to do some effects the app is running but the effects don't show up in the preview. Heres the complete code that i use:

package com.example.camerademo;

import java.io.IOException;

import android.app.Activity;

import android.hardware.Camera;

import android.os.Bundle;

import android.view.SurfaceHolder;

import android.view.SurfaceView;



public class MainActivity extends Activity implements 

SurfaceHolder.Callback {

SurfaceView cameraView;

SurfaceHolder surfaceHolder;

Camera camera;





@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

cameraView = (SurfaceView) this.findViewById(R.id.CameraView);

surfaceHolder = cameraView.getHolder();

surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

surfaceHolder.addCallback(this);


}





@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {

    // TODO Auto-generated method stub



    camera.startPreview();


}

@Override

public void surfaceCreated(SurfaceHolder holder) {

    // TODO Auto-generated method stub

    camera = Camera.open();

    try {

    camera.setPreviewDisplay(holder);

    Camera.Parameters parameters = camera.getParameters();

    parameters.setColorEffect(Camera.Parameters.EFFECT_NEGATIVE);

    camera.setParameters(parameters);



    }

    catch (IOException exception)

    {

    camera.release();

    }

    }



@Override

public void surfaceDestroyed(SurfaceHolder holder) {

    // TODO Auto-generated method stub

    camera.stopPreview();

    camera.release();

}}

Can anyone please tell me what is wrong with my code for effects. Is the a any other way of doing it?

Heres the code i used to get the supported effects and loop through it.

List<String> colorEffects = parameters.getSupportedColorEffects();
Iterator<String> cei = colorEffects.iterator();
while (cei.hasNext())
{
String currentEffect = cei.next();
if (currentEffect.equals(Camera.Parameters.EFFECT_NEGATIVE))
{
parameters.setColorEffect(Camera.Parameters.EFFECT_NEGATIVE);
break;
}
}

This gives me a null pointer exception.. So i tried to see if colorEffects holds any data or effects. I used an if loop like this

if(colorEffects == null){
editText.setText("Null");
}

And it was null. Why? Is there some problem with the downloaded sdk. Should I download android 2.3 SDK. Currently i have 2.2 SDK downloaded. Btw even if just type

parameters.setColorEffect(Camera.Parameters.EFFECT_NEGATIVE);

without using getSupportedColorEffects() method it runs without errors but the effect doesn't show up in preview.

Log Cat Information:

01-01 00:08:17.588: E/AndroidRuntime(297): FATAL EXCEPTION: main
01-01 00:08:17.588: E/AndroidRuntime(297): java.lang.NullPointerException
01-01 00:08:17.588: E/AndroidRuntime(297):  at com.example.camerademo.MainActivity.surfaceCreated(MainActivity.java:56)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.SurfaceView.updateWindow(SurfaceView.java:532)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.SurfaceView.dispatchDraw(SurfaceView.java:339)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.View.draw(View.java:6743)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.View.draw(View.java:6743)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
01-01 00:08:17.588: E/AndroidRuntime(297):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.ViewRoot.draw(ViewRoot.java:1407)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.os.Looper.loop(Looper.java:123)
01-01 00:08:17.588: E/AndroidRuntime(297):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-01 00:08:17.588: E/AndroidRuntime(297):  at java.lang.reflect.Method.invokeNative(Native Method)
01-01 00:08:17.588: E/AndroidRuntime(297):  at java.lang.reflect.Method.invoke(Method.java:521)
01-01 00:08:17.588: E/AndroidRuntime(297):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-01 00:08:17.588: E/AndroidRuntime(297):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-01 00:08:17.588: E/AndroidRuntime(297):  at dalvik.system.NativeStart.main(Native Method)

This error occurs when i enter this line of code:

List<String> colorEffects = parameters.getSupportedColorEffects();
    Iterator<String> cei = colorEffects.iterator();
    while (cei.hasNext())
    {
    String currentEffect = cei.next();
    if (currentEffect.equals(Camera.Parameters.EFFECT_NEGATIVE))
    {
    parameters.setColorEffect(Camera.Parameters.EFFECT_NEGATIVE);
    break;
    }
    }

回答1:

Could you try put these lines:

Camera.Parameters parameters = camera.getParameters();

parameters.setColorEffect(Camera.Parameters.EFFECT_NEGATIVE);

camera.setParameters(parameters);

into surfaceChanged method?

This is the sample code I copied from Android Developers Reference:

 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.

        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
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }

According to this reference, it is recommend to put any changes in surfaceChanged method just like the what the comments are saying.

I've similar app that will change the size of preview and I followed the guide. So could you try this method?