Preset an array - android

I set up an array[8] to store a string conversion. The X will range from 0 to 255. If X is less than 127 (7 bits) it does not write higher bit 0's. So I preset the array[8] to all 0's and the next routine would write only the changed data. Code compiles but the array[] all reads 1's regardless of what x= to.
int x = 10;
string=(Integer.toBinaryString(x));
int[] array = new int[8];
for (int j=0; j < 7; j++){
array[j]=0;
}
for (int i=0; i < string.length(); i++) {
array[i] = Integer.parseInt(string.substring(i,i+1));
}
Log.d("TAG", "Data " + array[0] + "" + array[1]+ "" + array[2] +
"" + array[3]+ "" + array[4]+ "" + array[5] +
"" + array[6] + "" + array[7]);

int x = 10;
String s=(Integer.toBinaryString(x));
int[] array = new int[8];
//no need for a loop that sets all values to 0.
int offset = array.length - s.length();
//you need this offset because the string may be shorter than the array
for (int i=0; i < s.length(); i++) {
array[i + offset] = Integer.parseInt(s.substring(i,i+1));
//applay the offset here
}
This will produce the follow array for int = 10:
[0, 0, 0, 0, 1, 0, 1, 0]

Related

Encoding ASCII in c++ for android application

I have some C++ code that I use in my android application. It takes a string and converts each character to an int from this ascii table. I then renders the correct glyph based on this.
It works on most of the devices I tested a few different google pixels and samsung phones, but when I test in a 3.3" WQVGA emulator on api 24 - 30 it doesn't function properly.
I imagine I'm going at this all wrong. Is there a better way of doing this? Is there a fix to my code?
Thanks for your time.
void text::drawtexthorizontal( double x, double y, int textheight, string textin)
{
int n = textin.length();
// declaring character array
char char_array[n + 1];
// copying the contents of the string to char array
strcpy(char_array, textin.c_str());
for (int i = 0; i < n; i++){
//get ascii or "extended" ascii index and store it in symbol
int symbol;
if( (char_array[i] & ( 1 << 7 )) >> 7 == 1){
if( (char_array[i] & ( 1 << 6 )) >> 6 == 1){
//comb takes 2 byte representation and returns an int between 128 and 255 corresponding to extended ascii
symbol = comb(int(char_array[i]),int(char_array[i+1]));
i++;
}
else{
symbol = comb(int(char_array[i+1]),int(char_array[i]));
i++;
}
}
else symbol = int(char_array[i]);
//render chars[symbol]
}
}
int text::comb( int n, int m){
/*https://naveenr.net/unicode-character-set-and-utf-8-utf-16-utf-32-encoding/ for 2 byte encoding*/
int a[8] = { 0 },b[8] = { 0 };;
int i,k=0;
for (i = 0; n > 0; i++) {
a[i] = n % 2;
n /= 2;
}
for (i = 0; m > 0; i++){
b[i] = m % 2;
m /= 2;
}
for (i = 4; i >=0 ; i--){
k = 10 * k + a[i];
}
for (i = 5; i >=0 ; i--){
k = 10 * k + b[i];
}
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int temp = k;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
if(dec_value > 255) dec_value = 32;
return dec_value;
}

Otsu histogram self implementation

