Android Byte Array to Unicode Convert parseToBnW(byte[] data, int length)

Here you can find the source of parseToBnW(byte[] data, int length)

Description

Convert a TS 131.102 image instance of code scheme '11' into Bitmap

License

Apache License

Parameter

Parameter Description
data The raw data
length The length of image body

Return

The bitmap

Declaration

public static Bitmap parseToBnW(byte[] data, int length) 

Method Source Code

//package com.java2s;
/*// w w w  .  jav  a 2s  .  c om
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;

public class Main {
    static final String LOG_TAG = "IccUtils";

    /**
     * Convert a TS 131.102 image instance of code scheme '11' into Bitmap
     * @param data The raw data
     * @param length The length of image body
     * @return The bitmap
     */
    public static Bitmap parseToBnW(byte[] data, int length) {
        int valueIndex = 0;
        int width = data[valueIndex++] & 0xFF;
        int height = data[valueIndex++] & 0xFF;
        int numOfPixels = width * height;

        int[] pixels = new int[numOfPixels];

        int pixelIndex = 0;
        int bitIndex = 7;
        byte currentByte = 0x00;
        while (pixelIndex < numOfPixels) {
            // reassign data and index for every byte (8 bits).
            if (pixelIndex % 8 == 0) {
                currentByte = data[valueIndex++];
                bitIndex = 7;
            }
            pixels[pixelIndex++] = bitToRGB((currentByte >> bitIndex--) & 0x01);
        }

        if (pixelIndex != numOfPixels) {
            Log.e(LOG_TAG, "parse end and size error");
        }
        return Bitmap.createBitmap(pixels, width, height,
                Bitmap.Config.ARGB_8888);
    }

    private static int bitToRGB(int bit) {
        if (bit == 1) {
            return Color.WHITE;
        } else {
            return Color.BLACK;
        }
    }
}

Related

  1. bcdToString(byte[] data, int offset, int length)
  2. upperToLower(byte[] b)
  3. upperToLowerLatin(byte[] b)
  4. tag2bytesBE(int tag, byte[] b, int off)
  5. tag2bytesLE(int tag, byte[] b, int off)