簡體   English   中英

從字節流創建圖像的最快方法是什么?

[英]What is the the fastest way to create image from bytes stream?

我有一個電子設備(一種掃描儀),可以通過wifi將字節發送到android設備。 在Android中,我正在字節並創建整數數組(每個數字代表像素)。 然后我用位圖類創建png文件。 圖像看起來應該是正確的。 我正在單獨的線程中處理圖像。 我的目標是創建新圖像並以最快的方式替換舊圖像。 實際上,我想創造電影的錯覺。

問題是創建圖像花了太多時間。

這是相關的代碼

Bitmap mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(mBitmap);
Paint paint = new Paint(); 

...

int count = mBufferIn.read(buffer);
while(count != -1)
{
for(int i=0;i<count;i=i+2)
{
    k = ((buffer[i] << 8) & 0x0000ff00) | (buffer[i+1] & 0x000000ff);
    Log.e("TCP Client", "k=" + Integer.toString(k) );   
    colors[pixelIndex]=k;
    pixelIndex++;
}
count = mBufferIn.read(buffer);

if(count ==-1)
{
         try {
                 pixelIndex=0;
             c.drawBitmap(colors, 0, width, 0, 0, width, height, false, paint);

...

我將代碼更改為此:

try {
                    while((numberOfRead =mBufferIn.read(buffer)) != -1)
                    {
                        babff.append(buffer, 0, numberOfRead);
                        //offset = offset + numberOfRead;
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                byte [] imageBytes = babff.toByteArray();

                BitmapFactory.Options opt = new BitmapFactory.Options();
                opt.outHeight = height;
                opt.outWidth = width;

                Bitmap bitmap = BitmapFactory.decodeByteArray (imageBytes, 0, imageBytes.length,opt);

                Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
                Canvas canvas = new Canvas(bitmap);
                canvas.drawBitmap(bitmap, 0, 0, paint1); 

現在的問題是位圖為NULL

逐像素生成圖像設置非常慢。 讀取字節數組中的所有內容,然后從字節數組創建位圖

暫無
暫無

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

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