Поворот изображения, полученного с камеры - PullRequest
0 голосов
/ 15 октября 2019

В моем приложении пользователь захватывает изображение с камеры или выбирает из галереи, он конвертирует его в PDF и загружает его на сервер. Теперь моя проблема в том, что изображения, снятые с камеры, вращаются на некоторых устройствах, я делаюесть код, чтобы попытаться решить эту проблему, но он не работает

 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();

Любой совет или в чем проблема, нет кодов ошибок или чего-либо еще

1 Ответ

2 голосов
/ 15 октября 2019

Вы не можете вращать свой холст после drawBitmap. Вы должны сначала повернуть его, а затем использовать drawBitmap, т.е.

        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();
...