Android dont print ARGB color like #c01c2112 - android

Does Android not supporting printing the color like #c01c2112 with the format ARGB? It display error because of invalid color.
This part of my code is Store 1 and 0 into the arraylist.
ArrayList<String>arrayList = new ArrayList<>();
for(int a = 0; a < bitmap1.getWidth(); a++){
for(int b = 0; b < bitmap1.getHeight(); b++){
String a1 = String.valueOf(arrayInput1[a][b]);
String a2 = String.valueOf(arrayInput2[a][b]);
String a3 = String.valueOf(arrayInput3[a][b]);
String a4 = String.valueOf(arrayInput4[a][b]);
String a5 = String.valueOf(arrayInput5[a][b]);
String a6 = String.valueOf(arrayInput6[a][b]);
String a7 = String.valueOf(arrayInput7[a][b]);
String a8 = String.valueOf(arrayInput8[a][b]);
arrayList.add(a1+a2+a3+a4+a5+a6+a7+a8);
// Store 1110001 into ArrayList
}
}//End of nested For
Then here is the part to pass the data to an array.
String [] hexArrayRed = new String[arrayList.size()];
arrayList.toArray(hexArrayRed);
Then I input myself the #ff and the 0000 and combine with the data as I convert the data to hexadecimal value type. It is working fine here.
for(int a = 0; a < hexArrayRed.length; a++){
int dec = Integer.parseInt(String.valueOf(arrayList.get(a)),2);
String hexString = Integer.toString(dec, 16);
String alpha = "#ff";
String behind = "0000";
hexArrayRed[a] = alpha+hexString+behind;
/*
Red Hexadecimal Value --> #ff _ _ 0000
*/
}
Then there is the problem.
QRCodeWriter qwRed = new QRCodeWriter();
try {
HashMap<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 2);
BitMatrix matrix = qwRed.encode(finalText,
BarcodeFormat.QR_CODE,
bitmap1.getWidth(),
bitmap1.getHeight(),
hints);
//START OF RED
final Bitmap newBitmapRed = Bitmap.createBitmap(
bitmap1.getWidth(),
bitmap1.getHeight(),
Bitmap.Config.ARGB_8888
);
int counter1 = 0;
for (int a = 0; a < bitmap1.getWidth(); a++) {
for (int b = 0; b < bitmap1.getHeight(); b++) {
//int c = 0;
int[] color = new int[hexArrayRed.length];
color[counter1] = Color.parseColor(hexArrayRed[counter1]); //Error is right here
int d = matrix.get(a,b)? color[counter1]: Color.WHITE;
newBitmapRed.setPixel(a,b,d);
counter1++;
}
}
//END OF RED
Then I get the error of printing the unknown color.
Process: kopilim.scs.prototyping, PID: 9890
java.lang.IllegalArgumentException: Unknown color
Is it the Android dont support color like #f212cc12 some sort like this the ARGB color?

Your code of converting from binary to decimal to hex works fine, except for one tiny part.
The problem is related to this part of your code:
String hexString = Integer.toString(dec, 16);
The problem with using Integer.toString() is that it'll give you the integer as a String, without the extra 0 padding.
What I mean by this is, for example: if your binary String was 00000111. Using Integer.parseInt("00000111", 2); would give you a decimal int of 7.
Finally, using String hexString = Integer.toString(7, 16); would give you a String of "7".
Therefore, when you plug that value into your hexArrayRed[a], instead of plugging it in as #AARRGGBB, you're plugging it in as #AARGGBB which is an improper format.
So to fix this, you simply have to check the length of hexString to see if it only has a size of 1. If it is, append an extra 0 to the front of it when you create your full hex string.

Related

Tensorflow Lite - Input shape must be 5 dimensional error

