0

I am loading an image from Gallery as bitmap - on to a canvas and performing some drawings on it.

At any point in time I have this bitmap containing my drawings, readily available.

I have a requirement to crop this bitmap. For this I need to convert the bitmap first into an image file - since there are no api's or libraries available to crop a bitmap directly. 2-3 seconds, I loose in this conversion process.

Is there a way out(or using any 3rd party library), by which I can directly crop a bitmap, without actually having to convert this into an image file. This will help me a lot in improving the performance of my app.

Any help is much appreciated.

PS: For reference, I have added the bitmap conversion code below. The library I use for cropping currently is - https://github.com/jdamcd/android-crop

drawView.setDrawingCacheEnabled(true);
            Date d = new Date();
            CharSequence s  = DateFormat.format("MM-dd-yy hh-mm-ss", d.getTime());
            Bitmap bitmap = drawView.getResultBitmap();
            File sdCardDirectory = Environment.getExternalStorageDirectory();
            File image = new File(sdCardDirectory, "DCIM/Camera/" + s.toString() + ".png");
            boolean success = false;

            // Encode the file as a PNG image.
            FileOutputStream outStream;
            try {

                outStream = new FileOutputStream(image);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                    /* 100 to keep full quality of the image */

                outStream.flush();
                outStream.close();
                success = true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //static final Uri imgUri;
            if (success) {
                MediaScannerConnection.scanFile(getActivity(), new String[]{
                                image.getAbsolutePath()},
                        null, new MediaScannerConnection.OnScanCompletedListener() {
                            public void onScanCompleted(String path, Uri uri) {
                                beginCrop(uri);
                            }
                        });
Arsaceus
  • 293
  • 2
  • 19
user264953
  • 1,837
  • 8
  • 37
  • 63
  • Why not just crop it using `bitmap.createBitmap(Bitmap source, int x, int y, int width, int height)`? – David Medenjak Feb 03 '16 at 20:31
  • By cropping , I mean selecting area of choice and cropping . I have updated question with the library used for this - which I want to change with something which accepts bitmap , instead of URI – user264953 Feb 03 '16 at 20:41
  • 2
    The library that you cited is open source, as are [most if not all of these libraries](https://android-arsenal.com/tag/45). If they do not have the feature that you desire (taking a `Bitmap` as input), fork one and add it. – CommonsWare Feb 03 '16 at 20:43

0 Answers0