首页 > 解决方案 > Android 不会像 #c01c2112 那样打印 ARGB 颜色

问题描述

不支持像ARGB 格式Android那样打印颜色?#c01c2112由于颜色无效,它显示错误。

我的这部分代码是 Store10进入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和并与数据结合。它在这里工作正常。0000hexadecimal

 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

标签: androidcolors

解决方案


您从二进制转换为十进制到十六进制的代码工作正常,除了一小部分。

问题与您的这部分代码有关:

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.


推荐阅读