MVC generate two-dimensional code and bar code

Copy the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZXing;
using ZXing.Common;//一维码
using ZXing.QrCode;//二维码
using System.Drawing.Imaging;//图片保存
using System.Drawing;

namespace WebApplication8.Controllers
{
    public class DefaultController : Controller
    {
        // GET: Default
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 生成条形码
        /// </summary>
        /// <param name="Name"></param>
        /// <Returns> </ Returns> 
        public String Code (the Name String) 
        { 
            // Set bar code specifications 
            EncodingOptions ENCOD new new EncodingOptions = (); 
            // set high 
            encod.Height = 120; 
            // set the width 
            encod.Width = 200 ; 
            // generates barcode images and save 
            BarcodeWriter WR = new new BarcodeWriter (); 
            // specify the size 
            wr.Options = ENCOD; 
            // designated bar code specifications for EAN_13 
            wr.Format = BarcodeFormat.EAN_13; 
            // generate images 
            Bitmap img = wr .write (the Name); 
            // get the absolute path to the folder 
            string filepath = Server.MapPath ( "/ image /"); 
            // get the current time 
            string datime = DateTime.Now.ToString ( "yyMMddHHmmss" ); 
            // save the image for 
            img.Save (filepath datime + + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 
            // Returns the one-dimensional code image generated path 
            return "/ image /" datime + + ".jpg"; 
        } 
        /// <Summary> 
        /// generate two-dimensional code 
        /// </ Summary> 
        /// <param name = "the name"> </ param> 
        /// <Returns> </ Returns> 
        public String QRcode (the Name String) 
        { 
            // set two-dimensional code specifications 
            QrCodeEncodingOptions new new QrCodeEncodingOptions QR = (); 
            // set the encoding format, otherwise distortion 
            qr.CharacterSet = "UTF-8";
            qr.Height = 200; 
            qr.Width = 200 is;
            // set the two-dimensional code blank margin around the picture
            =. 1 qr.Margin; 
            // Generate bar code image stored 
            BarcodeWriter new new BarcodeWriter WR = (); 
            // two-dimensional code 
            wr.Format = BarcodeFormat.QR_CODE; 
            wr.Options = QR; 
            Bitmap 'bit = wr.Write (the Name); 

            String = Server.MapPath filepath ( "/ Image /"); 
            // get the current time 
            String datime = DateTime.Now.ToString ( "YYMMDDHHMMSS"); 
            // pictures were saved 
            bit.Save (filepath + datime + ".jpg ", System.Drawing.Imaging.ImageFormat.Jpeg); 
            // returns the one-dimensional code image generation path of 
            return "/ image /" + datime + ".jpg"; 
        } 
    } 
    }
Copy the code

3. The page code

Copy the code
<! DOCTYPE HTML> 
<HTML> 
<head> 
    <Meta name = "the viewport" Content = "width = Device-width" /> 
    <title> Index </ title> 
</ head> 
<body> 
    <div ID = "IMG1 "> 
        @ * realize the picture box * @ 

</ div> 
<the p-> 
    text <input type =" text "id =" text1 "name =" name1 "value =" "/> @ * text box * @ 
    <the INPUT type = "button" onclick = " qqq ()" name = "name11" value = " one-dimensional code generation" /> @ * one-dimensional code generation button * @ 
    <INPUT type = "button" the onclick = "WWW ()" name = "name12" value = "two-dimensional code generation" /> to generate two-dimensional code button @ * @ * 
</ P> 
<Script>
        QQQ function () { 
            . var NAME1 = $ ( "# text1") Val (); // get the value of the text box 
            $ .ajax ({// perform image Ajax submitted to a controller (with a controller of the following methods)
                url: '? / Default / yi name =' + name1, // parameter passing path 
                of the type: 'POST', 
                Success: function (Rese) {// receive a one-dimensional code picture return address 
                    $ ( "# imge1") .remove (); // empty box image 
                    $ ( "# img1") append ( "<img id = 'images' src =" + rese + "/>");. // the most added in a two-dimensional box image display code 
                } 
            }) 

        } 
        function WWW () { 
            var NAME2 = $ ( "# text1") Val ();. 
            $ .ajax ({ 
                URL: '? / the Default / ER name =' + NAME2, 
                type: 'POST ', 

                Success: function (Rese) { 
                    $ ( "# imge1") Remove ();.
                    $("#img1").append("<img id='images' src=" + rese + " />");
                }
            })
        }
</script>
</body>
</html>

Guess you like

Origin www.cnblogs.com/ryzryz/p/12160024.html