Java generates a barcode

Source: Internet
Author: User
Tags check character

When a project involves product inventory, bar code is generally considered for ease of management and operation. I used it before when I was working on a project. I sorted it out a little and shared it with a friend who needed it for reference.

Implementation function: display various types of bar codes (18 types) in Java, and scan devices after printing.

To download the latest Java class, go to the last attachment (barcode.zip.

1. Introduce the four Java classes of barcode into your project;

2. configure a servlet for generating a barcode in Web. XML as follows:

HTML code
  1. ...
  2. <
    Servlet
    >

  3. <
    Servlet-name
    >
    CreateBarCode
    </
    Servlet-name
    >

  4. <
    Servlet-class
    >
    Com. soft4j. barcode. BarCodeServlet
    </
    Servlet-class
    >

  5. </
    Servlet
    >

  6. ...
  7. <
    Servlet-mapping
    >

  8. <
    Servlet-name
    >
    CreateBarCode
    </
    Servlet-name
    >

  9. <
    Url-pattern
    >
    /CreateBarCode
    </
    Url-pattern
    >

  10. </
    Servlet-Mapping
    >

  11. ...
...<servlet><servlet-name>CreateBarCode</servlet-name><servlet-class>com.soft4j.barcode.BarCodeServlet</servlet-class></servlet>...<servlet-mapping><servlet-name>CreateBarCode</servlet-name><url-pattern>/CreateBarCode</url-pattern></servlet-mapping>...


In this way, you can use the createbarcode servlet to generate a barcode.

Note:

(1) COM. soft4j. barcode. barcodeservlet. Enter the actual package path in your project.

(2) You can set the createbarcode name on your own.



3. Example of foreground reference (barcodeexample. jsp ):

HTML code
  1. <
    % @ Page
    Contenttype
    =
    "Text/html; charset = UTF-8"
    %
    >
    <
    %
  2. String code
    =
    "135792468"
    ; // Bar code content
  3. %>

  4. <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">

  5. <
    Html
    >
    <
    Head
    >
    <
    Title
    >
    </
    Title
    >

  6. <
    Meta
     
    HTTP-equiv
    =
    "Content-Type"
     
    Content
    =
    "Text/html; charset = UTF-8"
    >

  7. <
    Script
     
    Language
    =
    "JavaScript"
    >

  8. <! --
  9. Ns4
    = (Document. layers )? 1: 0;
  10. Visble_property_prefix
    = (Ns4 )? "Document. layers .":"";
  11. Visble_property_suffix
    = (NS4 )? ". Visibility": ". style. display ";
  12. Visble_property_true
    = (NS4 )? "Show": "block ";
  13. Visble_property_false
    = (NS4 )? "Hide": "none ";
  14. Visble_property_printview
    =
    Visble_property_prefix
    + "Viewpanel" + visble_property_suffix;
  15. Function nowprint (){
  16. Window. print ();
  17. }
  18. Function window. onbeforeprint (){
  19. Eval (visble_property_printview + "=/" "+ visble_property_false + "/"");
  20. }
  21. Function window. onafterprint (){
  22. Eval (visble_property_printview + "=/" "+ visble_property_true + "/"");
  23. }
  24. // -->

  25. </
    Script
    >

  26. </
    Head
    >

  27. <
    Body
     
    Topmargin
    =
    "0px"
     
    Leftmargin
    =
    "0px"
     
    Rightmargin
    =
    "0px"
     
    Bottommargin
    =
    "0px"
    >

  28. <
    %
  29. StringBuffer barCode
    =
    New
    StringBuffer ();
  30. BarCode. append ("<
    Img
     
    Src
    = '");
  31. BarCode. append (request. getContextPath ());
  32. BarCode. append ("/CreateBarCode? Code
    = ");
  33. BarCode. append (code );
  34. BarCode. append ("& barType
    =
    CODE39
    &
    CheckCharacter
    =
    N
    &
    CheckCharacterInText
    = N'
    >
    ");
  35. Out. println (barCode. toString ());
  36. %>

  37. <
    Div
     
    Id
    =
    "Viewpanel"
     
    Align
    =
    "Center"
    >

  38. <
    Input
     
    Name
    =
    "Bequery"
     
    Type
    =
    "Button"
     
    Value
    =
    "Print"
     
    Style
    =
    "Cursor: hand ;"
     
    Onclick
    =
    "Nowprint ();"
    >

  39. </
    Div
    >

  40. <! -- SCRIPT> nowprint (); </script -->

  41. </
    Body
    >

  42. </
    Html
    >

<% @ Page contentType = "text/html; charset = UTF-8" %> <% String code = "135792468"; // barcode content %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

4. Some parameters that can be set during bar code use:

------------------------------------

Bartype (code128 by default): the bar code type. Supports the 18-minute type, which is
Code39, code39ext, interleaved25, code11, Codabar, msi, upca, ind25, mat25, code93, ean13, ean8, upce, code128, code93ext, PostNet, planet, and ucc128
.

Code: the content of the barcode to be printed.

Width (the default value is adaptive. Generally, you do not need to set the width. width and height of the image.

Height (the default value is adaptive. Generally, you do not need to set the height. width and height of the image.

ST (Y by default): whether to display the bar code (show text). By default, the bar code content is displayed below the bar code image. Valid values: Y and N.

Textfont (Arial | plain | 11 by default): Specifies the font of the bar code text. The valid font format is <font name >|< style >|< size>. style can be plain, italic, or bold.

Fontcolor (black by default): Color of the bar code text. Valid values: red, blue, green, black, gray, lightgray, white, darkgray, yellow, orange, cyan, and magenta.

BarColor (black by default): The color of the bar code.

BackColor (white by default): Specifies the background color of the image.

Rotate (0 by default): sets the bar code rotation angle. valid values: 0 (not rotating), 90 (Rotating 90 degrees), 180 (rotating 180 degrees), 270 (rotating 270 degrees ).

BarHeightCM (1 cm by default): the bar code height.

X (the default value is 0.03 cm. Generally, you do not need to adjust it yourself): the nominal size of the narrow unit in the bar code symbol. The minimum value can be set to 0.001, that is, 1 pixel, which is usually increased by 0.03.

N (2 times by default): The sum of the width and width of the average width and the mean width and the null width (excluding the bar code character interval) divided by two times the narrow unit size. it is a technical parameter in the Width Adjustment encoding method.

LeftMarginCM (0.3 cm by default): the distance between the bar code and the left and right sides of the image.

TopMarginCM (default value: 0.2 cm): the distance between the bar code and the upper and lower sides of the image.

CheckCharacter (default value: y, which is generally not set by yourself): whether to automatically calculate check character. Valid values: y and n.

CheckCharacterInText (the default value is y, which is usually not set by yourself): check whether the bar code content is automatically calculated. The valid values are y and n.

Code128Set (the default value is 0 and is automatically selected. You do not need to set it): Set the character set used in CODE128. Valid values: 0, A, B, C.

UPCESytem (the default value is 0. You do not need to set it yourself): The Encoding System Used in UPCE. Valid values are 0 and 1.

------------------------------------

The key parameters are barType and code.
In addition, set the checkCharacter and checkCharacterInText parameters to n. If it is y, an additional
Check Code, although it does not affect the use (scanning the device to scan the barcode to obtain data will not show the check code), but it looks awkward (such ).

5. It can also be used in the applet. For specific usage parameters, see the BarCodeApplet. java file, which is similar to WEB usage.

6. Attachment barcode.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.