Nexus one的相机预览参数?

时间:2010-03-11 12:51:57

标签: android

任何人请帮我解决相机预览问题。我正在使用nexus将相机参数设置为640,480。但它的显示力量接近错误 我的代码如下

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.
        }
    };
}

请帮我找到我的错误.....

2 个答案:

答案 0 :(得分:1)

您是否尝试过检查是否支持该分辨率?

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

答案 1 :(得分:1)

你有问题:

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

您不能只指定任何您想要的尺寸,您应该只使用支持的尺寸。使用此example设置正确的预览尺寸。