android - Android 不会像 #c01c2112 那样打印 ARGB 颜色
问题描述
不支持像ARGB 格式Android
那样打印颜色?#c01c2112
由于颜色无效,它显示错误。
我的这部分代码是 Store1
并0
进入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
然后这是将数据传递给array
.
String [] hexArrayRed = new String[arrayList.size()];
arrayList.toArray(hexArrayRed);
然后在我将数据转换为值类型时,我输入自己的#ff
和并与数据结合。它在这里工作正常。0000
hexadecimal
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
*/
}
那么问题来了。
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
然后我得到打印未知颜色的错误。
Process: kopilim.scs.prototyping, PID: 9890
java.lang.IllegalArgumentException: Unknown color
是不是像这样的 ARGB 颜色Android
不支持颜色?#f212cc12
解决方案
您从二进制转换为十进制到十六进制的代码工作正常,除了一小部分。
问题与您的这部分代码有关:
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.
推荐阅读
- python - 将数字转换为可变长度的二进制
- c++ - 为什么我在函数中创建的对象不会被其他函数修改?(C++)
- python - 检索解包需要8字节的缓冲区错误套接字python
- github - 有什么方法可以使用 Google Colab 在 GitHub 上发送 Pull Requests 和/或 Merge PRs?
- android - Flutter - 错误:此库已禁用空安全功能
- php - laravel 8 中的单元测试控制器
- rendering - 在虚幻引擎 4 中使用自定义组件渲染 obj 网格会呈现视觉伪影
- c# - 用户控件不更新文本框 mvvm WPF C#
- javascript - 预加载器屏幕淡出后播放 embed youtube
- python-3.x - Tensorflow 2.2.0-rc4 AttributeError:模块'tensorflow.compat.v1'在Spyder中没有属性'contrib'?