簡體   English   中英

解碼圖像-BLACKBERRY / Java

[英]Decode Images - BLACKBERRY/Java

有誰知道如何將SD卡上的.png圖像解碼為黑莓上的位圖?

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() {  
        try {
            Thread.sleep(2000); // wait for the simulator SDCard to initialize in this example, on a phone this may take longer
            // for a better way see http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/1295814/How_To_-_Programmatically_determine_if_a_microSD_card_has_been_inserted.html?nodeid=1295868&vernum=0 
            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();
                //EncodedImages are useful for further file manipulation, otherwise you can go straight to Bitmap
                //EncodedImage encodedImage = EncodedImage.createEncodedImage(image, 0, -1); 
                //Bitmap bitmap = encodedImage.getBitmap();
                Bitmap bitmap = Bitmap.createBitmapFromBytes(image, 0, -1, 1);
                BitmapField bitmapField = new BitmapField(bitmap);
                fc.close();
                add(bitmapField);
            }
        } catch (Exception e) { System.out.println("EXCEPTION " + e); }
    }

    public void close()
    {  
        super.close();
    }   
}

上面的代碼是從SDCard讀取png文件到Bitmap並將其顯示在BitmapField中的示例。

干杯

射線

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM