1

I've made custom camera app, which works great but I'm stuck trying to get square images. For few days now I'm trying to make preview squared but now I'm thinking that is wrong approach especially when I read somewhere that Instagram app makes square images by hiding portion of preview with layout section which holds camera buttons (thus making preview look like it is square) and then cropping an image after it is taken. I have an idea how would I do first part but how would I crop image after its taken?

Here are my two methods which capture and save image:

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;
    }
Munez NS
  • 1,011
  • 1
  • 12
  • 31

0 Answers0