gpt4 book ai didi

java - Android 相机效果不起作用

转载 作者:行者123 更新时间:2023-12-01 14:59:21 26 4
gpt4 key购买 nike

我正在尝试为 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;
}
}

最佳答案

你能尝试把这些行:

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 方法中,就像注释所说的那样。

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

关于java - Android 相机效果不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13902101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com