Search in sources :

Example 1 with ImageWorker

use of digilib.image.ImageWorker in project digilib by robcast.

the class PDFStreamWorker method addImage.

/**
 * adds an image to the document.
 *
 * @param doc
 * @param iji
 * @return
 * @throws InterruptedException
 * @throws ExecutionException
 * @throws IOException
 * @throws DocumentException
 */
public Document addImage(Document doc, ImageJobDescription iji) throws InterruptedException, ExecutionException, IOException, DocumentException {
    // create image worker
    ImageWorker job = new ImageWorker(dlConfig, iji);
    // submit
    Future<DocuImage> jobTicket = imageJobCenter.submit(job);
    // wait for result
    DocuImage img = jobTicket.get();
    // scale the image
    Image pdfimg = Image.getInstance(img.getAwtImage(), null);
    float docW = PageSize.A4.getWidth() - 2 * PageSize.A4.getBorder();
    float docH = PageSize.A4.getHeight() - 2 * PageSize.A4.getBorder();
    // fit the image to the page
    pdfimg.scaleToFit(docW, docH);
    // add to PDF
    doc.add(pdfimg);
    return doc;
}
Also used : ImageWorker(digilib.image.ImageWorker) DocuImage(digilib.image.DocuImage) DocuImage(digilib.image.DocuImage) Image(com.itextpdf.text.Image)

Example 2 with ImageWorker

use of digilib.image.ImageWorker in project digilib by robcast.

the class ScalerNoThread method processRequest.

/**
 * Service this request using the response.
 *
 * @param request
 * @param response
 * @throws ServletException
 */
public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    if (dlConfig == null) {
        logger.error("ERROR: No Configuration!");
        throw new ServletException("NO VALID digilib CONFIGURATION!");
    }
    accountlog.debug("request: " + request.getQueryString());
    logger.debug("request: " + request.getQueryString());
    long startTime = System.currentTimeMillis();
    // parse request
    DigilibServletRequest dlRequest = new DigilibServletRequest(request, dlConfig);
    // type of error reporting
    ErrMsg errMsgType = ErrMsg.IMAGE;
    if (dlRequest.hasOption(DigilibOption.errtxt)) {
        errMsgType = ErrMsg.TEXT;
    } else if (dlRequest.hasOption(DigilibOption.errcode)) {
        errMsgType = ErrMsg.CODE;
    }
    try {
        // extract the job information
        ImageJobDescription jobTicket = ImageJobDescription.getInstance(dlRequest, dlConfig);
        /*
             * check if we can fast-track without scaling
             */
        ImageInput fileToLoad = (ImageInput) jobTicket.getInput();
        // check permissions
        if (useAuthorization) {
            // is the current request/user authorized?
            if (!authzOp.isAuthorized(dlRequest)) {
                // send deny answer and abort
                throw new AuthOpException();
            }
        }
        // if requested, send image as a file
        if (sendFileAllowed && jobTicket.getSendAsFile()) {
            String mt = null;
            if (jobTicket.hasOption(DigilibOption.rawfile)) {
                mt = "application/octet-stream";
            }
            logger.debug("Sending RAW File as is.");
            ServletOps.sendFile(fileToLoad.getFile(), mt, null, response, logger);
            logger.info("Done in " + (System.currentTimeMillis() - startTime) + "ms");
            return;
        }
        // it
        if (!jobTicket.isTransformRequired()) {
            logger.debug("Sending File as is.");
            ServletOps.sendFile(fileToLoad.getFile(), null, null, response, logger);
            logger.info("Done in " + (System.currentTimeMillis() - startTime) + "ms");
            return;
        }
        // create job
        ImageWorker job = new ImageWorker(dlConfig, jobTicket);
        // get result immediately
        DocuImage img = job.call();
        // forced destination image type
        String mt = null;
        if (jobTicket.hasOption(DigilibOption.jpg)) {
            mt = "image/jpeg";
        } else if (jobTicket.hasOption(DigilibOption.png)) {
            mt = "image/png";
        }
        // send image
        ServletOps.sendImage(img, mt, response, logger);
        logger.debug("Job Processing Time: " + (System.currentTimeMillis() - startTime) + "ms");
    } catch (ImageOpException e) {
        logger.error(e.getClass() + ": " + e.getMessage());
        digilibError(errMsgType, Error.IMAGE, null, response);
    } catch (IOException e) {
        logger.error(e.getClass() + ": " + e.getMessage());
        digilibError(errMsgType, Error.FILE, null, response);
    } catch (AuthOpException e) {
        logger.error(e.getClass() + ": " + e.getMessage());
        digilibError(errMsgType, Error.AUTH, null, response);
    }
}
Also used : ServletException(javax.servlet.ServletException) ImageOpException(digilib.image.ImageOpException) DigilibServletRequest(digilib.conf.DigilibServletRequest) ImageJobDescription(digilib.image.ImageJobDescription) ImageInput(digilib.io.ImageInput) AuthOpException(digilib.auth.AuthOpException) ImageWorker(digilib.image.ImageWorker) DocuImage(digilib.image.DocuImage) IOException(java.io.IOException)

Aggregations

DocuImage (digilib.image.DocuImage)2 ImageWorker (digilib.image.ImageWorker)2 Image (com.itextpdf.text.Image)1 AuthOpException (digilib.auth.AuthOpException)1 DigilibServletRequest (digilib.conf.DigilibServletRequest)1 ImageJobDescription (digilib.image.ImageJobDescription)1 ImageOpException (digilib.image.ImageOpException)1 ImageInput (digilib.io.ImageInput)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1