我已经从图片网址(lcdui图片)
创建了一个图片HttpConnection c = (HttpConnection) Connector.open(imageurl);
int len = (int)c.getLength();
if (len > 0)
{
is = c.openDataInputStream();
byte[] data = new byte[len];
is.readFully(data);
img = Image.createImage(data, 0, len);
我想为此设置高度和宽度?我想显示
答案 0 :(得分:1)
您无需设置宽度和高度,因为在图像加载期间会加载和设置此信息。因此,如果图像是320x100,您的代码将创建一个320x100图像。
img.getWidth()
将返回320. img.getHeight()
将返回100.
无法更改Image
对象的宽度和高度。您可以查询其宽度和高度。
您的图片已准备好在画布中的ImageItem
对象中显示。
答案 1 :(得分:0)
你无法设置宽度和高度到Image。但是,可以使用以下方法调整图像大小。
public Image resizeImage(Image src, int screenHeight, int screenWidth) {
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
Image tmp = Image.createImage(screenWidth, srcHeight);
Graphics g = tmp.getGraphics();
int ratio = (srcWidth << 16) / screenWidth;
int pos = ratio / 2;
//Horizontal Resize
for (int index = 0; index < screenWidth; index++) {
g.setClip(index, 0, 1, srcHeight);
g.drawImage(src, index - (pos >> 16), 0);
pos += ratio;
}
Image resizedImage = Image.createImage(screenWidth, screenHeight);
g = resizedImage.getGraphics();
ratio = (srcHeight << 16) / screenHeight;
pos = ratio / 2;
//Vertical resize
for (int index = 0; index < screenHeight; index++) {
g.setClip(0, index, screenWidth, 1);
g.drawImage(tmp, 0, index - (pos >> 16));
pos += ratio;
}
return resizedImage;
}
答案 2 :(得分:0)
接受的答案对我没有用(因为它在缩小图像尺寸时沿图像底部留下了一条白色条带 - 尽管保持相同的宽高比)。我找到了一个可以使用CodeRanch forum。
的代码段以下是该片段,已清理完毕:
protected static Image resizeImage(Image image, int resizedWidth, int resizedHeight) {
int width = image.getWidth();
int height = image.getHeight();
int[] in = new int[width];
int[] out = new int[resizedWidth * resizedHeight];
int dy, dx;
for (int y = 0; y < resizedHeight; y++) {
dy = y * height / resizedHeight;
image.getRGB(in, 0, width, 0, dy, width, 1);
for (int x = 0; x < resizedWidth; x++) {
dx = x * width / resizedWidth;
out[(resizedWidth * y) + x] = in[dx];
}
}
return Image.createRGBImage(out, resizedWidth, resizedHeight, true);
}