[英]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.