2010-03-11 63 views
1

任何人都可以幫我解決我的相機預覽問題。我正在使用nexus將相機參數設置爲640,480。但其表示強制關閉錯誤 我的代碼如下Nexus One的相機預覽參數?

public class CameraPreview extends Activity {  
    private Preview mPreview; 
    public static ProgressDialog pdDetail; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Hide the window title. 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     // Create our Preview view and set it as the content of our activity. 
     mPreview = new Preview(this); 
     setContentView(mPreview); 
    } 

    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      if(mPreview.myIntent!=null){ 
       setResult(android.app.Activity.RESULT_OK,mPreview.myIntent); 
       finish(); 
      } 
      return super.onKeyDown(keyCode, event); 
     } 

     if (keyCode == KeyEvent.KEYCODE_SPACE ||keyCode == KeyEvent.KEYCODE_CAMERA || keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { 
      mPreview.captureImage(); 
      pdDetail = ProgressDialog.show(this, null , null,true,false); 
      return true; 
     } 


     return false; 
    } 

} 

// ---------------------------------------------------------------------- 

class Preview extends SurfaceView implements SurfaceHolder.Callback { 
    SurfaceHolder mHolder; 
    Camera mCamera; 
    Intent myIntent = null; 
    Context appContext; 
    Preview(Context context) { 
     super(context); 
     appContext = context; 
     // Install a SurfaceHolder.Callback so we get notified when the 
     // underlying surface is created and destroyed. 
     mHolder = getHolder(); 
     mHolder.addCallback(this); 
     mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     // The Surface has been created, acquire the camera and tell it where 
     // to draw. 
     mCamera = Camera.open(); 
     try { 
      mCamera.setPreviewDisplay(holder); 
     } catch (IOException exception) { 
      mCamera.release(); 
      mCamera = null; 
      // Add more exception handling logic here 
     } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     // Surface will be destroyed when we return, so stop the preview. 
     // Because the CameraDevice object is not a shared resource, it's very 
     // important to release it when the activity is paused. 
     mCamera.stopPreview(); 
     mCamera.release(); 
     mCamera = null; 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
     // Now that the size is known, set up the camera parameters and begin 
     // the preview. 
     //TODO Fix There is a simple work-around for HERO. In the surfaceChanged method simply swap the 
     //width and height in setPreviewSize when in portrait mode. 
     Camera.Parameters parameters = mCamera.getParameters(); 
     parameters.setPictureSize(640,480); //800,300 
     parameters.setPreviewSize(w, h); 
     mCamera.setParameters(parameters); 
     mCamera.startPreview(); 

    } 

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { 
     public void onPictureTaken(byte[] data, Camera c) { 
      myIntent = new Intent(); 
      myIntent.putExtra("bitmapData", true); 
      Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length); 
      saveFile(b);//TODO Unable to Store Image. Problem with SD Card 

      //mCamera.startPreview(); 
      if(CameraPreview.pdDetail.isShowing()){ 
       CameraPreview.pdDetail.cancel(); 
       //CameraPreview.pdDetail.dismiss(); 
      } 
     } 
    }; 

    private boolean saveFile(Bitmap bitmap){ 
     boolean status = false; 
     try { 
      String externalStorageState = Environment.getExternalStorageState(); 
      URI uri = null; 
      //String name = appContext.getFilesDir().getAbsolutePath()+"/"+"camera.jpg"; 
      if(externalStorageState.equalsIgnoreCase("mounted")){ 
       //In SD Card 
       //uri = URI.create("file://"+Environment.getExternalStorageDirectory()+"/ExpenseTracker/"); 
       //File sddir = new File(uri); 

       /*File file = new File(uri); 
       if(!file.exists()){ 
        boolean mkdirs = file.mkdirs(); 
       }*/ 
       uri = URI.create("file://"+Environment.getExternalStorageDirectory()+"/ExpenseTracker/"+"camera.jpg"); 
       File file = new File(uri); 
       file.mkdirs(); 

       file.delete(); 
       file.createNewFile(); 
       FileOutputStream fos = new FileOutputStream(file); 
       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       //bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); //bitmap is the bitmap object  
       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
       byte[] b = baos.toByteArray(); 
       fos.write(b); 
       fos.flush(); 
       fos.close(); 
       status = true; 
      }else{ 
       //uri = URI.create("file://"+name); 
       /*FileOutputStream out = appContext.openFileOutput("camera.jpg", android.app.Activity.MODE_APPEND); 
       BufferedOutputStream bos = new BufferedOutputStream(out,8192); 
       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); 
       bos.flush(); 
       bos.close();*/ 
       status = true; 
      } 

     } catch (IOException ex) { 
     } 
     return status ; 
    } 
    public void captureImage(){ 
     mCamera.takePicture(shutterCallback, rawCallback, mPictureCallback); 
    } 
    ShutterCallback shutterCallback = new ShutterCallback() { 
     public void onShutter() { 
      // Do something when the shutter closes. 
     } 
    }; 
    PictureCallback rawCallback = new PictureCallback() { 
     public void onPictureTaken(byte[] _data, Camera _camera) { 
      // Do something with the image RAW data. 
     } 
    }; 
} 

請幫我找到我的錯誤.....

+0

您能否正確格式化代碼塊?目前它不可讀。 thx – zapping 2010-03-11 14:36:43

回答

1

您是否嘗試過檢查,看看是否支持該決議?

List<Camera.Size> picSizes = parameters.getSupportedPictureSizes(); 
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes(); 
+0

請你指定如何返回這個我的代碼? – harish 2010-03-12 13:28:15

+0

您定位的SDK級別是什麼?這些方法在2.0中添加。 – CaseyB 2010-03-12 16:36:16

1

您的問題在這裏:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
    ... 
    parameters.setPreviewSize(w, h); 
    ... 
} 

你不能只指定任何你想要的大小,你應該只使用支持的尺寸。使用此example設置正確的預覽大小。