如何在自定义相机应用程序拍摄后自动裁剪图像

时间:2014-09-25 10:05:43

标签: android image android-camera

我制作了自定义相机应用程序,效果很好,但我试图获得方形图像。几天之后,我试图让预览平方,但现在我认为这是错误的方法,特别是当我在某个地方看到Instagram应用程序通过隐藏预览部分制作方形图像时,布局部分保存了相机按钮(从而使预览看起来像是正方形)然后在拍摄后剪裁图像。我有一个想法,我将如何做第一部分,但如何在拍摄后裁剪图像?

以下是我捕获和保存图像的两种方法:

private PictureCallback mPicture = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            if (pictureFile == null) {
                Log.d("Camera error",
                        "Error creating media file, check storage permissions: ");
                return;
            }


            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);

                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.d("Camera error", "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d("Camera error", "Error accessing file: " + e.getMessage());
            }

            Intent intent = new Intent(MyCamera.this, NewItem.class);
            intent.putExtra("imagePath", pictureFile.getAbsolutePath());
            startActivity(intent);
        }

    };

    /** Create a File for saving an image or video */
    private static File getOutputMediaFile(int type) {
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
                "Pickante");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");

        } else if (type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "VID_" + timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

0 个答案:

没有答案