我试图在Android上使用相机创建应用程序。我使用的代码我能够在android文档中找到,在互联网上等等就像测试一样。 相机预览仍为白色,没有任何崩溃,但根本没有预览显示。无法找到它是来自我的模拟器还是我的代码......我在模拟器中使用集成的网络摄像头作为相机。
请帮忙吗?
这是我的相机预览课程:
package com.guillaimej.testapplication.app;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraInputView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraInputView(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) {
System.out.println("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){
System.out.println("Error starting camera preview: " + e.getMessage());
}
}
}
这是我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.guillaimej.testapplication.app.CameraInputActivity">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/camera_layout"
android:layout_weight="0.2">
</FrameLayout>
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/domygif"
android:layout_gravity="center_horizontal"
android:background="@drawable/flask"
android:clickable="true"
android:onClick="snapIt"
android:layout_weight="0.0"/>
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/returnButton"
android:layout_gravity="center_horizontal"
android:background="@drawable/return_button"
android:layout_weight="0.0"/>
</LinearLayout>
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.guillaimej.testapplication.app" >
<uses-sdk android:minSdkVersion="10"
android:targetSdkVersion="10"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ListOfFileActivity"
android:label="@string/title_activity_list_of_file" >
</activity>
<activity
android:name=".VideoActivity"
android:label="@string/title_activity_video" >
</activity>
<activity
android:name=".CameraInputActivity"
android:label="@string/title_activity_camera_input" >
</activity>
</application>
</manifest>
编辑:
以下是我使用相机预览的活动:
package com.guillaimej.testapplication.app;
import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
public class CameraInputActivity extends Activity{
private ImageButton returnButton = null;
private ImageButton domygifButton = null;
//private Camera camera = null;
private CameraInputView cameraView;
private SurfaceHolder previewHolder;
private FrameLayout cameraLayout = null;
// Check if the camera is free and return it
private static Camera isCameraAvailable() {
Camera cam = null;
try {
cam = Camera.open(0);
} catch (Exception e) {
}
return cam;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_input);
returnButton = (ImageButton) findViewById(R.id.returnButton);
returnButton.setOnTouchListener(new ReturnButtonListener());
domygifButton = (ImageButton) findViewById(R.id.domygif);
cameraLayout = (FrameLayout) findViewById(R.id.camera_layout);
Camera camera = isCameraAvailable();
// Create our Preview view and set it as the content of our activity.
cameraView = new CameraInputView(this, camera);
cameraLayout.addView(cameraView);
}
}