001/*
002 * Copyright 2007 ZXing authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.itextpdf.text.pdf.qrcode;
018
019/**
020 * <p>See ISO 18004:2006, 6.5.1. This enum encapsulates the four error correction levels
021 * defined by the QR code standard.</p>
022 *
023 * @author Sean Owen
024 * @since 5.0.2
025 */
026public final class ErrorCorrectionLevel {
027
028  // No, we can't use an enum here. J2ME doesn't support it.
029
030  /**
031   * L = ~7% correction
032   */
033  public static final ErrorCorrectionLevel L = new ErrorCorrectionLevel(0, 0x01, "L");
034  /**
035   * M = ~15% correction
036   */
037  public static final ErrorCorrectionLevel M = new ErrorCorrectionLevel(1, 0x00, "M");
038  /**
039   * Q = ~25% correction
040   */
041  public static final ErrorCorrectionLevel Q = new ErrorCorrectionLevel(2, 0x03, "Q");
042  /**
043   * H = ~30% correction
044   */
045  public static final ErrorCorrectionLevel H = new ErrorCorrectionLevel(3, 0x02, "H");
046
047  private static final ErrorCorrectionLevel[] FOR_BITS = {M, L, H, Q};
048
049  private final int ordinal;
050  private final int bits;
051  private final String name;
052
053  private ErrorCorrectionLevel(int ordinal, int bits, String name) {
054    this.ordinal = ordinal;
055    this.bits = bits;
056    this.name = name;
057  }
058
059  public int ordinal() {
060    return ordinal;
061  }
062
063  public int getBits() {
064    return bits;
065  }
066
067  public String getName() {
068    return name;
069  }
070
071  public String toString() {
072    return name;
073  }
074
075  /**
076   * @param bits int containing the two bits encoding a QR Code's error correction level
077   * @return {@link ErrorCorrectionLevel} representing the encoded error correction level
078   */
079  public static ErrorCorrectionLevel forBits(int bits) {
080    if (bits < 0 || bits >= FOR_BITS.length) {
081      throw new IllegalArgumentException();
082    }
083    return FOR_BITS[bits];
084  }
085
086
087}