我已经开发了一个基本的相机,并试图让它工作。
这是相机代码
package com.example.camera;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Camera camera;
Object size;
public CameraView(Context context) {
super(context);
holder = this.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(this);
}
public Camera getCamera(){
return camera;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters params = camera.getParameters();//Getting paramaters.
if(size!=null){ //make sure we don't pull a NullException.
params.setPreviewSize(width, height);//gotta set the size, since we know it.
}
camera.setParameters(params);//gotta set the paramaters now.
camera.startPreview();//starting the preview.
}
public void surfaceCreated(SurfaceHolder arg0) {
try{
camera = Camera.open();//setting the camera up.
camera.setPreviewDisplay(holder);//making the display our current SurfaceHolder
} catch (IOException e){
e.printStackTrace();//printing out the error message if it happens.
}
}
public void surfaceDestroyed(SurfaceHolder arg0) {
camera.stopPreview();
camera.release();
camera = null;
}
}
现在我试图从我的主要方法调用相机,但我似乎无法让它工作
包 com.example.camera;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class main {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
cam = cv.getCamera(); //notice how we used that method? ;)
cam.takePicture(null,null,PictureCallback_);
}
});
}
Camera.PictureCallback PictureCallback_ = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
InputStream is = new ByteArrayInputStream(imageData);
Bitmap bmp = BitmapFactory.decodeStream(is);
}
} ;
任何帮助表示赞赏。尝试调用原始相机活动 getCamera 然后获取照片并将其放入 imageView1 任何帮助表示赞赏。