Search in sources :

Example 1 with RawType

use of org.openhab.core.library.types.RawType in project openhab-addons by openhab.

the class ChromecastStatusUpdater method downloadImageFromCache.

@Nullable
private RawType downloadImageFromCache(String url) {
    if (IMAGE_CACHE.containsKey(url)) {
        try {
            byte[] bytes = IMAGE_CACHE.get(url);
            String contentType = HttpUtil.guessContentTypeFromData(bytes);
            return new RawType(bytes, contentType == null || contentType.isEmpty() ? RawType.DEFAULT_MIME_TYPE : contentType);
        } catch (IOException e) {
            logger.trace("Failed to download the content of URL '{}'", url, e);
        }
    } else {
        RawType image = downloadImage(url);
        if (image != null) {
            IMAGE_CACHE.put(url, image.getBytes());
            return image;
        }
    }
    return null;
}
Also used : RawType(org.openhab.core.library.types.RawType) IOException(java.io.IOException) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with RawType

use of org.openhab.core.library.types.RawType in project openhab-addons by openhab.

the class LGHomBotHandler method parseMap.

private void parseMap() {
    if (!isLinked(CHANNEL_MAP)) {
        return;
    }
    final int tileSize = 10;
    final int tileArea = tileSize * tileSize;
    final int rowLength = 100;
    final int scale = 1;
    String blackBox = findBlackBoxFile();
    String url = buildHttpAddress(blackBox);
    RawType dlData = HttpUtil.downloadData(url, null, false, -1);
    if (dlData == null) {
        return;
    }
    byte[] mapData = dlData.getBytes();
    final int tileCount = mapData[32];
    int maxX = 0;
    int maxY = 0;
    int minX = 0x10000;
    int minY = 0x10000;
    int pixPos;
    for (int i = 0; i < tileCount; i++) {
        pixPos = (mapData[52 + i * 16] & 0xFF) + (mapData[52 + 1 + i * 16] << 8);
        int xPos = (pixPos % rowLength) * tileSize;
        int yPos = (pixPos / rowLength) * tileSize;
        if (xPos < minX) {
            minX = xPos;
        }
        if (xPos > maxX) {
            maxX = xPos;
        }
        if (yPos > maxY) {
            maxY = yPos;
        }
        if (yPos < minY) {
            minY = yPos;
        }
    }
    final int width = (tileSize + maxX - minX) * scale;
    final int height = (tileSize + maxY - minY) * scale;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            image.setRGB(j, i, 0xFFFFFF);
        }
    }
    for (int i = 0; i < tileCount; i++) {
        pixPos = (mapData[52 + i * 16] & 0xFF) + (mapData[52 + 1 + i * 16] << 8);
        int xPos = ((pixPos % rowLength) * tileSize - minX) * scale;
        int yPos = (maxY - (pixPos / rowLength) * tileSize) * scale;
        int indexTab = 16044 + i * tileArea;
        for (int j = 0; j < tileSize; j++) {
            for (int k = 0; k < tileSize; k++) {
                int p = 0xFFFFFF;
                if ((mapData[indexTab] & 0xF0) != 0) {
                    p = 0xFF0000;
                } else if (mapData[indexTab] != 0) {
                    p = 0xBFBFBF;
                }
                image.setRGB(xPos + k * scale, yPos + (9 - j) * scale, p);
                indexTab++;
            }
        }
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        if (!ImageIO.write(image, "png", baos)) {
            logger.debug("Couldn't find PNG writer.");
        }
    } catch (IOException e) {
        logger.info("IOException creating PNG image.", e);
    }
    byte[] byteArray = baos.toByteArray();
    if (byteArray != null && byteArray.length > 0) {
        currentMap = new RawType(byteArray, "image/png");
    } else {
        currentMap = UnDefType.UNDEF;
    }
}
Also used : RawType(org.openhab.core.library.types.RawType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 3 with RawType

use of org.openhab.core.library.types.RawType in project openhab-addons by openhab.

the class CameraUtil method parseImageFromBytes.

/**
 * This converts a non-interleaved YUV-422 image to a JPEG image.
 *
 * @param yuvData The uncompressed YUV data
 * @param width The width of image.
 * @param height The height of the image.
 * @return A JPEG image as a State
 */
static State parseImageFromBytes(byte[] yuvData, int width, int height) {
    final int size = width * height;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    for (int i = 0; i < size; i++) {
        double y = yuvData[i] & 0xFF;
        double u = yuvData[size + i / 2] & 0xFF;
        double v = yuvData[(int) (size * 1.5 + i / 2.0)] & 0xFF;
        // red
        int r = Math.min(Math.max((int) (y + 1.371 * (v - 128)), 0), 255);
        // green
        int g = Math.min(Math.max((int) (y - 0.336 * (u - 128) - 0.698 * (v - 128)), 0), 255);
        // blue
        int b = Math.min(Math.max((int) (y + 1.732 * (u - 128)), 0), 255);
        // pixel
        int p = (r << 16) | (g << 8) | b;
        image.setRGB(i % width, i / width, p);
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        if (!ImageIO.write(image, "jpg", baos)) {
            logger.debug("Couldn't find JPEG writer.");
        }
    } catch (IOException e) {
        logger.info("IOException creating JPEG image.", e);
    }
    byte[] byteArray = baos.toByteArray();
    if (byteArray != null && byteArray.length > 0) {
        return new RawType(byteArray, "image/jpeg");
    } else {
        return UnDefType.UNDEF;
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RawType(org.openhab.core.library.types.RawType) BufferedImage(java.awt.image.BufferedImage)

Example 4 with RawType

use of org.openhab.core.library.types.RawType in project openhab-addons by openhab.

the class ImageItemConverter method process.

@Override
public void process(Content content) {
    String mediaType = content.getMediaType();
    updateState.accept(new RawType(content.getRawContent(), mediaType != null ? mediaType : RawType.DEFAULT_MIME_TYPE));
}
Also used : RawType(org.openhab.core.library.types.RawType)

Example 5 with RawType

use of org.openhab.core.library.types.RawType in project openhab-addons by openhab.

the class DoorbellHandler method getImages.

// Get an array list of history images
private ArrayList<BufferedImage> getImages(String channelId) {
    ArrayList<BufferedImage> images = new ArrayList<>();
    Integer numberOfImages = config.montageNumImages;
    if (numberOfImages != null) {
        for (int imageNumber = 1; imageNumber <= numberOfImages; imageNumber++) {
            logger.trace("Downloading montage image {} for channel '{}'", imageNumber, channelId);
            DoorbirdImage historyImage = CHANNEL_DOORBELL_IMAGE_MONTAGE.equals(channelId) ? api.downloadDoorbellHistoryImage(String.valueOf(imageNumber)) : api.downloadMotionHistoryImage(String.valueOf(imageNumber));
            if (historyImage != null) {
                RawType image = historyImage.getImage();
                if (image != null) {
                    try {
                        BufferedImage i = ImageIO.read(new ByteArrayInputStream(image.getBytes()));
                        images.add(i);
                    } catch (IOException e) {
                        logger.debug("IOException creating BufferedImage from downloaded image: {}", e.getMessage());
                    }
                }
            }
        }
        if (images.size() < numberOfImages) {
            logger.debug("Some images could not be downloaded: wanted={}, actual={}", numberOfImages, images.size());
        }
    }
    return images;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DoorbirdImage(org.openhab.binding.doorbird.internal.api.DoorbirdImage) ArrayList(java.util.ArrayList) RawType(org.openhab.core.library.types.RawType) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Aggregations

RawType (org.openhab.core.library.types.RawType)48 IOException (java.io.IOException)12 Nullable (org.eclipse.jdt.annotation.Nullable)9 State (org.openhab.core.types.State)8 DoorbirdImage (org.openhab.binding.doorbird.internal.api.DoorbirdImage)7 BufferedImage (java.awt.image.BufferedImage)5 Test (org.junit.jupiter.api.Test)5 DecimalType (org.openhab.core.library.types.DecimalType)4 StringType (org.openhab.core.library.types.StringType)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)3 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Request (org.eclipse.jetty.client.api.Request)2 ImageItem (org.openhab.core.library.items.ImageItem)2 DateTimeType (org.openhab.core.library.types.DateTimeType)2