【问题标题】:Image Gets rotated captured from camera图像从相机旋转捕获
【发布时间】:2020-02-11 20:28:08
【问题描述】:

在我的应用程序中,用户从相机捕获图像或从图库中选择将其转换为 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();

任何建议或问题出在哪里,没有错误代码或任何东西

【问题讨论】:

    标签: android camera exif image-rotation


    【解决方案1】:

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

    【讨论】:

    • 对不起这个愚蠢的问题,这会旋转位图吗?,我假设它没有看到画布旋转然后位图被绘制
    • 没有。 Canvas.rotate 转换矩阵和 canvas.drawBitmap 绘制由该矩阵转换的位图,并且您的图像正确旋转。我会建议你使用 Matrix,因为它更高效
    • 这样的?? Matrix matrix = new Matrix(); matrix.postRotate(rotation); Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true); canvas.rotate(rotation); canvas.drawBitmap(image, 0, 0, null); document.finishPage(page);
    • 是的,但是您不必使用画布 Bitmap.createBitmap 将为您提供所需的位图。如果您觉得这个答案有帮助,请点个赞。
    • PDF 需要画布
    猜你喜欢
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2017-04-15
    • 2015-03-19
    • 1970-01-01
    相关资源
    最近更新 更多