我正在尝试学习如何在android中创建一个简单的应用程序。作为背景信息,我只用C语言编程,没有OOP。
目前我正在尝试使用Android开发者网站上的指示打开相机,但有一些小的改动: - 没有捕捉图像的按钮。 - 没有新的活动。
我要做的是jut预览相机。 我将发布我正在使用的代码,清单和LogCat。
主要活动:
package com.example.camera_display;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Menu;
import android.widget.FrameLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
private Camera mcamera;
private CameraPreview mCameraPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcamera = getCameraInstance();
mCameraPreview = new CameraPreview(this, mcamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);
}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
CameraPreview类:
package com.example.camera_display;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "MyActivity";
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
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());
}
}
}
清单:
<uses-permission
android:name="android.permission.CAMERA"/>
<uses-feature
android:name="android.hardware.camera"/>
LOG CAT:
06-30 15:58:35.075: D/libEGL(1153): loaded /system/lib/egl/libEGL_emulation.so
06-30 15:58:35.147: D/(1153): HostConnection::get() New Host Connection established 0x2a156060, tid 1153
06-30 15:58:35.478: D/libEGL(1153): loaded /system/lib/egl/libGLESv1_CM_emulation.so
06-30 15:58:35.515: D/libEGL(1153): loaded /system/lib/egl/libGLESv2_emulation.so
06-30 15:58:36.334: W/EGL_emulation(1153): eglSurfaceAttrib not implemented
06-30 15:58:36.685: D/OpenGLRenderer(1153): Enabling debug mode 0
06-30 15:58:36.935: D/MyActivity(1153): Error starting camera preview: startPreview failed
06-30 15:58:36.965: I/Choreographer(1153): Skipped 125 frames! The application may be doing too much work on its main thread.
06-30 15:58:38.455: W/Camera(1153): Camera server died!
06-30 15:58:38.455: W/Camera(1153): ICamera died
06-30 15:58:38.476: E/Camera(1153): Error 100
所以如果有人能告诉我我做错了什么(并解释为什么它应该做得不同)那就太棒了:))
谢谢!