Search in sources :

Example 1 with JpegImageFormat

use of com.serotonin.util.image.JpegImageFormat in project ma-core-public by infiniteautomation.

the class ImageValueServlet method doGet.

/**
 * @TODO(security): Validate the point access against the user. If anonymous, make sure the view allows public
 *                  access to the point.
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String imageInfo = request.getPathInfo();
    try {
        // Remove the / and the extension
        int dot = imageInfo.indexOf('.');
        String extension = imageInfo.substring(dot + 1, imageInfo.length()).toLowerCase();
        imageInfo = imageInfo.substring(1, dot);
        // Split by underscore.
        String[] imageBits = imageInfo.split("_");
        // Get the data.
        String timestamp = imageBits[0];
        int dataPointId = Integer.parseInt(imageBits[1]);
        int scalePercent = getIntRequestParameter(request, "p", -1);
        int width = getIntRequestParameter(request, "w", -1);
        int height = getIntRequestParameter(request, "h", -1);
        // DataPointRT dp = Common.ctx.getRuntimeManager().getDataPoint(dataPointId);
        // Permissions.ensureDataPointReadPermission(Common.getUser(request), dp.getVO());
        PointValueFacade pointValueFacade = new PointValueFacade(dataPointId);
        PointValueTime pvt = null;
        if (timestamp.startsWith(historyPrefix)) {
            // Find the point with the given timestamp
            long time = Long.parseLong(timestamp.substring(historyPrefix.length()));
            pvt = pointValueFacade.getPointValueAt(time);
        } else
            // Use the latest value
            pvt = pointValueFacade.getPointValue();
        if (pvt == null || pvt.getValue() == null || !(pvt.getValue() instanceof ImageValue)) {
            LOG.warn("Invalid pvt: " + pvt);
            response.sendError(HttpStatus.SC_NOT_FOUND);
        } else {
            ImageValue imageValue = (ImageValue) pvt.getValue();
            byte[] data = imageValue.getImageData();
            if (scalePercent != -1) {
                // Definitely going to be JPEG
                response.setContentType(MediaType.IMAGE_JPEG_VALUE);
                // Scale the image
                PercentScaledImage scaler = new PercentScaledImage(((float) scalePercent) / 100);
                data = ImageUtils.scaleImage(scaler, data, new JpegImageFormat(0.85f));
            } else if (width != -1 && height != -1) {
                // Definitely going to be JPEG
                response.setContentType(MediaType.IMAGE_JPEG_VALUE);
                // Scale the image
                BoxScaledImage scaler = new BoxScaledImage(width, height);
                data = ImageUtils.scaleImage(scaler, data, new JpegImageFormat(0.85f));
            } else {
                // Use the Image extension to se the Content Type
                if ("jpg".equals(extension))
                    response.setContentType(MediaType.IMAGE_JPEG_VALUE);
                else if ("png".equals(extension))
                    response.setContentType(MediaType.IMAGE_PNG_VALUE);
                else if ("gif".equals(extension))
                    response.setContentType(MediaType.IMAGE_GIF_VALUE);
            }
            response.getOutputStream().write(data);
        }
    } catch (FileNotFoundException e) {
        LOG.warn("", e);
    } catch (InterruptedException e) {
        LOG.warn("", e);
    } catch (StringIndexOutOfBoundsException e) {
        LOG.warn("", e);
    } catch (NumberFormatException e) {
        LOG.warn("", e);
    } catch (ArrayIndexOutOfBoundsException e) {
        LOG.warn("", e);
    } catch (IllegalArgumentException e) {
        LOG.warn("", e);
    }
}
Also used : PointValueFacade(com.serotonin.m2m2.rt.dataImage.PointValueFacade) FileNotFoundException(java.io.FileNotFoundException) JpegImageFormat(com.serotonin.util.image.JpegImageFormat) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) BoxScaledImage(com.serotonin.util.image.BoxScaledImage) ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue) PercentScaledImage(com.serotonin.util.image.PercentScaledImage)

Aggregations

PointValueFacade (com.serotonin.m2m2.rt.dataImage.PointValueFacade)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)1 BoxScaledImage (com.serotonin.util.image.BoxScaledImage)1 JpegImageFormat (com.serotonin.util.image.JpegImageFormat)1 PercentScaledImage (com.serotonin.util.image.PercentScaledImage)1 FileNotFoundException (java.io.FileNotFoundException)1