2013-12-12 76 views
1

我有一個電子設備(種類的掃描儀),通過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

回答

0

以像素爲單位生成圖像設置非常慢。讀取字節數組中的所有內容並從字節數組創建位圖

相關問題