I tried to make my own implementation of Otsu. I already read some source code from java and some sites that explains the formula and tried to implement it. I want to share this to ask if anyone can help me or at least tell about what can I do or improve.
I already coded get width, height and the background and foreground weight, mean, variance, and within class variance.
Note that I have not implemented how to set or find the exact threshold or even change the picture to black-white(binarize) using within class variance. If you can help me, feel welcome to. I also see some java codes that has treshhold = i or treshhold = t but I can't see how they made the image to black-white.
Here is my code:
Otsu.java
Bitmap tempImg = (Bitmap) original;
Bitmap OImg = Bitmap.createBitmap(tempImg.getWidth(), tempImg.getHeight(), tempImg.getConfig());
int width = tempImg.getWidth();
int height = tempImg.getHeight();
int A, R, G, B,colorPixel;
for (int x = 0; x < width; x++) { //original image to grayscale
for (int y = 0; y < height; y++) {
colorPixel = tempImg.getPixel(x, y);
A = Color.alpha(colorPixel);
R = Color.red(colorPixel);
G = Color.green(colorPixel);
B = Color.blue(colorPixel);
R = (R + G + B) / 3;
G = R;
B = R;
OImg.setPixel(x, y, Color.argb(A, R, G,B ));
}
}
return OImg;
}
public static Bitmap Botsu(Bitmap gImg){
Bitmap tempImg = (Bitmap) gImg;
Bitmap BWimg = Bitmap.createBitmap(tempImg.getWidth(), tempImg.getHeight(), tempImg.getConfig());
int width = tempImg.getWidth();
int height = tempImg.getHeight();
int A, R, G, B, colorPixel;
// histo-thresh
double Wcv = 0;
int[] Bx = new int[256];
int[] By = new int[256];
int[] Fx = new int[256];
int[] Fy = new int[256];
double Bw =0, Bm =0, Bv =0, Bp = 0;
double Fw =0, Fm =0, Fv =0, Fp = 0;
int c = 0, ImgPix = 0, ImgPixB = 0, ImgPixF = 0, newPixel = 0;
// pixel check for histogram
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
colorPixel = tempImg.getPixel(x, y);
A = Color.alpha(colorPixel);
R = Color.red(colorPixel);
G = Color.green(colorPixel);
B = Color.blue(colorPixel);
int gray = (int) (0.2989 * R + 0.5870 * G + 0.1140 * B);
if (gray > 128) { // white - foreground
for (int z=0; z<Fx.length; z++){
if (Fx[z] == gray){
c++;
}
}
if (c==1){
Fy[gray] = Fy[gray]+1; //y axis - counter for pixels for each x
}
else{
Fx[x] = gray; //x axis - 0-255
Fy[gray] = Fy[gray]+1;
}
}//By[Bx[x]]
else{ // black - background
for (int z=0; z<Bx.length; z++){
if (Bx[z] == gray){
c++;
}
}
if (c==1){
By[gray] = By[gray]+1; //y axis - counter for pixels for each x
}
else{
Bx[x] = gray; //x axis - 0-255
By[gray] = By[gray]+1;
}
}
}
}
for (int b=0; b<By.length; b++){
ImgPixB = ImgPixB + By[b];
}
for (int f=0; f<Fy.length; f++){
ImgPixF = ImgPixF + Fy[f];
}
ImgPix = ImgPixB + ImgPixF;
//bg part hist
for (int i=0; i<By.length; i++){ //weight
Bw = Bw + By[i];
}
Bw = Bw/ImgPix;
for (int i=0; i<By.length; i++){ //pixel sum
Bp = Bp + By[i];
}
for (int i = 0; i<Bx.length; i++){ //mean
Bm = Bm + (Bx[i]*By[Bx[i]]);
}
Bm = Bm/Bp;
for (int i=0; i<Bx.length; i++){ //variance
Bv = Bv + (Math.pow((Bx[i]-Bm),2)*By[Bx[i]]); // (Bx[i]-Bm) * (Bx[i]-Bm)
}
Bv = Bv/Bp;
//fg part hist
for (int i=0; i<Fy.length; i++){ //weight
Fw = Fw + Fy[i];
}
Fw = Fw/ImgPix;
for (int i=0; i<Fy.length; i++){ //pixel sum
Fp = Fp + Fy[i];
}
for (int i = 0; i<Fx.length; i++){ //mean
Fm = Fm + (Fx[i]*Fy[Fx[i]]);
}
Fm = Fm/Fp;
for (int i=0; i<Fx.length; i++){ //variance
Fv = Fv + (Math.pow((Fx[i]-Fm),2)*Fy[Fx[i]]); // (Bx[i]-Bm) * (Bx[i]-Bm)
}
Fv = Fv/Fp;
// within class variance
Wcv = (Bw * Bv) + (Fw * Fv);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
colorPixel = tempImg.getPixel(x, y);
A = Color.alpha(colorPixel);
R = Color.red(colorPixel);
G = Color.green(colorPixel);
B = Color.blue(colorPixel);
//int gray = (int) (0.2989 * R + 0.5870 * G + 0.1140 * B);
int gray2 = (int) (Wcv * R + Wcv * G + Wcv * B);
if (gray2 > 128) {
gray2 = 255;
}
else if (gray2 <129){
gray2 = 0;
}
BWimg.setPixel(x, y, Color.argb(A, gray2, gray2, gray2));
}
}
return BWimg;
x[z] is for x-axis andy[gray] is for y-axis. I based this on the graph on Lab Book
x = 0-255
y = how many pixels is on a certain color shade
feel free to send more samples that can help me.
OUTPUT: (I added 2 function with 3 output that has an output. Other value will only return few black dots or just white image.)
if (gray2 > 128) {
gray2 = 255;
}
else if (gray2 < 129){
gray2 = 0;
}
if (gray2 > 64 && gray2 < 129) {
gray2 = 255;
}
else if (gray2 < 65){
gray2 = 0;
}

How to get the RGB matrix from an image?

how to create matrix R,G,B from image in openCv .
matSrcBRG is Mat variable . and my image is img_about
try {
matSrcBGR = Utils.loadResource(context, R.raw.img_about);
} catch (IOException e) {
e.printStackTrace();
}
int rows = matSrcBGR.rows(); //Calculates number of rows
int cols = matSrcBGR.cols(); //Calculates number of columns
int ch = matSrcBGR.channels(); //Calculates number of channels (Grayscale: 1, RGB: 3, etc.)
double[] bgrColor = matSrcBGR.get(matSrcBGR.rows(), matSrcBGR.cols());
my answer is
int rows = matSrcBGR.rows(); //Calculates number of rows
int cols = matSrcBGR.cols(); //Calculates number of columns
int channels = matSrcBGR.channels(); //Calculates number of channels
//channel2=red , channel1=green ,channel0=blue
int[][][] matrix = new int[cols][rows][channels];
for (int col = 0; col < cols; col++) {
for (int row = 0; row < rows; row++) {
for (int channel = 0; channel < channels; channel++) {
matrix[col][row][channel] = (int) matSrcBGR.get(row, col)[channel];
}
}
}

