开发者

float array to byte array for displaying bitmap on canvas in android

I have a float array in java and want开发者_JS百科 to convert each element to its byte array counterpart to display using android.graphics.Bitmap and Bitmapfactory, however this operation doesn't seem to work any words of wisdom would be greatly appreciated.

package com.bitmapdisplay;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
import android.view.Window;


public class BitmapDiplay extends Activity {
/** Called when the activity is first created. */
    float[] array = {1, 6, 1, 4, 5, 0, 8, 7, 8, 6, 1,0, 5 ,6, 1,8};
    float[] new_array= new float[array.length];
    int[] int_array;
    byte[] byte_array;
    private static final int MASK = 0xff;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);


    for(int k = 0;k<array.length;k++)
    {
        new_array[k] = (float)((double)array[k]*2);
        int_array[k] = Float.floatToRawIntBits(new_array[k]);
        byte_array = intToByteArray(int_array[1]);
        setContentView(new Panel(this));
    }

}



public static byte[] intToByteArray(int param) {
    byte[] result = new byte[4];
    for (int i = 0; i < 4; i++) {
        int offset = (result.length - 1 - i) * 8;
        result[i] = (byte) ((param >>> offset) & MASK);
    }
    return result;
}




class Panel extends View {
    public Panel(Context byteArray) {
        super(byteArray);
    }
    @Override
    public void onDraw(Canvas canvas) {
        Bitmap _scratch = BitmapFactory.decodeByteArray(byte_array, 0, byte_array.length);
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(_scratch, 10, 10, null);
    }
}
}

I am converting from a single float to its 4bit array representation, however to do this I firstly convert it to raw int then to byte[]. I am not sure if this is the best way. However in the end I hope to construct a float matrix of calculated log spectral amplitudes of an audio track and map the matrix to a bitmap. However, I am stuck on just converting a single array to a bitmap. I am new to android and java. If there is another method that people might suggest to perform this task, that would be great to.Thanks.


BitmapFactory.decodeByteArray(byte_array,..) expects byte_array to be "byte array of compressed image data". I doubt that some arbitrary bytes make up a compressed image. Maybe you should call Canvas.drawPoint() repeatedly?

for(int k = 0;k < array.length;k++)
{
  ..
  byte_array = intToByteArray(int_array[1]);
  ..
}

Here you set byte_array to be the 4 bytes that make up the second int(int_array[1]). And do that 16 times.

Converting a float array to a byte array can be done easier: (did not test)

float[] array = {1, 6, 1, 4, 5, 0, 8, 7, 8, 6, 1,0, 5 ,6, 1,8};
ByteBuffer byteBuf = ByteBuffer.allocate(4 * array.length);
FloatBuffer floatBuf = byteBuf.asFloatBuffer();
floatBuf.put(array);
byte [] byte_array = byteBuf.array();

However, turning this into a image will look like funky random colors, unless you know exactly what you see. Doesn't it make more sense, to generate colors from white to black?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