Monday, 17 May 2021

More efficient method to take picture from Android in-app-cam and upload it to server

I'm building an app that require method to take picture from in-app camera, but for some Android devices (old device or low ram), it's quite freeze when taking picture triggered. Is there any code i can modify or optimize to make user experience feels better?

//this function trigger to take picture (or screenshot) from user screen
    private void captureImage() {
        mPreview.setDrawingCacheEnabled(true);
        final Bitmap[] drawingCache = {Bitmap.createBitmap(mPreview.getDrawingCache())};
        mPreview.setDrawingCacheEnabled(false);

     mCameraSource.takePicture(null, bytes -> {
            int orientation = Exif.getOrientation(bytes);
            Bitmap temp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            Bitmap picture = rotateImage(temp, orientation);
            assert picture != null;
            Bitmap overlay = Bitmap.createBitmap(mGraphicOverlay.getWidth(), mGraphicOverlay.getHeight(), picture.getConfig());
            Canvas canvas = new Canvas(overlay);

            Matrix matrix = new Matrix();
            matrix.setScale((float) overlay.getWidth() / (float) picture.getWidth(), (float) overlay.getHeight() / (float) picture.getHeight());

            // mirror by inverting scale and translating
            matrix.preScale(-1, 1);
            matrix.postTranslate(canvas.getWidth(), 0);

            Paint paint = new Paint();
            canvas.drawBitmap(picture, matrix, paint);
            canvas.drawBitmap(drawingCache[0], 0, 0, paint);

//this function to save picture taken and put it on app storage cache
            try {
                String mainpath = getApplicationContext().getFilesDir().getPath() + separator + "e-Presensi" + separator;
                File basePath = new File(mainpath);
                if (!basePath.exists())
                    Log.d("CAPTURE_BASE_PATH", basePath.mkdirs() ? "Success" : "Failed");

//this function to get directory path of saved photo
                String path = mainpath + "photo_" + getPhotoTime() + ".jpg";
                String namafotoo = "photo_" + getPhotoTime() + ".jpg";
                filePath = path;

                namafoto = namafotoo;
                SessionManager.createNamaFoto(namafoto);

                File captureFile = new File(path);
                boolean sucess = captureFile.createNewFile();
                if (!captureFile.exists())
                    Log.d("CAPTURE_FILE_PATH", sucess ? "Success" : "Failed");
                FileOutputStream stream = new FileOutputStream(captureFile);
                overlay.compress(Bitmap.CompressFormat.WEBP, 60, stream);
                stream.flush();
                stream.close();
                if (!picture.isRecycled()) {
                    picture.recycle();
                }

                if (drawingCache[0] != null && !drawingCache[0].isRecycled()) {
                    drawingCache[0].recycle();
                    drawingCache[0] = null;
                }
                mPreview.setDrawingCacheEnabled(false);
                uploadPicture();
                finish();

            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

Thanks for your help.



from More efficient method to take picture from Android in-app-cam and upload it to server

No comments:

Post a Comment