如何在 J2ME 中将图像转换为字节数组?
我的要求是这样的。我需要使用文件连接从手机读取文件,创建该图像的缩略图并将其发布到服务器。我能够使用 FileConnection API 读取图像,并且还能够创建缩略图。
创建缩略图后,我找不到将该图像转换回 byte[] 的方法。是否可以?
缩略图转换代码:
private Image createThumbnail(Image image) {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
int thumbWidth = 128;
int thumbHeight = -1;
if (thumbHeight == -1)
thumbHeight = thumbWidth * sourceHeight / sourceWidth;
Image thumb = Image.createImage(thumbWidth, thumbHeight);
thumb.getGraphics();
Graphics g = thumb.getGraphics();
for (int y = 0; y < thumbHeight; y++) {
for (int x = 0; x < thumbWidth; x++) {
g.setClip(x, y, 1, 1);
int dx = x * sourceWidth / thumbWidth;
int dy = y * sourceHeight / thumbHeight;
g.drawImage(image, x - dx, y - dy);
}
}
Image immutableThumb = Image.createImage(thumb);
return thumb;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MIDP2.0的Image.getRGB() 是你的朋友。您可以按如下方式获取 int 数组形式的 ARGB 像素数据:
然后可以将 int 数组用作 Image.createRGBImage(),或在桌面 Java 中,
BufferedImage
可以使用如下:MIDP2.0's Image.getRGB() is your friend. You can obtain the ARGB pixel data as an int array as follows:
The int array can then be used as a parameter to Image.createRGBImage(), or in desktop Java,
BufferedImage
can be used as follows: