最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to add a title and additional information to barcodes generated with R? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to create PDF barcodes with titles and additional information using R like shown below.

Here's my current code:

library(baRcodeR)

Labels = c("123456789012", "987654321098", "112233445566", "556677889900")  # Example barcode numbers
titles = c("Product 1\n", "Product 2", "Product 3", "Product")

custom_create_PDF(
 Labels = Labels,         
 name = "barcodes_with_titles", # Output file name
 type = "linear",         
 Fsz = 12,           
 numcol = 2,
 numrow = 3,
 trunc = FALSE,
 denote = '.'
)

This code generates basic barcodes with labels:

I need to achieve this specific layout:

Expected output:

  • Each barcode should have a title above it
  • Additional text-information should appear below the barcode, including the Label used for generating the Bar Code (see image above)

What I've tried:

  • Using the alt_text parameter to add titles, which resulted in an Error In custom_create_PDF(Labels = Labels, name = "barcodes_with_titles", : alt_text is not encoded in barcode. Non-encoded text begins with.
  • I tried to adapt SOpost, but that just describes how to plot "bla" and "fu" as a bar plot, which is not what I want.

Questions:

How can I modify my existing code to add titles above each barcode? What parameters or functions should I use to add additional information below the barcode?

Environment:

R version: 4.4.2

Package used: baRcodeR

I'm trying to create PDF barcodes with titles and additional information using R like shown below.

Here's my current code:

library(baRcodeR)

Labels = c("123456789012", "987654321098", "112233445566", "556677889900")  # Example barcode numbers
titles = c("Product 1\n", "Product 2", "Product 3", "Product")

custom_create_PDF(
 Labels = Labels,         
 name = "barcodes_with_titles", # Output file name
 type = "linear",         
 Fsz = 12,           
 numcol = 2,
 numrow = 3,
 trunc = FALSE,
 denote = '.'
)

This code generates basic barcodes with labels:

I need to achieve this specific layout:

Expected output:

  • Each barcode should have a title above it
  • Additional text-information should appear below the barcode, including the Label used for generating the Bar Code (see image above)

What I've tried:

  • Using the alt_text parameter to add titles, which resulted in an Error In custom_create_PDF(Labels = Labels, name = "barcodes_with_titles", : alt_text is not encoded in barcode. Non-encoded text begins with.
  • I tried to adapt SOpost, but that just describes how to plot "bla" and "fu" as a bar plot, which is not what I want.

Questions:

How can I modify my existing code to add titles above each barcode? What parameters or functions should I use to add additional information below the barcode?

Environment:

R version: 4.4.2

Package used: baRcodeR

Share Improve this question edited Jan 19 at 7:23 Tim G 4,8131 gold badge2 silver badges16 bronze badges asked Jan 18 at 15:09 Keivan.kKeivan.k 6387 silver badges22 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 3

You can adapt a subfunction from the baRcodeR package to fit your requirement. The function barcode_bars_extended will print a bar code for every label in Labels. It takes above_text and below_text as extra args. You can adapt the function to fit your needs.

Output

Code

setwd(dirname(rstudioapi::getSourceEditorContext()$path)) # set the current script's location as working directory
if (!require("pacman")) install.packages("pacman")
pacman::p_load(grid, gridExtra, Cairo)

barcode_bars_extended <- function(Labels,  # name of your labels, these are barencoded
                                  above_text = "Barcode",  # text above barcode
                                  below_text = NULL,  # text below barcode
                                  output_file = "barcodes.pdf",  # name of outputted pdf file
                                  barsHeight = 0.08 # height of barcode per page, 1 is the whole page height
                                  ) 
  {
  ## Helper function to create barcode grobs with labels
  generate_barcode_grob <- function(label, above_text, below_text) {
    label <- as.character(label)
    label <- iconv(label, from = "utf-8", to = "ascii", sub = "-")
    
    start_code <- 209
    lab_chars <- unlist(strsplit(label, split = ""))
    lab_values <- sapply(lab_chars, function(x) utf8ToInt(x))
    code_values <- lab_values - 32
    check_sum <- 104 + sum(code_values * seq(1, length(code_values)))
    check_character <- check_sum %% 103
    
    Barcodes <- Barcodes <- structure(list(code = c("32", "33", "34", "35", "36", "37", "38", 
                                                    "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", 
                                                    "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", 
                                                    "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", 
                                                    "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", 
                                                    "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", 
                                                    "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", 
                                                    "104", "105", "106", "107", "108", "109", "110", "111", "112", 
                                                    "113", "114", "115", "116", "117", "118", "119", "120", "121", 
                                                    "122", "123", "124", "125", "126", "209"), barcode = c("11011001100", 
                                                                                                           "11001101100", "11001100110", "10010011000", "10010001100", "10001001100", 
                                                                                                           "10011001000", "10011000100", "10001100100", "11001001000", "11001000100", 
                                                                                                           "11000100100", "10110011100", "10011011100", "10011001110", "10111001100", 
                                                                                                           "10011101100", "10011100110", "11001110010", "11001011100", "11001001110", 
                                                                                                           "11011100100", "11001110100", "11101101110", "11101001100", "11100101100", 
                                                                                                           "11100100110", "11101100100", "11100110100", "11100110010", "11011011000", 
                                                                                                           "11011000110", "11000110110", "10100011000", "10001011000", "10001000110", 
                                                                                                           "10110001000", "10001101000", "10001100010", "11010001000", "11000101000", 
                                                                                                           "11000100010", "10110111000", "10110001110", "10001101110", "10111011000", 
                                                                                                           "10111000110", "10001110110", "11101110110", "11010001110", "11000101110", 
                                                                                                           "11011101000", "11011100010", "11011101110", "11101011000", "11101000110", 
                                                                                                           "11100010110", "11101101000", "11101100010", "11100011010", "11101111010", 
                                                                                                           "11001000010", "11110001010", "10100110000", "10100001100", "10010110000", 
                                                                                                           "10010000110", "10000101100", "10000100110", "10110010000", "10110000100", 
                                                                                                           "10011010000", "10011000010", "10000110100", "10000110010", "11000010010", 
                                                                                                           "11001010000", "11110111010", "11000010100", "10001111010", "10100111100", 
                                                                                                           "10010111100", "10010011110", "10111100100", "10011110100", "10011110010", 
                                                                                                           "11110100100", "11110010100", "11110010010", "11011011110", "11011110110", 
                                                                                                           "11110110110", "10101111000", "10100011110", "10001011110", "11010010000"
                                                    )), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                -96L))
    
    Binary_code <- sapply(lab_values, function(x, Barcodes) Barcodes$barcode[x == Barcodes$code], Barcodes = Barcodes)
    quiet_zone <- paste(c(1:(10)*0), collapse = "")
    binary_label <- paste(quiet_zone, 
                          Barcodes$barcode[Barcodes$code == start_code],
                          paste(Binary_code, collapse = ""),
                          Barcodes$barcode[check_character + 1],
                          "1100011101011",
                          quiet_zone,
                          collapse = "", sep = "")
    bar_values <- as.numeric(unlist(strsplit(binary_label, split = "")))
    
    barcode_grob <- grid::rasterGrob(t(!as.matrix(bar_values)), width = 0.5, height = barsHeight, interpolate = FALSE)
    
    ## Text Grobs
    text_grobs <- list()
    
    # Create temporary text grobs to calculate heights
    temp_above <- textGrob(above_text, gp = gpar(fontsize = 14, fontfamily = "Arial Unicode MS"))
    temp_label <- textGrob(label, gp = gpar(fontsize = 15, fontface = "bold", fontfamily = "Arial Unicode MS"))
    temp_below <- if (!is.null(below_text)) {
      textGrob(below_text, gp = gpar(fontsize = 14, fontfamily = "Arial Unicode MS"))
    } else NULL
    
    # Calculate heights
    above_height <- convertUnit(grobHeight(temp_above), "npc", valueOnly = TRUE)
    label_height <- convertUnit(grobHeight(temp_label), "npc", valueOnly = TRUE)
    below_height <- if (!is.null(below_text)) {
      convertUnit(grobHeight(temp_below), "npc", valueOnly = TRUE)
    } else 0
    
    # Calculate padding (adjust this value to control spacing between elements)
    padding <- 0.02
    
    # Calculate positions
    barcode_center <- 0.5
    above_y <- barcode_center + barsHeight/2 + padding + above_height/2
    label_y <- barcode_center - barsHeight/2 - padding - label_height/2
    below_y <- if (!is.null(below_text)) {
      label_y - label_height/2 - padding - below_height/2
    } else NULL
    
    # Create final text grobs with calculated positions
    text_grobs$above <- textGrob(above_text, 
                                 gp = gpar(fontsize = 14, fontfamily = "Arial Unicode MS"), 
                                 y = above_y)
    text_grobs$barcode <- barcode_grob
    text_grobs$label <- textGrob(label, 
                                 gp = gpar(fontsize = 15, fontface = "bold", fontfamily = "Arial Unicode MS"),
                                 y = label_y)
    if (!is.null(below_text)) {
      text_grobs$below <- textGrob(below_text, 
                                   gp = gpar(fontsize = 14, fontfamily = "Arial Unicode MS"),
                                   y = below_y)
    }
    
    ## Draw each text and barcode grob with its respective viewport
    grob_list <- gTree(children = do.call("gList", text_grobs))
    grid.draw(grob_list)
  }
  
  ## Output to PDF 8.27 × 11.69
  CairoPDF(output_file, width = 8.27, height = 11.69)
  for (i in seq_along(Labels)) {
    generate_barcode_grob(Labels[i], above_text[i], below_text[i])
    if(i!= length(Labels)) # make sure to not add an empty page after last elements are printed
      grid.newpage()
  }
  dev.off()
}

# Example usage with custom above and below text for each label
Labels <- c("515200297","Test")
above_text <- c("سوسپانسیون ضد آکنه و لک آزلائی \n 0.00 |  ندارد ","Text above")   # Custom text above each barcode
below_text <- c("498-34559","Text below")   # Custom text below each barcode

barcode_bars_extended(Labels, above_text = above_text, below_text = below_text, output_file = "barcodes_with_labels.pdf")
发布评论

评论列表(0)

  1. 暂无评论