java - float array to byte array for displaying bitmap on canvas in android -
i have float array in java , want convert each element byte array counterpart display using android.graphics.bitmap , bitmapfactory, operation doesn't seem work words of wisdom 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 activity 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]; (int = 0; < 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 converting single float 4bit array representation, firstly convert raw int byte[]. not sure if best way. in end hope construct float matrix of calculated log spectral amplitudes of audio track , map matrix bitmap. however, stuck on converting single array bitmap. new android , java. if there method people might suggest perform task, great to.thanks.
bitmapfactory.decodebytearray(byte_array,..) expects byte_array "byte array of compressed image data". doubt arbitrary bytes make compressed image. maybe should call canvas.drawpoint() repeatedly?
for(int k = 0;k < array.length;k++) { .. byte_array = inttobytearray(int_array[1]); .. }
here set byte_array 4 bytes make second int(int_array[1]). , 16 times.
converting float array byte array can 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 image funky random colors, unless know see. doesn't make more sense, generate colors white black?
Comments
Post a Comment