I am trying to port a tensorflow model to tensorflow lite to use it in an android application. The conversion is successful and everything runs except for Internal error: Failed to run on the given Interpreter: input must be 5-dimensional. The input in the original model was input_shape=(20, 320, 240, 1), which is 20 320 x 240 grayscale images (therefore ...,1). Here is the important code:
List<Mat> preprocessedFrames = preprocFrames(buf);
//has length of 20 -> no problem there (shouldn't affect dimensionality either...)
int[] output = new int[2];
float[][][] inputMatrices = new float[preprocessedFrames.toArray().length][320][240];
for(int i = 0; i < preprocessedFrames.toArray().length; i++) {
Mat inpRaw = preprocessedFrames.get(i);
Bitmap data = Bitmap.createBitmap(inpRaw.cols(), inpRaw.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(inpRaw, data);
int[][] pixels = pixelsFromBitmap(data);
float[][] inputMatrix = inputMatrixFromIntPixels(pixels);
// returns float[][] with floats from 0 to 1
inputMatrices[i] = inputMatrix;
}
try{
detector.run(inputMatrices, output);
Debug("results: " + output.toString());
}
The model gives me an output of 2 neurons translating into 2 labels.
The model code is the following:
model = tf.keras.Sequential(name='detector')
model.add(tf.keras.layers.Conv3D(filters=(56), input_shape=(20, 320, 240, 1), strides=(2,2,2), kernel_size=(3,11,11), padding='same', activation="relu"))
model.add(tf.keras.layers.AveragePooling3D(pool_size=(1,4,4)))
model.add(tf.keras.layers.Conv3D(filters=(72), kernel_size=(4,7,7), strides=(1,2,2), padding='same'))
model.add(tf.keras.layers.Conv3D(filters=(81), kernel_size=(2,4,4), strides=(2,2,2), padding='same'))
model.add(tf.keras.layers.Conv3D(filters=(100), kernel_size=(1,2,2), strides=(3,2,2), padding='same'))
model.add(tf.keras.layers.Conv3D(filters=(128), kernel_size=(1,2,2), padding='same'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(768, activation='tanh', kernel_regularizer=tf.keras.regularizers.l2(0.011)))
model.add(tf.keras.layers.Dropout(rate=0.1))
model.add(tf.keras.layers.Dense(256, activation='sigmoid', kernel_regularizer=tf.keras.regularizers.l2(0.012)))
model.add(tf.keras.layers.Dense(2, activation='softmax'))
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.00001), loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=['accuracy'])
EDIT: I printed out the first input tensor as follows:
int[] shape = detector.getInputTensor(0).shape();
for(int r = 0; r < shape.length; r++){
Log.d("********" + r, "*******: " + r + " : " + shape[r]);
}
With that I first get the output [1,20,320,240,1]and after that I only get [20,320,240]. I am really quite desperate now...
So, I figured it out by myself and it seems like I really only had to make the input 5 dimensional by putting the content into a first dimension and every single pixel into a fifth dimension. I don't know why, but I will accept that xD.
float[][] output = new float[1][2];
float[][][][][] inputMatrices = new float[1][preprocessedFrames.toArray().length][320][240][1];
for(int i = 0; i < preprocessedFrames.toArray().length; i++) {
Mat inpRaw = preprocessedFrames.get(i);
Bitmap data = Bitmap.createBitmap(inpRaw.cols(), inpRaw.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(inpRaw, data);
int[][] pixels = pixelsFromBitmap(data);
float[][] inputMatrix = inputMatrixFromIntPixels(pixels);
for (int j = 0; j < inputMatrix.length - 1; j++) {
for(int k = 0; k < inputMatrix[0].length - 1; k++) {
inputMatrices[0][i][k][j][0] = inputMatrix[j][k];
}
}
}

Generate QR Code with Colors in every pixel of image and binary value

Well, I am doing a project with the QR Code. The idea of my project is to combine the numbers of QR Code(8-numbers of QR Code for my project) and generate a single color QR Code which idea is to increase the data storage of the QR Code.
However, so far I have done most of the part but I left the most important part which is generating color QR Code. The color QR Code has to be generated with the hexadecimal color and set the color to every pixel of the QR Code so it would be like a color QR Code. For now, I would try to generate with the Red color 1st.
So I have store the binary value in an ArrayList and the data is some sort like 10101010. Then I change it to hexadecimal. Here is my code:
ArrayList<String>arrayList = new ArrayList<>();
arrayList.add(a1+a2+a3+a4+a5+a6+a7+a8); // Store 1110001 into ArrayList
String [] hexArray = new String[arrayList.size()];
arrayList.toArray(hexArray);
for(int a = 0; a < hexArray.length; a++){
int dec = Integer.parseInt(String.valueOf(arrayList.get(a)),2);
String hexString = Integer.toString(dec, 16);
String alpha = "0xff";
String behind = "0000";
hexArray[a] = alpha+hexString+behind;
}
I have written some code of changing the color, but the code is changing the whole color of the QR Code which is the foreground and the background of QR Code as well.
private Bitmap encode(String contents, int width, int height, #ColorInt int foreground, #ColorInt int background) {
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix matrix = null;
Bitmap bitmap = null;
try {
matrix = writer.encode(contents, BarcodeFormat.QR_CODE, width, height);
} catch (WriterException e) {
e.printStackTrace();
}
if(matrix != null) {
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = matrix.get(x, y) ? foreground : background;
}
}
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
}
return bitmap;
}
Since ARGB having Alpha, Red, Green, Blue colors. So what if I want to set the hexadecimal value just to Red color only. Then set to every pixel of the QR Code which will become a red color (would slightly different in kind of red color because the hexadecimal value is different) QR Code.
If you are using Color.parseColor(hexadecimal) to generate the color. The hexadecimal should be like #ff00ff RGB format or #ff00ff00 ARGB format.
for(int a = 0; a < hexArrayRed.length; a++){
int dec = Integer.parseInt(String.valueOf(arrayList.get(a)),2);
String hexString = Integer.toString(dec, 16);
while(hexString.length() < 2){
hexString = "0"+hexString;
}
String head = "#ff";
String behind = "0000";
hexArrayRed[a] = head+hexString+behind;
/*
Red Hexadecimal Value --> #ff _ _ 0000
*/
}
For your information, I add while statement to ensure the length of hexString. If the binary is 00000011 and it is 3 in hexadecimal instead of 03. So I add "0" to the hexString.