An Allocation variable is set before it gains any data

After flipping throw the Grass Live Wallpaper, I have come around this bit of code - it creates the grass blade mesh and sets it indices:
private void createMesh() {
mVertexBuffer = new ScriptField_Vertex(mRS, mVerticies * 2);
final Mesh.AllocationBuilder meshBuilder = new Mesh.AllocationBuilder(mRS);
meshBuilder.addVertexAllocation(mVertexBuffer.getAllocation());
mBladesIndicies = Allocation.createSized(mRS, Element.U16(mRS), mIndicies);
meshBuilder.addIndexSetAllocation(mBladesIndicies, Primitive.TRIANGLE);
mBladesMesh = meshBuilder.create();
short[] idx = new short[mIndicies];
int idxIdx = 0;
int vtxIdx = 0;
for (int i = 0; i < mBladeSizes.length; i++) {
for (int ct = 0; ct < mBladeSizes[i]; ct ++) {
idx[idxIdx + 0] = (short)(vtxIdx + 0);
idx[idxIdx + 1] = (short)(vtxIdx + 1);
idx[idxIdx + 2] = (short)(vtxIdx + 2);
idx[idxIdx + 3] = (short)(vtxIdx + 1);
idx[idxIdx + 4] = (short)(vtxIdx + 3);
idx[idxIdx + 5] = (short)(vtxIdx + 2);
idxIdx += 6;
vtxIdx += 2;
}
vtxIdx += 2;
}
mBladesIndicies.copyFrom(idx);
}
I am puzzled by this 2 lines: meshBuilder.addIndexSetAllocation(mBladesIndicies, Primitive.TRIANGLE); and mBladesIndicies.copyFrom(idx);
Why do they send themBladesIndicies to the mesh builder and create the mesh, before the mBladesIndicies variable actually got he's data at mBladesIndicies.copyFrom(idx);
In short - why meshBuilder.addIndexSetAllocation(mBladesIndicies, Primitive.TRIANGLE); and mBladesMesh = meshBuilder.create(); come BEFORE mBladesIndicies.copyFrom(idx); ?
The Allocation is connected to the Mesh by a reference and not a copy. Because of this the order in which the data is added does not matter.
The Mesh.Builder does retain the allocation references after creation.

Fast Fourier transform of the recorded wav file in Android

There is a wav file on SD card. First, I connect to a file and reads the byte:
File file = null;file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/202.wav");
byte[] byteData = new byte[(int) file.length()];
FileInputStream in = null;
try {
in = new FileInputStream( file );
in.read( byteData );
in.close();}
I then use the transformation of data from a byte type in type double, because the FFT procedure does not work with byte type:
double[] transformed = new double[(int) file.length()];
for (int j=1;j<8194;j++) {
transformed[j] = (double)byteData[j]; }
And then the procedure of FFT processing this data:
public void spektr(double[] x, double[] y, int Dim, int D, double[] xx, double[] yy) {
int I,J,N,L,K,LE,LE1,IP,NV2,NM1;
double Arg,U1,U2,U3,C,S,T1,T2,T3,T4;
N = (int) Math.pow(2,Dim);
Log.v("N", "N "+N);
for (L = 1; L < Dim; L++){
LE = (int) Math.pow(2,Dim+1-L);
LE1 = LE/2;
U1 = (double)1.0;
U2 = (double)0.0;
Arg = (double)Math.PI/LE1;
C = (double)Math.cos(Arg);
S = D*(double)Math.sin(Arg);
for(J = 1; J < LE1; J++){
I = J;
do{
IP = I+LE1;
T1 = x[I] + x[IP];
T2 = y[I] + y[IP];
T3 = x[I] - x[IP];
T4 = y[I] - y[IP];
x[IP] = T3*U1 - T4*U2;
y[IP] = T4*U1 + T3*U2;
x[I] = T1;
y[I] = T2;
I = I + LE;
}while(I <= N);
U3 = U1*C - U2*S;
U2 = U2*C - U1*S;
U1 = U3;
}
}
NV2 = N / 2;
Log.v("NV2", "NV2 "+NV2);
NM1 = N-1;
Log.v("NM1", "NM1 "+NM1);
J = 1;
for(I = 1; I < NM1; I++){
if (I < J){
T1 = x[J];
T2 = y[J];
x[J] = x[I];
y[J] = y[I];
x[I] = T1;
y[I] = T2;
}
K = NV2;
while (K < J){
J = J - K;
K = K / 2;
};
J = J + K;
}
for(I = 1; I < N; I++){
x[I] = x[I] / N*2;
y[I] = y[I] / N*2;
}
for(I = 1; I < N; I++){
xx[I] = x[I];
yy[I] = y[I];
}
xx[1] = (double)0.0;
}
Reads the file successfully, but the processing procedure of the FFT is not working.
How to find the FFT procedure, or give the code to check the FFT result?
here is some free library to do FFT in java.

Categories

Resources