将1D int []数组转换为1D byte []数组

时间:2015-11-08 13:53:39

标签: java arrays matlab buffer bytestream

我的项目出了问题。我的程序将字节加载到缓冲区(它的1D字节数组),当我想设置数据的偏移量时,我创建了一个2D int[]数组,然后我将其转换为1D int数组,最后一步是将1D int数组转换为1D字节数组。但结果与我的matlab函数不同。我把我的代码放在下面,希望有人能给我一些提示。

public byte[] sekwencja(String nazwa) throws FileNotFoundException, IOException{

    InputStream is = null;
    DataInputStream dis = null;
    is = new FileInputStream(nazwa);
    dis = new DataInputStream(is);
    int length = dis.available();
    byte[] buf = new byte[length];
    dis.readFully(buf);


    int l_klatek = ((length-158864)/158864)+1;
    int width = 320;
    int height = 240;
    int C1=21764040;
    double C2=3033.3;
    double C3=134.06;
    int z = 0;
    int[] oneDArray = new int[width*height];
    byte[] rawPixels2 = new byte[oneDArray.length];
    int [][] pixels = new int[width][height];
    int offset = 1360;
    int wart;

    for(int x = 0; x<320; x++){
        for (int y = 0; y<240;y++){ 

            //int wart = buf[offset & 0xFF] ;
            //int wart = buf[offset];
            wart =(buf[offset& 0xFF + 1] )*256 + buf[offset& 0xFF ]  ;

            pixels[x][y]= (int) (C2/log(C3+C1/(wart+1))-273.15);


            //rawPixels2[z++] =  (byte) (0xFF & oneDArray[x]);
            offset = offset+2;



        }
    }
    for(int i = 0; i < width; i ++)
    {
        for(int s = 0; s <height; s ++)
        {
             oneDArray[(i * height) + s] = pixels[i][s];
        }
    }
     ByteBuffer byteBuffer = ByteBuffer.allocate(oneDArray.length * 4);
     IntBuffer intBuffer = byteBuffer.asIntBuffer();
     intBuffer.put(oneDArray);

     byte[] array = byteBuffer.array();

    return array;

Matlab有趣

function macierz_2D = pokaz_img_nr(nazwa_pliku,nr_klatki)

%wyswietla klatke z sekwencji
%close all

fid = fopen(nazwa_pliku,'r');
[sekwencja,l_bajtow] = fread(fid, inf, 'uchar');
l_klatek=(l_bajtow-158864)/158864 +1;

%stale kalibracyjne kamery
C1=21764040;
C2=3033.3;
C3=134.06;


clk=clock;
offset=(nr_klatki-1).*158864 + 1373;
%offset=1360;
%if (nr_klatki>1)
  % offset=158136+(nr_klatki-1)*569+(nr_klatki-2)*(320*240*2+3839);
%end 
 for nr_y=1:240
     for nr_x=1:320
        wartosc=sekwencja(offset);
        wartosc= (sekwencja(offset))+(sekwencja(offset+1))*256;
        %macierz_2D(nr_y,nr_x) = wartosc;
        macierz_2D(nr_y,nr_x)=C2/log(C3+C1/wartosc)-273.15;
        offset=offset+2;
     end
  end

liczba_minut = etime(clock,clk)/60
clear sekwencja


%colormap(hot)
%colormap((jet))
im = imagesc(macierz_2D(:,:));
imagesc(macierz_2D(:,:));
saveas(gcf,'im.png');
grid on;
%caxis([9000 12500]);
axis([1 320 1 240]);
axis image;
colorbar('horiz');

0 个答案:

没有答案