How to Display the text in Reverse from char to String

I tried to display the text but it is displaying in wrong view. I want to display the Textview in reverse. I tried this code
String subMenuId = (String) key[position];
String subMenuName = subMenuTable.get(subMenuId);
for (int x = 0; x < subMenuName.length(); x++) {
int splitword = subMenuName.charAt(x);
char c = (char) splitword;
TextView product = new TextView(con);
product.setText(String.valueOf(c));
product.setRotation(-90);
holder.tv.setGravity(Gravity.CENTER_VERTICAL);
product.setTextSize(12);
holder.tv.setPadding(5,0,5,0);
product.setTextColor(Color.BLACK);
product.setTypeface(null, Typeface.BOLD);
holder.tv.addView(product);
}
Try the following
String reversedString = new StringBuilder("LIKE").reverse().toString()
Just reverse your loop
i.e.
for (int x = 0; x < subMenuName.length(); x++)
to
for (int x = subMenuName.length() -1; x >=0 ; x--)
Hope it helps
When you iterate from 0 ⇢ n first char get referred first and will be added to top. since you want it in reverse, iterate n ⇢ 0

Android OpenCV color detection in HSV space

i tried to wrote color(green) detection code for android(live camera view) in OpenCV. first use RGB space and it's half okay but when switch to the HSV space the result is the mess !!!
this is my code
Mat A = src;
Mat B = dst;
Imgproc.cvtColor(A, A, Imgproc.COLOR_RGB2HSV,3);
Size sizeA = A.size();
for (int i = 0; i < sizeA.height; i++)
for (int j = 0; j < sizeA.width; j++) {
double[] data = A.get(i, j);
if (data[0]>=95 && data[0]<=130 & data[1]>=150 && data[1]<=255 & data[2]<=150 && data[2]<=255){
data[0] = 120;
data[1] = 255 ;
data[2] = 255 ;
}
else
data[0] = 100;
data[1] = 255;
data[2] = 255;
B.put(i, j, data);
}
Imgproc.cvtColor(B, B, Imgproc.COLOR_RGB2RGBA, 4);}
}
what's wrong with this code ? and this method run slowly.why?
(i new to android and OpenCV)
Tnx
You should probably convert from BGR (not RGB) to HSV, but that depends on your code before this snippet
Imgproc.cvtColor(A, A, Imgproc.COLOR_BGR2HSV,3);
Check your if statement and use always && (you sometimes use &)
You should convert from HSV to RGB and then to RGBA
Imgproc.cvtColor(B, B, Imgproc.COLOR_HSV2RGB, 3);}
Imgproc.cvtColor(B, B, Imgproc.COLOR_RGB2RGBA, 4);}

LineIterator in OpenCV C++ in Android

I write a program which obtain value of color Asymmetric at lesion.
my program in visual studio Working correctly and I get color Asymmetric. But program in android (use Native) always return 0.
The problem is that points[j] always has same value therefor value of avg_i[i] with avg is equal.
my code:
Mat& srcMat = *(Mat*)src;
Mat& mask_tr = *(Mat*)Mask;
Mat gray;
cvtColor(srcMat, gray, CV_BGR2GRAY);
double summation=0, avg=0, NCD=0, mismatch=0;
double* avg_i = new double[p.size()];
double* ncd = new double[p.size()];
double* XOR_Cs_Cl = new double[p.size()];
int A_x=0, A_t=0;
vector<Vec3b> buf;
for(int i=0; i<p.size(); i++){
LineIterator it(gray, p5, p[i], 8);
vector<Point> points(it.count);
for(int j=0; j<it.count-1; j++)
{
buf.push_back( Vec3b(*it) );
points[j] = it.pos();
it=it++;
summation += int(gray.at<uchar>(points[j].y , points[j].x));
}
avg_i[i] = summation / (it.count-1);
summation = 0;
avg += avg_i[i];
}
avg = avg/p.size();
for(int i=0; i<p.size(); i++){
ncd[i] = avg_i[i] * (100/avg);
A_t += ncd[i];
XOR_Cs_Cl[i] = abs(100 - ncd[i]);
A_x += XOR_Cs_Cl[i];
}
mismatch = (double(A_x) / double(A_t))*100;
delete [] avg_i;
delete [] ncd;
return mismatch;

Categories

Resources