我想寫一種相機應用程序,它使用SurfaceView上顯示的相機進行實時預覽,另一層預覽是一個蒙版(有趣的圖片等)。點擊顯示照片的功能。問題是該應用程序在adroid 2.2上工作得很好,但不適用於2.3(使用手機和模擬器)。我有Mainfest中的相機的權利。應用程序適用於android 2.2,但不適用於2.3
這裏是我的activivty:
package funny.camera;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
public class CameraActivity extends Activity{
private CamScreen cam;
private LayoutInflater controlInflater = null;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
View touchscreen;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cam = new CamScreen(this);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.camscreen);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(cam);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.camoverlay, null);
LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
addContentView(viewControl, layoutParamsControl);
View viewTouch = controlInflater.inflate(R.layout.camtouch, null);
addContentView(viewTouch, layoutParamsControl);
viewControl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cam.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
}
ShutterCallback shutterCallback = new ShutterCallback(){
@Override
public void onShutter() {
}};
PictureCallback rawCallback = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
}};
PictureCallback jpegCallback = new PictureCallback(){
@Override
public void onPictureTaken(byte[] _data, Camera _camera) {
}};
}
而且classrelated到相機預覽
public class CamScreen extends SurfaceView implements SurfaceHolder.Callback {
//SurfaceHolder sHolder;
//SurfaceView surfaceView;
Camera camera;
CamScreen(Context context) {
super(context);
}
public void surfaceCreated(SurfaceHolder holder) {
// Open the camera and start viewing
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
} catch (IOException exception) {
camera.release();
camera = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Kill all our crap with the surface
camera.stopPreview();
camera.release();
camera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Modify parameters to match size.
Camera.Parameters params = camera.getParameters();
params.setPreviewSize(w, h);
params.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(params);
camera.startPreview();
}
}
任何想法有什麼不好?我認爲如果應用程序適用於較舊的Android版本,它也適用於新版本。
謝謝
究竟哪個部分不起作用?沒有重疊?崩潰? – 2012-03-04 19:25:03
[你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – nfechner 2012-03-04 19:26:07