SDCard中捕获的图像存储使用相机捕获(非意图)

时间:2013-12-04 12:50:02

标签: android camera android-sdcard

我创建了一个捕获图像并存储在SD卡中的应用程序。该应用程序似乎与预览工作正常。但问题是,当我通过eclipse在手机上运行应用程序时,图像会被捕获。如果我以正常方式运行我的应用程序并尝试捕获图像,则无法捕获图像。

我已附上我的代码

我的MainActivity.class

public class MainActivity extends Activity {

    private ImageHandler preview;
    Camera camera;
    int cameraID, currentCamera;
    Button capture;
    FrameLayout imageLayout;
    SurfaceView imgl;
    private static final String TAG = "CallCamera";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*
         * Invoking Camera and Setting the ImageHandler for preview
         */
        camera = getCameraInstance();
        preview = new ImageHandler(this, camera);

        /*
         * Getting the ID of the FrameLayout that displays the camera
         */

        imageLayout = (FrameLayout) findViewById(R.id.preview_layout);
        imageLayout.addView(preview);

        /*
         * Getting the ID of capture Button and setting its Functionality
         */

        capture = (Button) findViewById(R.id.cameraButton);

        capture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                camera.takePicture(null, null, null, pic);
            }
        });

    }

    private Camera getCameraInstance() {
        // TODO Auto-generated method stub
        Camera cam = null;
        try {
            cam = Camera.open();
        } catch (Exception e) {

        }
        return cam;
    }

    PictureCallback pic = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // TODO Auto-generated method stub
            // File picFile = getOutputPhotoFile();
            // if(picFile == null){
            // return;
            // }
            //
            // try{
            // FileOutputStream fos = new FileOutputStream(picFile);
            // fos.write(data);
            // fos.close();
            //
            // }catch(FileNotFoundException e){
            //
            // }catch (IOException e){
            //
            // }
            //

            File pictureFileDir = getDir();

            if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {

                Log.d(TAG, "Can't create directory to save image.");
                Toast.makeText(getApplicationContext(),
                        "Can't create directory to save image.",
                        Toast.LENGTH_LONG).show();
                return;

            }

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
            String date = dateFormat.format(new Date());
            String photoFile = "Picture_" + date + ".jpg";

            String filename = pictureFileDir.getPath() + File.separator
                    + photoFile;

            File pictureFile = new File(filename);

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
                Toast.makeText(getApplicationContext(),
                        "New Image saved:" + photoFile, Toast.LENGTH_LONG)
                        .show();
            } catch (Exception error) {
                Log.d(TAG,
                        "File" + filename + "not saved: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        "Image could not be saved.", Toast.LENGTH_LONG).show();
            }
            camera.startPreview();
        }

        private File getDir() {
            File sdDir = Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            return new File(sdDir, "CameraAPIDemo");
        }

    };

    // private File getOutputPhotoFile() {
    //
    // File directory = new File(
    // Environment
    // .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
    // "IMAGES_CAMERA");
    //
    // if (!directory.exists()) {
    // if (!directory.mkdirs()) {
    // Log.e(TAG, "Failed to create storage directory.");
    // return null;
    // }
    // }
    //
    // String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss")
    // .format(new Date());
    //
    // return new File(directory.getPath() + File.separator + "IMG_"
    // + timeStamp + ".jpg");
    // }
    //

    @Override
    public void onResume() {
        super.onResume();
        if (camera == null) {
            camera = Camera.open();
            preview.setCamera(camera);
        }
    }

}

我的预览/ ImageHandler.class

public class ImageHandler extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder holder;
    private Camera imgCamera;
    List<Size> imgSupportedPreviewSizes;

    public ImageHandler(Context context, Camera camera) {
        super(context);
        // TODO Auto-generated constructor stub
        this.imgCamera = camera;
        this.holder = this.getHolder();
        this.holder.addCallback(this);
        this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
        try {
            imgCamera.setPreviewDisplay(holder);
            imgCamera.startPreview();
        } catch (IOException e) {

        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        try {
            imgCamera.setPreviewDisplay(holder);
            imgCamera.startPreview();
            imgCamera.setDisplayOrientation(90);
        } catch (Exception e) {

        }

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        imgCamera.stopPreview();
        imgCamera.release();
    }

    public void setCamera(Camera camera) {
        imgCamera = camera;
        if (imgCamera != null) {
            imgSupportedPreviewSizes = imgCamera.getParameters()
                    .getSupportedPreviewSizes();
            requestLayout();
        }
    }

}

我希望能够在通过手机正常运行应用程序时捕获图像。

0 个答案:

没有答案