{"id":6591,"date":"2024-03-21T18:01:01","date_gmt":"2024-03-21T10:01:01","guid":{"rendered":""},"modified":"2024-03-21T18:01:01","modified_gmt":"2024-03-21T10:01:01","slug":"\u751f\u6210\u5706\u70b9\u4e8c\u7ef4\u7801\u5de5\u5177","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6591.html","title":{"rendered":"\u751f\u6210\u5706\u70b9\u4e8c\u7ef4\u7801\u5de5\u5177"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u5e8f\u8a00<\/h2>\n

\u6700\u8fd1\u9879\u76ee\u4e0a\u9700\u8981\u751f\u6210\u5706\u70b9\u4e8c\u7ef4\u7801\u56fe\u7247\u7684\u529f\u80fd
\u5c31\u5f00\u53d1\u4e86\u4e00\u4e2a\u5de5\u5177\u7528\u6765\u751f\u6210\u5706\u70b9\u4e8c\u7ef4\u7801\u3002
\u652f\u6301\u4e0d\u540c\u5927\u5c0f\uff0c\u4e0d\u540c\u989c\u8272\u3002<\/p>\n

\u6548\u679c<\/h2>\n

\u663e\u793a\u6548\u679c<\/h3>\n

\"\u751f\u6210\u5706\u70b9\u4e8c\u7ef4\u7801\u5de5\u5177<\/p>\n

\u8bc6\u522b\u6548\u679c<\/h3>\n

\"\u751f\u6210\u5706\u70b9\u4e8c\u7ef4\u7801\u5de5\u5177<\/p>\n

\u4ee3\u7801<\/h2>\n

\u4f9d\u8d56<\/h3>\n

\u53ea\u4f9d\u8d56zxing<\/p>\n

implementation 'com.<\/span>google.<\/span>zxing:<\/span>core:<\/span>3.4<\/span>.0<\/span>'\n<\/code><\/pre>\n

CircularQRCodeUtil<\/h3>\n
package<\/span> com.<\/span>example.<\/span>circular_qr_code<\/span>;<\/span>\n\nimport<\/span> android.<\/span>graphics.<\/span><\/span>Bitmap<\/span>;<\/span>\nimport<\/span> android.<\/span>graphics.<\/span><\/span>Canvas<\/span>;<\/span>\nimport<\/span> android.<\/span>graphics.<\/span><\/span>Color<\/span>;<\/span>\nimport<\/span> android.<\/span>graphics.<\/span><\/span>Paint<\/span>;<\/span>\n\nimport<\/span> com.<\/span>google.<\/span>zxing.<\/span><\/span>EncodeHintType<\/span>;<\/span>\nimport<\/span> com.<\/span>google.<\/span>zxing.<\/span><\/span>WriterException<\/span>;<\/span>\nimport<\/span> com.<\/span>google.<\/span>zxing.<\/span>qrcode.<\/span>decoder.<\/span><\/span>ErrorCorrectionLevel<\/span>;<\/span>\nimport<\/span> com.<\/span>google.<\/span>zxing.<\/span>qrcode.<\/span>encoder.<\/span><\/span>ByteMatrix<\/span>;<\/span>\nimport<\/span> com.<\/span>google.<\/span>zxing.<\/span>qrcode.<\/span>encoder.<\/span><\/span>Encoder<\/span>;<\/span>\nimport<\/span> com.<\/span>google.<\/span>zxing.<\/span>qrcode.<\/span>encoder.<\/span><\/span>QRCode<\/span>;<\/span>\n\nimport<\/span> java.<\/span>util.<\/span><\/span>HashMap<\/span>;<\/span>\nimport<\/span> java.<\/span>util.<\/span><\/span>Map<\/span>;<\/span>\n\n\/** * <pre> * Created by zhuguohui * Date: 2023\/5\/26 * Time: 14:07 * Desc:\u7528\u4e8e\u751f\u6210\u5706\u5f62\u7684\u4e8c\u7ef4\u7801\u7684\u5de5\u5177 * <\/pre> *\/<\/span>\npublic<\/span> class<\/span> CircularQRCodeUtil<\/span> { \n   <\/span>\n\n\n    \/** * \u751f\u6210\u5706\u5f62\u4e8c\u7ef4\u7801 * @param text \u9700\u8981\u7f16\u7801\u7684\u6587\u672c * @param width \u751f\u6210\u56fe\u7247\u7684\u5bbd\u5ea6 * @param height \u751f\u6210\u56fe\u7247\u7684\u9ad8\u5ea6 * @param color \u5706\u70b9\u7684\u989c\u8272 * @param quietZone \u8fb9\u8ddd * @return \u751f\u6210\u597d\u7684\u4e8c\u7ef4\u7801 * @throws WriterException *\/<\/span>\n    public<\/span> static<\/span> Bitmap<\/span> generateQRCodeImage<\/span>(<\/span>String<\/span> text,<\/span> int<\/span> width,<\/span> int<\/span> height,<\/span> int<\/span> color,<\/span>int<\/span> quietZone)<\/span> throws<\/span> WriterException<\/span> { \n   <\/span>\n        final<\/span> Map<\/span><<\/span>EncodeHintType<\/span>,<\/span> Object<\/span>><\/span><\/span> encodingHints =<\/span> new<\/span> HashMap<\/span><<\/span>><\/span><\/span>(<\/span>)<\/span>;<\/span>\n        encodingHints.<\/span>put<\/span>(<\/span>EncodeHintType<\/span>.<\/span>CHARACTER_SET,<\/span> \"UTF-8\"<\/span>)<\/span>;<\/span>\n        QRCode<\/span> code =<\/span> Encoder<\/span>.<\/span>encode<\/span>(<\/span>text,<\/span> ErrorCorrectionLevel.<\/span>M<\/span>,<\/span> encodingHints)<\/span>;<\/span>\n        return<\/span> renderQRImage<\/span>(<\/span>code,<\/span> width,<\/span> height,<\/span> color,<\/span>quietZone)<\/span>;<\/span>\n    }<\/span>\n\n\n\n    private<\/span> static<\/span> Bitmap<\/span> renderQRImage<\/span>(<\/span>QRCode<\/span> code,<\/span> int<\/span> width,<\/span> int<\/span> height,<\/span>int<\/span> color,<\/span> int<\/span> quietZone)<\/span> { \n   <\/span>\n        Bitmap<\/span> bitmap=<\/span>Bitmap<\/span>.<\/span>createBitmap<\/span>(<\/span>width,<\/span>height,<\/span> Bitmap.<\/span>Config<\/span>.<\/span>ARGB_8888)<\/span>;<\/span>\n        Canvas<\/span> canvas=<\/span>new<\/span> Canvas<\/span>(<\/span>bitmap)<\/span>;<\/span>\n        Paint<\/span> paint=<\/span>new<\/span> Paint<\/span>(<\/span>)<\/span>;<\/span>\n        paint.<\/span>setAntiAlias<\/span>(<\/span>true<\/span>)<\/span>;<\/span>\n        paint.<\/span>setColor<\/span>(<\/span>color)<\/span>;<\/span>\n        canvas.<\/span>drawColor<\/span>(<\/span>Color<\/span>.<\/span>WHITE)<\/span>;<\/span>\n\n        ByteMatrix<\/span> input =<\/span> code.<\/span>getMatrix<\/span>(<\/span>)<\/span>;<\/span>\n        if<\/span> (<\/span>input ==<\/span> null<\/span>)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> IllegalStateException<\/span>(<\/span>)<\/span>;<\/span>\n        }<\/span>\n        int<\/span> inputWidth =<\/span> input.<\/span>getWidth<\/span>(<\/span>)<\/span>;<\/span>\n        int<\/span> inputHeight =<\/span> input.<\/span>getHeight<\/span>(<\/span>)<\/span>;<\/span>\n        int<\/span> qrWidth =<\/span> inputWidth +<\/span> (<\/span>quietZone *<\/span> 2<\/span>)<\/span>;<\/span>\n        int<\/span> qrHeight =<\/span> inputHeight +<\/span> (<\/span>quietZone *<\/span> 2<\/span>)<\/span>;<\/span>\n        int<\/span> outputWidth =<\/span> Math<\/span>.<\/span>max<\/span>(<\/span>width,<\/span> qrWidth)<\/span>;<\/span>\n        int<\/span> outputHeight =<\/span> Math<\/span>.<\/span>max<\/span>(<\/span>height,<\/span> qrHeight)<\/span>;<\/span>\n\n        int<\/span> multiple =<\/span> Math<\/span>.<\/span>min<\/span>(<\/span>outputWidth \/<\/span> qrWidth,<\/span> outputHeight \/<\/span> qrHeight)<\/span>;<\/span>\n        int<\/span> leftPadding =<\/span> (<\/span>outputWidth -<\/span> (<\/span>inputWidth *<\/span> multiple)<\/span>)<\/span> \/<\/span> 2<\/span>;<\/span>\n        int<\/span> topPadding =<\/span> (<\/span>outputHeight -<\/span> (<\/span>inputHeight *<\/span> multiple)<\/span>)<\/span> \/<\/span> 2<\/span>;<\/span>\n        final<\/span> int<\/span> FINDER_PATTERN_SIZE =<\/span> 7<\/span>;<\/span>\n        final<\/span> float<\/span> CIRCLE_SCALE_DOWN_FACTOR =<\/span> 30f<\/span>\/<\/span>30f<\/span>;<\/span>\n        int<\/span> circleSize =<\/span> (<\/span>int<\/span>)<\/span> (<\/span>multiple *<\/span> CIRCLE_SCALE_DOWN_FACTOR)<\/span>;<\/span>\n        float<\/span> radius=<\/span>circleSize*<\/span>1.0f<\/span>\/<\/span>2<\/span>;<\/span>\n        int<\/span> cxStep=<\/span>multiple\/<\/span>2<\/span>;<\/span>\n\n        for<\/span> (<\/span>int<\/span> inputY =<\/span> 0<\/span>,<\/span> outputY =<\/span> topPadding;<\/span> inputY <<\/span> inputHeight;<\/span> inputY++<\/span>,<\/span> outputY +=<\/span> multiple)<\/span> { \n   <\/span>\n            for<\/span> (<\/span>int<\/span> inputX =<\/span> 0<\/span>,<\/span> outputX =<\/span> leftPadding;<\/span> inputX <<\/span> inputWidth;<\/span> inputX++<\/span>,<\/span> outputX +=<\/span> multiple)<\/span> { \n   <\/span>\n                if<\/span> (<\/span>input.<\/span>get<\/span>(<\/span>inputX,<\/span> inputY)<\/span> ==<\/span> 1<\/span>)<\/span> { \n   <\/span>\n                    if<\/span> (<\/span>!<\/span>(<\/span>inputX <=<\/span> FINDER_PATTERN_SIZE &&<\/span> inputY <=<\/span> FINDER_PATTERN_SIZE ||<\/span>\n                            inputX >=<\/span> inputWidth -<\/span> FINDER_PATTERN_SIZE &&<\/span> inputY <=<\/span> FINDER_PATTERN_SIZE ||<\/span>\n                            inputX <=<\/span> FINDER_PATTERN_SIZE &&<\/span> inputY >=<\/span> inputHeight -<\/span> FINDER_PATTERN_SIZE)<\/span>)<\/span> { \n   <\/span>\n                        int<\/span> cx=<\/span>outputX+<\/span>cxStep;<\/span>\n                        int<\/span> cy=<\/span>outputY+<\/span>cxStep;<\/span>\n                        canvas.<\/span>drawCircle<\/span>(<\/span>cx,<\/span>cy,<\/span> radius,<\/span>paint)<\/span>;<\/span>\n                    }<\/span>\n                }<\/span>\n            }<\/span>\n        }<\/span>\n\n        int<\/span> circleDiameter =<\/span> multiple *<\/span> FINDER_PATTERN_SIZE;<\/span>\n        circleDiameter\/=<\/span>2<\/span>;<\/span>\n        drawFinderPatternCircleStyle<\/span>(<\/span>canvas,<\/span>paint,<\/span>color,<\/span> leftPadding,<\/span> topPadding,<\/span> circleDiameter)<\/span>;<\/span>\n        drawFinderPatternCircleStyle<\/span>(<\/span>canvas,<\/span>paint,<\/span> color,<\/span>leftPadding +<\/span> (<\/span>inputWidth -<\/span> FINDER_PATTERN_SIZE)<\/span> *<\/span> multiple,<\/span> topPadding,<\/span> circleDiameter)<\/span>;<\/span>\n        drawFinderPatternCircleStyle<\/span>(<\/span>canvas,<\/span>paint,<\/span> color,<\/span>leftPadding,<\/span> topPadding +<\/span> (<\/span>inputHeight -<\/span> FINDER_PATTERN_SIZE)<\/span> *<\/span> multiple,<\/span> circleDiameter)<\/span>;<\/span>\n\n        return<\/span> bitmap;<\/span>\n    }<\/span>\n\n    private<\/span> static<\/span> void<\/span> drawFinderPatternCircleStyle<\/span>(<\/span>Canvas<\/span> canvas,<\/span>Paint<\/span> paint,<\/span>int<\/span> color,<\/span> int<\/span> x,<\/span> int<\/span> y,<\/span> int<\/span> circleDiameter)<\/span> { \n   <\/span>\n        final<\/span> int<\/span> WHITE_CIRCLE_DIAMETER =<\/span> circleDiameter*<\/span>5<\/span>\/<\/span>7<\/span>;<\/span>\n        final<\/span> int<\/span> MIDDLE_DOT_DIAMETER =<\/span> circleDiameter*<\/span>3<\/span>\/<\/span>7<\/span>;<\/span>\n        x+=<\/span>circleDiameter;<\/span>\n        y+=<\/span>circleDiameter;<\/span>\n        paint.<\/span>setColor<\/span>(<\/span>color)<\/span>;<\/span>\n        canvas.<\/span>drawCircle<\/span>(<\/span>x,<\/span> y,<\/span> circleDiameter,<\/span>paint)<\/span>;<\/span>\n        paint.<\/span>setColor<\/span>(<\/span>Color<\/span>.<\/span>WHITE)<\/span>;<\/span>\n        canvas.<\/span>drawCircle<\/span>(<\/span>x ,<\/span> y ,<\/span> WHITE_CIRCLE_DIAMETER,<\/span>paint)<\/span>;<\/span>\n        paint.<\/span>setColor<\/span>(<\/span>color)<\/span>;<\/span>\n        canvas.<\/span>drawCircle<\/span>(<\/span>x ,<\/span> y ,<\/span> MIDDLE_DOT_DIAMETER,<\/span>paint)<\/span>;<\/span>\n    }<\/span>\n}<\/span>\n\n<\/code><\/pre>\n

\u7528\u4f8b<\/h3>\n
   ImageView<\/span> ivCode2=<\/span>findViewById<\/span>(<\/span>R<\/span>.<\/span>id.<\/span>iv_code_2)<\/span>;<\/span>\n        try<\/span> { \n   <\/span>\n            Bitmap<\/span> bitmap =<\/span> CircularQRCodeUtil<\/span>.<\/span>generateQRCodeImage<\/span>(<\/span>\"http:\/\/www.zhihu.com\"<\/span>,<\/span> 300<\/span>,<\/span> 300<\/span>,<\/span> Color<\/span>.<\/span>parseColor<\/span>(<\/span>\"#FF2A6F\"<\/span>)<\/span>,<\/span> 0<\/span>)<\/span>;<\/span>\n            ivCode2.<\/span>setImageBitmap<\/span>(<\/span>bitmap)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>Exception<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> RuntimeException<\/span>(<\/span>e)<\/span>;<\/span>\n        }<\/span>\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u751f\u6210\u5706\u70b9\u4e8c\u7ef4\u7801\u5de5\u5177\u6700\u8fd1\u9879\u76ee\u4e0a\u9700\u8981\u751f\u6210\u5706\u70b9\u4e8c\u7ef4\u7801\u56fe\u7247\u7684\u529f\u80fd\u5c31\u5f00\u53d1\u4e86\u4e00\u4e2a\u5de5\u5177\u7528\u6765\u751f\u6210\u5706\u70b9\u4e8c\u7ef4\u7801","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6591"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6591"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6591\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}