2012-11-04 26 views
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,任何幫助表示讚賞。

+1

你會得到什麼錯誤?提供具體信息,所以我們可以幫助 –

+0

「我似乎無法使它工作」。發佈錯誤/ logcat。 –

+0

對不起,我向底部畫了一個空白。 cam = cv.getCamera(); \t \t cam.takePicture(null,null,PictureCallback_);我不知道如何調用camerview類中的getCamera方法來允許拍照 – blankwall

回答

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_); 
    } 
});