[英]Does anyone know how to display a BitmapField?
I have a code that encode a image to bitmap. 我有一个将图像编码为位图的代码。 But I am not sure on how to display it on the screen. 但是我不确定如何在屏幕上显示它。 By the way, I am using Blackberry JavaME. 顺便说一句,我正在使用Blackberry JavaME。 This is the code where i use to encode the image. 这是我用来编码图像的代码。 Is this the way I can use in order to get image from a sd card and display it on the screen? 这是我可以用来从SD卡中获取图像并将其显示在屏幕上的方法吗?
FileConnection conn =
(FileConnection)Connector.open("image1.png",Connector.READ_WRITE);
if(conn.exists()) {
InputStream is = conn.openInputStream();
BitmapField bitmap = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1)
{
baos.write(ch);
}
byte imageData[] = baos.toByteArray();
bitmap = new BitmapField(
EncodedImage.createEncodedImage(imageData, 0, imageData.length).getBitmap());
add(bitmap);
}
Secko is correct that you can override paint but I think you are getting stuck because you have not created a ui application. Secko是正确的,您可以覆盖绘画,但我认为您会陷入困境,因为尚未创建ui应用程序。
Here is a very simple ui application example for displaying a bitmap field, if you copy it exactly you will need an images folder under src with image.png inside of it. 这是一个用于显示位图字段的非常简单的ui应用程序示例,如果要精确复制它,则将需要src下的images文件夹,其中包含image.png。
This was modified from the HelloWorldDemo that comes with the SDK. 这是从SDK附带的HelloWorldDemo修改而来的。 I recommend that if you are just starting out you look at the samples in the folder. 我建议如果您刚开始,请查看文件夹中的示例。 \\plugins\\net.rim.ejde.componentpack5.0.0_5.0.0.25\\components\\samples\\ \\ plugins \\ net.rim.ejde.componentpack5.0.0_5.0.0.25 \\ components \\ samples \\
good luck 祝好运
Ray 射线
public class DisplayBitmaps extends UiApplication
{
public static void main(String[] args)
{
DisplayBitmaps theApp = new DisplayBitmaps();
theApp.enterEventDispatcher();
}
public DisplayBitmaps()
{
pushScreen(new DisplayBitmapsScreen());
}
}
final class DisplayBitmapsScreen extends MainScreen
{
DisplayBitmapsScreen()
{
Bitmap bitmap = EncodedImage.getEncodedImageResource("images/image.png").getBitmap();
BitmapField bitmapField = new BitmapField(bitmap);
add(bitmapField);
}
public void close()
{
super.close();
}
}
Edit for when the image is on the sdcard 编辑图像何时在sdcard上
DisplayBitmapsScreen()
{
//Bitmap bitmap = EncodedImage.getEncodedImageResource("images/image.png").getBitmap();
try {
FileConnection fc = (FileConnection) Connector.open("file:///SDCard/BlackBerry/pictures/image.png");
if (fc.exists()) {
byte[] image = new byte[(int) fc.fileSize()];
InputStream inStream = fc.openInputStream();
inStream.read(image);
inStream.close();
EncodedImage encodedImage = EncodedImage.createEncodedImage(image, 0, -1);
BitmapField bitmapField = new BitmapField(encodedImage.getBitmap());
fc.close();
add(bitmapField);
}
} catch (Exception e) { System.out.println("EXCEPTION " + e); }
}
Overriding paint in Field or any extension Field class could also display an image, but I didn't really understand from Secko's example where he would display the image so I have included drawImage in this example below. 在Field或任何扩展Field类中覆盖涂料也可以显示图像,但是我从Secko的示例中并不真正理解他将在何处显示图像,因此我在下面的示例中包括了drawImage。
protected void paint(Graphics graphics) {
graphics.drawImage(x, y, width, height, image, frameIndex, left, top);
super.paint(graphics);
}
You can do it with: 您可以执行以下操作:
paint(Graphics g); 油漆(图形g);
Perhaps in a function: 也许在一个函数中:
protected void DrawStuff(Graphics g) {
this.paint(g);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.