开发者

camera preview parameters of Nexus one?

开发者 https://www.devze.com 2022-12-22 19:56 出处:网络
Any one please help me to solve my camera preview issue. am using a nexus one am setting the camera parameters to 640,480. but its showing force close error

Any one please help me to solve my camera preview issue. am using a nexus one am setting the camera parameters to 640,480. but its showing force close error my code is as follows

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

please help me to find my error .....


Have you tried checking to see if that resolution is supported?

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


You problem here:

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

You can't just specify any size you want, you should use only supported sizes. Use this example to set proper preview size.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号