简体   繁体   English

我想从相机文件夹图像显示中将图像添加到pdf中,可以在pdf中添加图像吗?

[英]i want to add images to pdf from camera folder image show can i add a images in pdf

i am creating the pdf by using Document class,after i am downloading the images from sdcard camera folder,i want to keep all the images in pdf file,i am taking document page is A4 sizes,i want to scale image with A4 size means i want keep image with a4 sizes images. 我正在使用Document类创建pdf,从sdcard相机文件夹下载图像后,我想将所有图像保留在pdf文件中,我正在将文档页面设置为A4尺寸,我想使用A4尺寸缩放图像我想保持a4尺寸的图像。

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

        }

    }

try something like this 尝试这样的事情

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

And for scaling the image if not fill on page 并且用于缩放图像(如果未填满页面)

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

Also go through this it has a good explanation for scaling the bitmap 还要经历一下,它对于缩放位图有很好的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在pdf报告中添加从相机或图库捕获的图像? - How to add captured images from camera or gallery in pdf report? 如何将图像添加到PDF文件中 - How can I add an image into a PDF file 如何从图库中获取多个图像及其路径,并且我想在图库中添加只能选择9张图像的验证?” - How to get multiple image and its path from gallery and also I want to add validation in gallery that you can select only 9 images"? 如何在GridView中添加图像? 我正在从API获取网格数据,但我想向该gridView添加图像 - How to add images in gridview? I am getting grid data from API but I want to add images to that gridView 如何在Android中显示PDF和图像? - How can i display PDF and images in android? 我想单击按钮添加图像 - I want to add images on click of a button 如何在Android中的任何位置的编辑文本图像中添加图像 - How can I add the image in edit text images in anywhere in android 如何在Android中将图像添加到具有多个页面的pdf中 - how to add images into pdf with multiple pages in android 我可以在使用PrintedPDFDocument创建的PDF中添加关键字吗? - Can I add Keywords to a PDF created with PrintedPDFDocument? 如何找到Android手机的相机图像文件夹? - How can I find out the camera images folder of an Android phone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM