BarcodeUtils.java

  1. /*
  2.  * Copyright 2000-2014 Namics AG. All rights reserved.
  3.  */

  4. package com.namics.commons.random.support;

  5. /**
  6.  * BarcodeUtils.
  7.  *
  8.  * @author pnueesch
  9.  * @since 08.05.14 13:00
  10.  */
  11. public class BarcodeUtils {

  12.     public static String createEanCheckSumDigit(String value) {
  13.         try {
  14.             int total = 0;
  15.             for (int i = 0; i < value.length(); i++) {
  16.                 int digit = Integer.parseInt(value.substring(value.length() - i - 1, value.length() - i));
  17.                 total += digit * ((i % 2 == 1) ? 1 : 3);
  18.             }
  19.             int checkSum = 10 - total % 10;
  20.             return String.valueOf(checkSum % 10);
  21.         } catch (NumberFormatException nfe) {
  22.             return null;
  23.         }
  24.     }

  25.     public static String createIsbn10CheckSumDigit(String value) {
  26.         try {
  27.             int total = 0;
  28.             for (int i = 1; i <= value.length(); i++) {
  29.                 int digit = Integer.parseInt(value.substring(i - 1, i));
  30.                 total += i * digit;
  31.             }
  32.             int modulo11 = total % 11;
  33.             return (modulo11 != 10) ? String.valueOf(modulo11) : "X";
  34.         } catch (NumberFormatException nfe) {
  35.             return null;
  36.         }
  37.     }
  38. }