schreiben/Lesen von float-array in java schnell Weg

Ich versuche, das Lesen und schreiben von float Arrays(ziemlich groß eigentlich, 640*480) in android-Geräte mit java-codes.
wie diese eine

    DataOutputStream out = ...;
for (int i=0; i<floatarray.length; ++i)
    out.writeFloat(floatarray[i]);

ist sehr langsam und ich habe versucht es in dieser.

SCHREIBEN:

            float[] test=new float[3];
        test[0]=1.0f;
        test[1]=1.2f;
        test[2]=1.5f;
        //long timeBeforeWrite = System.nanoTime();
        try {
            BufferedOutputStream  dataOut = new BufferedOutputStream (
                    new FileOutputStream("/sdcard/DCIM/Camera/Dual/demo.bin"));

            byte buf[]=new byte[4*test.length];

            long timeBeforeWrite = System.nanoTime();

            for (int i=0; i<test.length; ++i)
            {
                int val = Float.floatToRawIntBits(test[i]);
                buf[4 * i] = (byte) (val >> 24);
                buf[4 * i + 1] = (byte) (val >> 16) ;
                buf[4 * i + 2] = (byte) (val >> 8);
                buf[4 * i + 3] = (byte) (val);
            }



            dataOut.write(buf);

            long ct_write = System.nanoTime();
            long offset_write = ct_write - timeBeforeWrite;
            float mOffsetInMs_write = (float)(offset_write)/1000000;
            Log.e("ETA", "write float[]  " + Float.toString(mOffsetInMs_write));

            dataOut.flush();
            dataOut.close();
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
        }

LESEN:

            float[] read=new float[3];

        try{
            BufferedInputStream  dataIn=new BufferedInputStream (new FileInputStream("/sdcard/DCIM/Camera/Dual/demo.txt"));
            byte buf[]=new byte[4*read.length];
            long timeBeforeWrite = System.nanoTime();

            dataIn.read(buf);

            for (int i=0; i<read.length; ++i)
            {
                    int val;
val = buf[4 * i] << 24;
                    val += buf[4 * i + 1] << 16;
                    val += buf[4 * i + 2] << 8;
                    val += buf[4 * i + 3];

                read[i]=Float.valueOf(Integer.toBinaryString(val));

                //int val = Float.floatToRawIntBits(disparityMap[i]);

            }

            long ct_write = System.nanoTime();
            long offset_write = ct_write - timeBeforeWrite;
            float mOffsetInMs_write = (float)(offset_write)/1000000;
            Log.e("ETA", "read float[]  " + Float.toString(mOffsetInMs_write));

            dataIn.close();

        }catch (IOException ex) {
            System.err.println(ex.getMessage());
        }

Dinge wieder zu Lesen ist ziemlich seltsam floating point Werte an, und was ist daran falsch?BTW, lies wieder läuft ziemlich langsam, weiß auch nicht warum.
Oder jede andere gute Idee für die schnelle read/write float-array?

Dank!

//Vielen Dank an Larry, ich habe versucht bytebuffer.asfloatbuffer() Möglichkeiten, wie:

//WRITE
    float[] test = new float[3];
    test[0]=1.1f;
    test[1]=1.2f;
    test[2]=1.5f;
    try{
        RandomAccessFile aFile = new RandomAccessFile("/sdcard/demo.data", "rw");
        FileChannel outChannel = aFile.getChannel();

        //one float 3 bytes
        ByteBuffer buf = ByteBuffer.allocate(12);
        buf.clear();
        buf.asFloatBuffer().put(test);

        //while(buf.hasRemaining()) 
        {
            outChannel.write(buf);
        }

        outChannel.close();

    }
    catch (IOException ex) {
        System.err.println(ex.getMessage());
    }

    //READ
    float[] readback=new float[3];
    try{


        RandomAccessFile rFile = new RandomAccessFile("/sdcard/demo.data", "rw");
        FileChannel inChannel = rFile.getChannel();
        ByteBuffer buf_in = ByteBuffer.allocate(12);
        buf_in.clear();

        inChannel.read(buf_in);

        buf_in.asFloatBuffer().get(readback);

        inChannel.close();

    }
    catch (IOException ex) {
        System.err.println(ex.getMessage());
    }

und das Programm stürzt ab in buf_in.asFloatBuffer().get(readback);
jede Idee, und es waren Wege geht und debug-innen in java, Sorry, ganz neu in der java-Welt. Nochmals vielen Dank

  • Ich habe das gleiche problem wie du... "buf_in.asFloatBuffer().bekommen(readback);" abstürzt... Hast du das problem gelöst?
  • Das problem hier noch fehlt ist, dass die position der Puffer ist falsch. Sie müssen call-position(0) auf.
InformationsquelleAutor flankechen | 2014-03-07
Schreibe einen Kommentar