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;
}
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;
}
}
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;
}
}
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));
}
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;
}
Aggregations