1

我已经开发了一个基本的相机,并试图让它工作。

这是相机代码

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 任何帮助表示赞赏。

4

2 回答 2

1

尝试在您的 onClick() 方法中执行此操作:

cam.autoFocus(new AutoFocusCallback(){
    Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
        public void onShutter() {
          // If you want to play your own sound (otherwise, it plays the sound by default)
        }
    };

    @Override
    public void onAutoFocus(boolean arg0, Camera arg1) {
        cam.takePicture(shutterCallback, null, PictureCallback_);
    }
});
于 2012-11-04T23:04:05.830 回答
0

我在这里创建了一个相机库项目,您可以使用该来调用相机并拍照并保存它或阅读有关如何使用相机的代码。样品已提供,您可以查看

于 2012-11-05T05:19:39.447 回答