我想从相机文件夹中添加图像到pdf图像显示我可以用pdf添加图像

时间:2014-03-20 09:20:16

标签: android image pdf android-image

我正在使用Document类创建pdf,在我从sdcard相机文件夹下载图像后,我想将所有图像保存在pdf文件中,我拿文档页面是A4尺寸,我想缩放图像A4尺寸意味着我想要保留具有4种尺寸图像的图像。

private void addImages(Document document) {
        // TODO Auto-generated method stub

        File targetDirector = new File(targetPath);

        File[] files = targetDirector.listFiles();
        for (File file : files) {


            ByteArrayOutputStream stream = new ByteArrayOutputStream();

            thumbnails = decodeFile(file.getAbsolutePath());

            ExifInterface exif;
            try {
                exif = new ExifInterface(file.getAbsolutePath());

                String orientString = exif
                        .getAttribute(ExifInterface.TAG_ORIENTATION);

                int orientation = orientString != null ? Integer
                        .parseInt(orientString)
                        : ExifInterface.ORIENTATION_NORMAL;
                int rotationAngle = 0;

                if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                    System.out.println("photopotart");
                    thumbnails = getResizedBitmap(thumbnails, 150, 150);
                    thumbnails
                            .compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    Image myImg = null;
                    try {
                        myImg = Image.getInstance(stream.toByteArray());

                    } catch (BadElementException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();

                    }
                    // add image to document
                    try {
                        document.newPage();
                        document.add(myImg);
                    } catch (DocumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

                else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                    System.out.println("180 angle");
                    rotationAngle = 180;

                    System.out.println("photo180");
                    thumbnails = getResizedBitmap180(thumbnails, 200, 200);
                    thumbnails
                            .compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    Image myImg = null;
                    try {
                        myImg = Image.getInstance(stream.toByteArray());

                    } catch (BadElementException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();

                    }
                    myImg.setAlignment(Image.LEFT);

                    // add image to document
                    try {
                        document.newPage();
                        document.add(myImg);
                    } catch (DocumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                    System.out.println("other 270");

                    System.out.println("photo270");
                    thumbnails = getResizedBitmap270(thumbnails, 200, 200);
                    thumbnails
                            .compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    Image myImg = null;
                    try {
                        myImg = Image.getInstance(stream.toByteArray());

                    } catch (BadElementException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();

                    }

                    // add image to document
                    try {
                        document.newPage();
                        document.add(myImg);
                    } catch (DocumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("photolandscape");

                    System.out.println("photo0");
                    // thumbnails =thumbnails.createScaledBitmap(thumbnails,
                    // 350, 250, false);
                    System.out.println("thumbnailsw" + thumbnails.getWidth());
                    System.out.println("thumbnailsh" + thumbnails.getHeight());


                    String imageUrl = file.getAbsolutePath();
                    Image image2 = Image.getInstance(file.getAbsolutePath());
                    image2.scaleAbsolute(150f, 150f);
                      document.newPage();
                      document.add(image2);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (BadElementException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

 ByteArrayOutputStream stream = new ByteArrayOutputStream();
                         Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.ic_launcher);
                         bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
                         Image myImg = Image.getInstance(stream.toByteArray());
                         myImg.setAlignment(Image.MIDDLE);

                         //add image to document
                         document.add(myImg);

如果没有填写页面,则缩放图像

 Bitmap.createScaledBitmap(unscaledBitmap, wantedWidth, wantedHeight, true);

同样通过this它有一个很好的解释缩放位图