我正在为android构建一个相机应用程序,试图学习如何。我已经按照教程,但它一直失败并强制关闭。请看下面我的详细信息:
Xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/layout">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Camera Demo"
android:textSize="24sp" />
<FrameLayout android:id="@+id/preview"
android:layout_weight="1" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/buttonClick"
android:text="Click" android:layout_gravity="center"></Button>
</LinearLayout>
Java代码:
public class CameraActivity extends Activity {
private static final String TAG = "CameraDemo";
Camera camera;
Preview preview;
Button buttonClick;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_screen);
preview = new Preview(this);
((FrameLayout) findViewById(R.id.preview)).addView(preview);
buttonClick = (Button) findViewById(R.id.buttonClick);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
preview.camera.takePicture(shutterCallback, rawCallback,
jpegCallback);
}
});
Log.d(TAG, "onCreate'd");
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.d(TAG, "onShutter'd");
}
};
/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(TAG, "onPictureTaken - raw");
}
};
/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
// write to local sandbox file system
// outStream =
// CameraDemo.this.openFileOutput(String.format("%d.jpg",
// System.currentTimeMillis()), 0);
// Or write to sdcard
outStream = new FileOutputStream(String.format(
"/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};
}
Preview.java:
class Preview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format(
"/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
Log.d(TAG, "onPreviewFrame - wrote bytes: "
+ data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Preview.this.invalidate();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
camera.stopPreview();
camera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(w, h);
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Paint p = new Paint(Color.RED);
Log.d(TAG, "draw");
canvas.drawText("PREVIEW", canvas.getWidth() / 2,
canvas.getHeight() / 2, p);
}
}
logcat的:
02-01 19:44:41.825: E/AndroidRuntime(4329): FATAL EXCEPTION: main
02-01 19:44:41.825: E/AndroidRuntime(4329): java.lang.RuntimeException: setParameters failed
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.hardware.Camera.native_setParameters(Native Method)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.hardware.Camera.setParameters(Camera.java:1527)
02-01 19:44:41.825: E/AndroidRuntime(4329): at com.mysoftware.mysoftwareos.mobile.Preview.surfaceChanged(Preview.java:78)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.view.SurfaceView.updateWindow(SurfaceView.java:554)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.view.SurfaceView.access$000(SurfaceView.java:81)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1848)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1028)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4246)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.view.Choreographer.doFrame(Choreographer.java:525)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.os.Handler.handleCallback(Handler.java:615)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.os.Handler.dispatchMessage(Handler.java:92)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.os.Looper.loop(Looper.java:137)
02-01 19:44:41.825: E/AndroidRuntime(4329): at android.app.ActivityThread.main(ActivityThread.java:4765)
02-01 19:44:41.825: E/AndroidRuntime(4329): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 19:44:41.825: E/AndroidRuntime(4329): at java.lang.reflect.Method.invoke(Method.java:511)
02-01 19:44:41.825: E/AndroidRuntime(4329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-01 19:44:41.825: E/AndroidRuntime(4329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-01 19:44:41.825: E/AndroidRuntime(4329): at dalvik.system.NativeStart.main(Native Method)
02-01 19:44:45.455: E/Trace(4386): error opening trace file: No such file or directory (2)
02-01 19:44:45.585: E/PhonePolicy(4386): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
请帮助我,非常感谢!
答案 0 :(得分:2)
您没有显示预览类的来源,因此很难说出可能出现的问题。
但是从跟踪中,错误来自Preview.surfaceChanged的Camera.setParameters()调用。所以问题是,你想在那里设置相机不喜欢的东西?
此外,您的日志仅涵盖了异常,因此没有关于系统在失败之前正在执行的操作的上下文。在失败之前包括一些日志对调试也很有帮助;系统通常会在堆栈跟踪之前打印出调用失败几行的原因。
编辑:现在使用Preview.java,我可以看到问题。
您无法为相机预览输出选择随机大小。它必须是getSupportedPreviewSizes()提供的预览尺寸之一。现在,你只是将Surface的大小用于预览输出,但这并不能保证完全正常工作(如果大小与列表中的大小匹配,你可能会很幸运,但这不太可能。)
查看Android开发者网站上的camera app tutorial;它试图涵盖其中一些细节。