Image still rotated after Editing Exif Data

183 Views Asked by At

In My app the User captures an image from the gallery or camera it is then converted to PDF, Now my problem is that images captured from the samsung devices are rotated,

I am trying to edit the exif data but it does not work,

there is no errors the image just stays rotated

I am new to android and the entire EXIF data thing goes over my head, some advice would be awesome

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

                ExifInterface exif;
                int rotate = 0;

                try
                {
                    exif = new ExifInterface(selectedPhoto);
                    int exifOrientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);

                    switch (exifOrientation)
                    {
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            rotate = 90;

                            break;

                        case ExifInterface.ORIENTATION_ROTATE_180:
                            rotate = 180;
                            break;

                        case ExifInterface.ORIENTATION_ROTATE_270:
                            rotate = 270;
                            break;
                    }


                    if (rotate != 0)
                    {
                        int w = image.getWidth();
                        int h = image.getHeight();

                        // Setting pre rotate
                        Matrix mtx = new Matrix();
                        mtx.preRotate(rotate);

                        // Rotating Bitmap & convert to ARGB_8888, required by tess
                        try
                        {
                            image = Bitmap.createBitmap(image, 0, 0, w, h, mtx, false);
                        }
                        catch(OutOfMemoryError e)
                        {
                            e.printStackTrace();
                            //image = Bitmap.createBitmap(image, 0, 0, w/2, h/2, mtx, false);

                        }

                    }
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }


                Matrix matrix = new Matrix();
                matrix.postRotate(rotate);
                image=createBitmap(image, 0, 0,
                        image.getWidth(), image.getHeight(),
                        matrix, true);

                canvas.drawBitmap(image, 0, 0, null);
                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();


        }
0

There are 0 best solutions below