我正在尝试为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()方法,它运行时没有错误,但效果并未在预览中显示。
Log Cat信息:
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;
}
}
答案 0 :(得分: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());
}
}
根据此参考资料,建议对surfaceChanged
方法进行任何更改,就像评论所说的那样。
我有类似的应用程序,它将改变预览的大小,我按照指南。你可以试试这种方法吗?