Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Zxing.net为二维码上色_.net_Model View Controller_Qr Code_Zxing.net - Fatal编程技术网

如何使用Zxing.net为二维码上色

如何使用Zxing.net为二维码上色,.net,model-view-controller,qr-code,zxing.net,.net,Model View Controller,Qr Code,Zxing.net,我已经使用ZXing.net生成了二维码,我想给生成的二维码加上颜色。如何在MVC.net中使用ZXing.net为二维码添加颜色 代码如下 IBarcodeWriter barcodeWriter = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = new QrCodeEncodingOptions

我已经使用ZXing.net生成了二维码,我想给生成的二维码加上颜色。如何在MVC.net中使用ZXing.net为二维码添加颜色

代码如下

IBarcodeWriter barcodeWriter = new BarcodeWriter
                {
                    Format = BarcodeFormat.QR_CODE,
                    Options = new QrCodeEncodingOptions
                    {
                        Width = 400,
                        Height = 400
                    }


                };

                var result = barcodeWriter.Write(qrcode);
                var barcodeBitmap = new Bitmap(result);
                #region code for text
                //RectangleF rectf = new RectangleF(0, 0, barcodeBitmap.Width, barcodeBitmap.Height);
                //Graphics g = Graphics.FromImage(barcodeBitmap);

                //g.SmoothingMode = SmoothingMode.AntiAlias;
                //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                //g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                //StringFormat format = new StringFormat()
                //{
                //    Alignment = StringAlignment.Center,
                //    LineAlignment = StringAlignment.Center
                //};
                //// Draw the text onto the image
                //g.DrawString("Vaishali", new Font("Tahoma", 8), Brushes.Red, rectf,format);
                #endregion

                #region code for logo
                System.Drawing.Image logo = System.Drawing.Image.FromFile(Server.MapPath("~/image") + "/logo.png");

                int left = (barcodeBitmap.Width / 2) - (logo.Width / 2);
                int top = (barcodeBitmap.Height / 2) - (logo.Height / 2);

                Graphics g = Graphics.FromImage(barcodeBitmap);

                g.DrawImage(logo, new Point(left, top));
                #endregion
                using (MemoryStream memory = new MemoryStream())
                {
                    using (FileStream fs = new FileStream(barcodePath, FileMode.Create, FileAccess.ReadWrite))
                    {

                        barcodeBitmap.Save(memory, ImageFormat.Jpeg);
                        byte[] bytes = memory.ToArray();
                        fs.Write(bytes, 0, bytes.Length);
                    }
                }

请有人告诉你如何给颜色。

好吧,如果你只想要单色(例如蓝色或任何
十六进制值),你可以用这种方法试试

私有位图生成(){
Map hintsMap=newhashmap();
hintsMap.put(EncodeHintType.CHARACTER_集,“utf-8”);
hintsMap.put(EncodeHintType.ERROR\u CORRECTION,ErrorCorrectionLevel.Q);
hintsMap.put(EncodeHintType.MARGIN,2);
int mWidth=100;
int mhweight=100;
试一试{
BitMatrix BitMatrix=new QRCodeWriter().encode(finalText,BarcodeFormat.QR_代码,mWidth,mHeight,hintsMap);
int[]像素=新的int[mWidth*mHeight];
对于(int i=0;i
我创建了一个GitHub Repo“ColorZXing.Net”,基本上你可以生成带有单调颜色的二维码,或者完全彩色的二维码

GitHub:

  private Bitmap generate() {
    Map<EncodeHintType, Object> hintsMap = new HashMap<>();
    hintsMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
    hintsMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
    hintsMap.put(EncodeHintType.MARGIN, 2);
    int mWidth = 100;
    int mHeight = 100;
    try {
        BitMatrix bitMatrix = new QRCodeWriter().encode(finalText, BarcodeFormat.QR_CODE, mWidth, mHeight, hintsMap);
        int[] pixels = new int[mWidth * mHeight];
        for (int i = 0; i < mHeight; i++) {
            for (int j = 0; j < mWidth; j++) {
                if (bitMatrix.get(j, i)) {// True if is is Black
                    pixels[i * mWidth + j] = 0xFFFFFFFF; //White
                } else {
                    pixels[i * mWidth + j] = 0x282946; //Insert the color here. 
                }
            }
        }
        Bitmap bitmap1 = Bitmap.createBitmap(pixels, mWidth, mHeight, Bitmap.Config.ARGB_8888);
        //SaveImage(bitmap1);
        return bitmap1;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}