Ruben Meiring
Ruben Meiring

Reputation: 343

Image Gets rotated captured from camera

In my app the user captures an image from the camera or selects from the gallery it converts it to pdf and Uploads it to the server, Now my problem is that the images captured from the camera are rotated on some devices, I do have code to try to solve this but it does not work

 private void PDFCreation(){
        PdfDocument document=new PdfDocument();
        PdfDocument.PageInfo pageInfo;
        PdfDocument.Page page;
        Canvas canvas;
        int i;
        for (i=0; i < list.size(); i++)  {
            pageInfo=new PdfDocument.PageInfo.Builder(992, 1432, 1).create();
            page=document.startPage(pageInfo);
            canvas=page.getCanvas();
            image=BitmapFactory.decodeFile(list.get(i));
            image=Bitmap.createScaledBitmap(image, 980, 1420, true);
            image.setDensity(DisplayMetrics.DENSITY_300);
            canvas.setDensity(DisplayMetrics.DENSITY_300);
            canvas.drawBitmap(image, 0, 0, null);
            float rotation=0;
            try {
                ExifInterface exifInterface=new ExifInterface(selectedPhoto);
                int orientation=exifInterface.getAttributeInt(TAG_ORIENTATION, ORIENTATION_NORMAL);
                switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90: {
                        rotation=-90f;
                        break;
                    }
                    case ExifInterface.ORIENTATION_ROTATE_180: {
                        rotation=-180f;
                        break;
                    }
                    case ExifInterface.ORIENTATION_ROTATE_270: {
                        rotation=90f;
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            canvas.rotate(rotation);
            document.finishPage(page);
        }
        @SuppressWarnings("deprecation") String directory_path=Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
        File file=new File(directory_path);
        if (!file.exists()) {
            //noinspection ResultOfMethodCallIgnored
            file.mkdirs();
        }
        @SuppressLint("SimpleDateFormat") String timeStamp=(new SimpleDateFormat("yyyyMMdd_HHmmss")).format(new Date());
        String targetPdf=directory_path + timeStamp + ".pdf";
         filePath=new File(targetPdf);
        try {
            document.writeTo(new FileOutputStream(filePath));
        } catch (IOException e) {
            Log.e("main", "error " + e.toString());
            Toasty.error(this, "Error making PDF" + e.toString(), Toast.LENGTH_LONG).show();
        }
        document.close();

Any Advice or where is the problem, there is no error codes or anything

Upvotes: 0

Views: 186

Answers (1)

Amit Tiwary
Amit Tiwary

Reputation: 797

You can't rotate your canvas after drawBitmap. You have to first rotate it and then use drawBitmap i.e

        float rotation=0;
        try {
            ExifInterface exifInterface=new ExifInterface(selectedPhoto);
            int orientation=exifInterface.getAttributeInt(TAG_ORIENTATION, ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90: {
                    rotation=-90f;
                    break;
                }
                case ExifInterface.ORIENTATION_ROTATE_180: {
                    rotation=-180f;
                    break;
                }
                case ExifInterface.ORIENTATION_ROTATE_270: {
                    rotation=90f;
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        canvas.save();
        canvas.rotate(rotation);

        //canvas is rotated now use drawBitmap
        canvas.drawBitmap(image, 0, 0, null);
        canvas.restore();

Upvotes: 1

Related Questions