Skip to content

Commit

Permalink
Improve com.siemens.mp.lcdui.Image implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
woesss committed Sep 2, 2024
1 parent 23d4d1e commit 86d3435
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
61 changes: 56 additions & 5 deletions app/src/main/java/com/siemens/mp/lcdui/Image.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2018 Nikita Shakarun
* Copyright 2024 Yury Kharchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,38 +17,88 @@

package com.siemens.mp.lcdui;

import android.graphics.Bitmap;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;

import ru.playsoftware.j2meloader.util.PNGUtils;

public class Image extends com.siemens.mp.ui.Image {

public static javax.microedition.lcdui.Image createImageFromFile(String filename,
boolean scaleToFullScreen)
throws IOException {
return javax.microedition.lcdui.Image.createImage(filename);
if (scaleToFullScreen) {
return createImageFromFile(filename,
Displayable.getVirtualWidth(),
Displayable.getVirtualHeight());
} else {
return createImageFromFile(filename, 0, 0);
}
}

public static javax.microedition.lcdui.Image createImageFromFile(String filename,
int ScaleToWidth,
int ScaleToHeight)
throws IOException {
return javax.microedition.lcdui.Image.createImage(filename);
if (filename == null) {
throw new IllegalArgumentException();
} else if (filename.startsWith("\\") || filename.startsWith("/")) {
filename = "a:" + filename;
}
Bitmap bitmap;
try (InputStream stream = Connector.openInputStream("file:///" + filename)) {
bitmap = PNGUtils.getFixedBitmap(stream);
}
if (bitmap == null) {
throw new IOException("Can't decode image");
}
if (ScaleToWidth > 0) {
if (ScaleToHeight <= 0) {
ScaleToHeight = ScaleToWidth * bitmap.getHeight() / bitmap.getWidth();
}
bitmap = Bitmap.createScaledBitmap(bitmap, ScaleToWidth, ScaleToHeight, false);
} else if (ScaleToHeight > 0) {
ScaleToWidth = ScaleToHeight * bitmap.getWidth() / bitmap.getHeight();
bitmap = Bitmap.createScaledBitmap(bitmap, ScaleToWidth, ScaleToHeight, false);
}
return new javax.microedition.lcdui.Image(bitmap);
}

public static int getPixelColor(javax.microedition.lcdui.Image image, int x, int y)
throws IllegalArgumentException {
return image.getBitmap().getPixel(x, y);
}

public static void setPixelColor(
javax.microedition.lcdui.Image image, int x, int y, int color) throws IllegalArgumentException {
public static void setPixelColor(javax.microedition.lcdui.Image image, int x, int y, int color)
throws IllegalArgumentException {
image.getBitmap().setPixel(x, y, color);
}

public static void writeImageToFile(javax.microedition.lcdui.Image img, String file)
throws IOException {
if (img == null) {
throw new NullPointerException();
} else if (file == null) {
throw new IllegalArgumentException();
} else if (file.startsWith("\\") || file.startsWith("/")) {
file = "a:" + file;
}
try (OutputStream stream = Connector.openOutputStream("file:///" + file)) {
if (file.regionMatches(true, file.length() - 4, ".jpg", 0, 4)) {
img.getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, stream);
} else {
img.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, stream);
}
}
}

public static void writeBmpToFile(Image image, String filename)
public static void writeBmpToFile(javax.microedition.lcdui.Image image, String filename)
throws IOException {
writeImageToFile(image, filename);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import com.siemens.mp.io.file.FileConnection;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
Expand All @@ -57,7 +59,6 @@
import java.util.regex.Pattern;

import javax.microedition.io.file.ConnectionClosedException;
import javax.microedition.io.file.FileConnection;
import javax.microedition.util.ContextHolder;

public class FileSystemFileConnection implements FileConnection {
Expand Down

0 comments on commit 86d3435

Please sign in to comment.