0

我正在尝试为 android 构建一个图像编辑应用程序,而我刚刚开始并且已经遇到了麻烦。好吧,我可以拍照并保存到SD卡。但我尝试做一些应用程序正在运行的效果,但效果没有出现在预览中。这是我使用的完整代码:

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();

}}

谁能告诉我我的效果代码有什么问题。有其他方法吗?

这是我用来获取支持的效果并循环播放的代码。

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

这给了我一个空指针异常。所以我试图看看 colorEffects 是否包含任何数据或效果。我使用了这样的 if 循环

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

它是空的。为什么?下载的sdk有问题吗?我应该下载 android 2.3 SDK。目前我已经下载了 2.2 SDK。顺便说一句,即使只是输入

parameters.setColorEffect(Camera.Parameters.EFFECT_NEGATIVE);

不使用 getSupportedColorEffects() 方法,它运行没有错误,但效果不会显示在预览中。

日志猫信息:

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)

当我输入这行代码时发生此错误:

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

1 回答 1

1

你能试试把这些行:

Camera.Parameters parameters = camera.getParameters();

parameters.setColorEffect(Camera.Parameters.EFFECT_NEGATIVE);

camera.setParameters(parameters);

进入surfaceChanged方法?

这是我从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());
        }
    }

根据this reference,建议对surfaceChanged方法进行任何更改,就像评论所说的那样。

我有类似的应用程序会改变预览的大小,我按照指南进行操作。那你可以试试这个方法吗?

于 2012-12-17T11:18:17.310 回答