本文整理汇总了Java中org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject类的典型用法代码示例。如果您正苦于以下问题:Java PDImageXObject类的具体用法?Java PDImageXObject怎么用?Java PDImageXObject使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PDImageXObject类属于org.apache.pdfbox.pdmodel.graphics.image包,在下文中一共展示了PDImageXObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createBookPage
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
/**
* Creates a pdf page from two pages from another 'original' pdf document
* @param doc original pdf from which the pages will be taken
* @param leftPage page number of the page to go on the left side
* @param rightPage page number of the page to go on the right side
* @return generated page containing the left and right pages from the original document side-by-side.
*/
private static PDPage createBookPage(PDDocument doc, int leftPage, int rightPage) {
// double the width of a normal page to create the booklet
PDRectangle baseSize = doc.getPage(0).getMediaBox();
PDRectangle box = new PDRectangle(baseSize.getWidth()*2, baseSize.getHeight());
if(sizeOverride != null) {
box = sizeOverride.asPDRectangle();
}
PDPage page = new PDPage(box);
try {
PDImageXObject leftImg = PrintDF.pageToImage(doc, leftPage, scale);
PDImageXObject rightImg = PrintDF.pageToImage(doc, rightPage, scale);
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
if(leftImg != null)
contentStream.drawImage(leftImg, 0, 0, box.getWidth()/2, box.getHeight());
if(rightImg != null)
contentStream.drawImage(rightImg, box.getWidth()/2, 0, box.getWidth()/2, box.getHeight());
contentStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return page;
}
示例2: findPhoto
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
public static void findPhoto(String path,int empId) throws IOException, SQLException, Error{
// Loading an existing document
int imageFound=0;
File file = new File(path);
PDDocument document=PDDocument.load(file);
PDPageTree list=document.getPages();
for(PDPage page:list){ //check in all pages of pdf
PDResources pdResources=page.getResources(); //get all resources
for(COSName cosName:pdResources.getXObjectNames()) //loop for all resources
{
PDXObject pdxObject=pdResources.getXObject(cosName);
if (pdxObject instanceof PDImageXObject) { //check that the resource is image
BufferedImage br=((PDImageXObject) pdxObject).getImage();
RgbImage im = RgbImageJ2se.toRgbImage(br);
// step #3 - convert image to greyscale 8-bits
RgbAvgGray toGray = new RgbAvgGray();
toGray.push(im);
// step #4 - initialize face detector with correct Haar profile
InputStream is = ExtractPhoto.class.getResourceAsStream("/haar/HCSB.txt");
Gray8DetectHaarMultiScale detectHaar = new Gray8DetectHaarMultiScale(is, 1,40);
// step #5 - apply face detector to grayscale image
List<Rect> result= detectHaar.pushAndReturn(toGray.getFront());
if(result.size()!=0)
{
database.StorePhoto.storePhoto(empId,br);
imageFound=1;
break;
}
}
}
if(imageFound==1)
break;
}
System.out.println(imageFound);
if(imageFound!=1){
BufferedImage in = ImageIO.read(ExtractPhoto.class.getResource("/images/nopic.jpg"));
database.StorePhoto.storePhoto(empId,in);
}
document.close();
}
示例3: drawAgeImageFront
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
private void drawAgeImageFront(PDPage page, Attendee attendee) throws IOException {
String imageFilename = BadgeLib.getStaffAgeImageFilename(attendee, currentDateForAgeCalculation);
if (imageFilename == null) { return; }
PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, false);
PDImageXObject xImage = PDImageXObject.createFromFile(imageFilename, document);
Dimension scaledDim = getScaledDimension(
new Dimension(xImage.getWidth(), xImage.getHeight()),
new Dimension(149, 130));
stream.drawImage(xImage,
45 + ((149-scaledDim.width)/2),
335 + ((158-scaledDim.height)/2),
scaledDim.width, scaledDim.height);
stream.close();
}
示例4: drawAgeImageBack
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
private void drawAgeImageBack(PDPage page, Attendee attendee) throws IOException {
String imageFilename = BadgeLib.getStaffAgeImageFilename(attendee, currentDateForAgeCalculation);
if (imageFilename == null) { return; }
PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, DRAW_BOUNDING_BOX);
PDImageXObject xImage = PDImageXObject.createFromFile(imageFilename, document);
Dimension scaledDim = getScaledDimension(
new Dimension(xImage.getWidth(), xImage.getHeight()),
new Dimension(149, 115));
stream.drawImage(xImage,
201 + ((149-scaledDim.width)/2),
344 + ((158-scaledDim.height)/2),
scaledDim.width, scaledDim.height);
stream.close();
}
示例5: getImagesFromResources
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
private List<RenderedImage> getImagesFromResources(PDResources resources) throws IOException {
List<RenderedImage> images = new ArrayList<>();
for (COSName xObjectName : resources.getXObjectNames()) {
PDXObject xObject = resources.getXObject(xObjectName);
if (xObject instanceof PDImageXObject) {
images.add(((PDImageXObject) xObject).getImage());
} else if (xObject instanceof PDFormXObject) {
images.addAll(getImagesFromResources(((PDFormXObject) xObject).getResources()));
}
}
return images;
}
示例6: drawImage
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
public static void drawImage(final BufferedImage image,
final PDDocument document, final PDPageContentStream contentStream,
Position upperLeft, final float width, final float height)
throws IOException {
PDImageXObject cachedImage = getCachedImage(document, image);
float x = upperLeft.getX();
float y = upperLeft.getY() - height;
contentStream.drawImage(cachedImage, x, y, width, height);
}
示例7: drawImage
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
/**
* Draw an image at the x,y coordinates, with the given size.
*
* @param image
* The image to draw.
* @param x
* The x-coordinate to draw the image.
* @param y
* The y-coordinate to draw the image.
* @param width
* The width to draw the image.
* @param height
* The height to draw the image.
* @throws IOException
* If there is an error writing to the stream.
* @throws IllegalStateException
* If the method was called within a text block.
*/
public void drawImage (final PDImageXObject image,
final float x,
final float y,
final float width,
final float height) throws IOException
{
if (inTextMode)
{
throw new IllegalStateException ("Error: drawImage is not allowed within a text block.");
}
saveGraphicsState ();
final AffineTransform transform = new AffineTransform (width, 0, 0, height, x, y);
transform (new Matrix (transform));
writeOperand (resources.add (image));
writeOperator ((byte) 'D', (byte) 'o');
restoreGraphicsState ();
}
示例8: drawImage
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {
AffineTransform tf = new AffineTransform();
tf.concatenate(baseTransform);
tf.concatenate(transform);
tf.concatenate((AffineTransform) xform.clone());
PDImageXObject pdImage = imageEncoder.encodeImage(document, contentStream, img);
try {
contentStreamSaveState();
int imgHeight = img.getHeight(obs);
tf.translate(0, imgHeight);
tf.scale(1, -1);
contentStream.transform(new Matrix(tf));
Object keyInterpolation = renderingHints.get(RenderingHints.KEY_INTERPOLATION);
if (RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR.equals(keyInterpolation))
pdImage.setInterpolate(false);
contentStream.drawImage(pdImage, 0, 0, img.getWidth(obs), imgHeight);
contentStreamRestoreState();
} catch (IOException e) {
throwException(e);
}
return true;
}
示例9: applyTexturePaint
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
private void applyTexturePaint(TexturePaint texturePaint) throws IOException {
Rectangle2D anchorRect = texturePaint.getAnchorRect();
PDTilingPattern pattern = new PDTilingPattern();
pattern.setPaintType(PDTilingPattern.PAINT_COLORED);
pattern.setTilingType(PDTilingPattern.TILING_CONSTANT_SPACING_FASTER_TILING);
pattern.setBBox(new PDRectangle((float) anchorRect.getX(), (float) anchorRect.getY(),
(float) anchorRect.getWidth(), (float) anchorRect.getHeight()));
pattern.setXStep((float) anchorRect.getWidth());
pattern.setYStep((float) anchorRect.getHeight());
AffineTransform patternTransform = new AffineTransform();
patternTransform.translate(0, anchorRect.getHeight());
patternTransform.scale(1f, -1f);
pattern.setMatrix(patternTransform);
PDAppearanceStream appearance = new PDAppearanceStream(document);
appearance.setResources(pattern.getResources());
appearance.setBBox(pattern.getBBox());
PDPageContentStream imageContentStream = new PDPageContentStream(document, appearance,
((COSStream) pattern.getCOSObject()).createOutputStream());
BufferedImage texturePaintImage = texturePaint.getImage();
PDImageXObject imageXObject = imageEncoder.encodeImage(document, imageContentStream, texturePaintImage);
float ratioW = (float) ((anchorRect.getWidth()) / texturePaintImage.getWidth());
float ratioH = (float) ((anchorRect.getHeight()) / texturePaintImage.getHeight());
float paintHeight = (texturePaintImage.getHeight()) * ratioH;
imageContentStream.drawImage(imageXObject, (float) anchorRect.getX(), (float) (paintHeight + anchorRect.getY()),
texturePaintImage.getWidth() * ratioW, -paintHeight);
imageContentStream.close();
PDColorSpace patternCS1 = new PDPattern(null, imageXObject.getColorSpace());
COSName tilingPatternName = resources.add(pattern);
PDColor patternColor = new PDColor(tilingPatternName, patternCS1);
contentStream.setNonStrokingColor(patternColor);
contentStream.setStrokingColor(patternColor);
}
示例10: barCode
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
public PaymentPdfBuilder barCode(final String barCodeMessage,
final float x,
final float y) throws IOException {
final float scale = 100f / BAR_CODE_DPI;
final Code128Bean code128Bean = new Code128Bean();
code128Bean.setCodeset(Code128Constants.CODESET_C);
code128Bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
code128Bean.setBarHeight(scale * BAR_CODE_HEIGHT_MM);
final BitmapCanvasProvider canvas = new BitmapCanvasProvider(
BAR_CODE_DPI, BufferedImage.TYPE_BYTE_BINARY, false, 0);
code128Bean.generateBarcode(canvas, barCodeMessage);
canvas.finish();
final PDImageXObject pdImage = LosslessFactory.createFromImage(pdfDocument, canvas.getBufferedImage());
try (final PDPageContentStream contentStream = new PDPageContentStream(
pdfDocument, pdfDocument.getPage(0), PDPageContentStream.AppendMode.APPEND, true)) {
contentStream.drawImage(pdImage, x, y, scale * pdImage.getWidth(), scale * pdImage.getHeight());
}
return this;
}
示例11: put
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
@Override
public void put(COSObject indirect, PDXObject xobject) throws IOException {
final int length = xobject.getStream().getLength();
if (length > Environment.getMaxImageSize()) {
LOG.trace("Not caching image with Size: {}", length);
return;
}
if (xobject instanceof PDImageXObject) {
PDImageXObject imageObj = (PDImageXObject) xobject;
if (imageObj.getWidth() * imageObj.getHeight() > Environment.getMaxImageSize()) {
return;
}
}
this.xobjects.put(indirect, new SoftReference(xobject));
}
示例12: drawImage
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
private void drawImage(PDPage page, Attendee attendee) throws IOException {
String imageFilename = BadgeLib.getStaffImageFilename(attendee);
if (imageFilename == null) {
imageFilename = BadgeLib.getMascotImageFilename();
}
PDImageXObject xImage;
try (PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, false)) {
xImage = PDImageXObject.createFromFile(imageFilename, document);
Dimension scaledDim = getScaledDimension(
new Dimension(xImage.getWidth(), xImage.getHeight()),
new Dimension(149, 158));
stream.drawImage(xImage,
150 + ((149-scaledDim.width)/2),
334 + ((158-scaledDim.height)/2),
scaledDim.width, scaledDim.height);
// stream.fillRect(150, 334, 149, 158);
stream.close();
} catch (IIOException ex) {
System.out.println("Error opening " + imageFilename);
}
}
示例13: testPlaceByBoundingBox
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
/**
* <a href="https://stackoverflow.com/questions/47383506/pdfbox-obtain-bounding-box-of-rotated-image">
* PDFBox - obtain bounding box of rotated image
* </a>
* <p>
* This test demonstrates how to position images given their dimensions,
* rotation angle, and the coordinates of the lower left corner of their
* bounding box. The work horse is {@link #placeImage(PDDocument, PDPage,
* PDImageXObject, float, float, float, float, float)}.
* </p>
*/
@Test
public void testPlaceByBoundingBox() throws IOException {
try ( InputStream resource = getClass().getResourceAsStream("Willi-1.jpg");
PDDocument document = new PDDocument() ) {
PDPage page = new PDPage();
document.addPage(page);
PDRectangle mediaBox = page.getMediaBox();
float bbLowerLeftX = 50;
float bbLowerLeftY = 100;
try ( PDPageContentStream contentStream = new PDPageContentStream(document, page) ) {
contentStream.moveTo(bbLowerLeftX, mediaBox.getLowerLeftY());
contentStream.lineTo(bbLowerLeftX, mediaBox.getUpperRightY());
contentStream.moveTo(mediaBox.getLowerLeftX(), bbLowerLeftY);
contentStream.lineTo(mediaBox.getUpperRightX(), bbLowerLeftY);
contentStream.stroke();
}
PDImageXObject image = PDImageXObject.createFromByteArray(document, IOUtils.toByteArray(resource), "Image");
placeImage(document, page, image, bbLowerLeftX, bbLowerLeftY, image.getWidth(), image.getHeight(), (float)(Math.PI/4));
placeImage(document, page, image, bbLowerLeftX, bbLowerLeftY, .5f*image.getWidth(), .5f*image.getHeight(), 0);
placeImage(document, page, image, bbLowerLeftX, bbLowerLeftY, .25f*image.getWidth(), .25f*image.getHeight(), (float)(9*Math.PI/8));
document.save(new File(RESULT_FOLDER, "rotatedImagesByBoundingBox.pdf"));
}
}
示例14: testExtractPageImageResources10948
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/40531871/how-can-i-check-if-pdf-page-is-imagescanned-by-pdfbox-xpdf">
* How can I check if PDF page is image(scanned) by PDFBOX, XPDF
* </a>
* <br/>
* <a href="https://drive.google.com/file/d/0B9izTHWJQ7xlT2ZoQkJfbGRYcFE">
* 10948.pdf
* </a>
* <p>
* The only special thing about the two images returned for the sample PDF is that
* one image is merely a mask used for the other image, and the other image is the
* actual image used on the PDF page. If one only wants the images immediately used
* in the page content, one also has to scan the page content.
* </p>
*/
@Test
public void testExtractPageImageResources10948() throws IOException
{
try ( InputStream resource = getClass().getResourceAsStream("10948.pdf"))
{
PDDocument document = PDDocument.load(resource);
int page = 1;
for (PDPage pdPage : document.getPages())
{
PDResources resources = pdPage.getResources();
if (resource != null)
{
int index = 0;
for (COSName cosName : resources.getXObjectNames())
{
PDXObject xobject = resources.getXObject(cosName);
if (xobject instanceof PDImageXObject)
{
PDImageXObject image = (PDImageXObject)xobject;
File file = new File(RESULT_FOLDER, String.format("10948-%s-%s.%s", page, index, image.getSuffix()));
ImageIO.write(image.getImage(), image.getSuffix(), file);
index++;
}
}
}
page++;
}
}
}
示例15: testExtractPageImageResources10948New
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/40531871/how-can-i-check-if-pdf-page-is-imagescanned-by-pdfbox-xpdf">
* How can I check if PDF page is image(scanned) by PDFBOX, XPDF
* </a>
* <br/>
* <a href="https://drive.google.com/open?id=0B9izTHWJQ7xlYi1XN1BxMmZEUGc">
* 10948.pdf
* </a>, renamed "10948-new.pdf" here to prevent a collision
* <p>
* Here the code extracts no image at all because the images are not immediate page
* resources but wrapped in form xobjects.
* </p>
*/
@Test
public void testExtractPageImageResources10948New() throws IOException
{
try ( InputStream resource = getClass().getResourceAsStream("10948-new.pdf"))
{
PDDocument document = PDDocument.load(resource);
int page = 1;
for (PDPage pdPage : document.getPages())
{
PDResources resources = pdPage.getResources();
if (resource != null)
{
int index = 0;
for (COSName cosName : resources.getXObjectNames())
{
PDXObject xobject = resources.getXObject(cosName);
if (xobject instanceof PDImageXObject)
{
PDImageXObject image = (PDImageXObject)xobject;
File file = new File(RESULT_FOLDER, String.format("10948-new-%s-%s.%s", page, index, image.getSuffix()));
ImageIO.write(image.getImage(), image.getSuffix(), file);
index++;
}
}
}
page++;
}
}
}