Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.util.Log;

public class Main {
    private static String TAG = "Util";

    /**
     * 
     * @return Resource's RGBA byte array
     */
    public static byte[] getImageRGBA(Resources res, int resouceId) {
        Bitmap bitmap = BitmapFactory.decodeResource(res, resouceId);
        if (bitmap.getWidth() != 32 || bitmap.getHeight() != 32) {
        }
        return getImageRGBA(bitmap);
    }

    /**
     * 
     * @return Bitmap's RGBA byte array
     */
    public static byte[] getImageRGBA(Bitmap inputBitmap) {
        Config config = inputBitmap.getConfig();
        ByteBuffer buffer;

        Bitmap bitmap;
        /**
         * if bitmap size is not 32*32 create scaled bitmap
         */

        if (inputBitmap.getWidth() != 32 || inputBitmap.getHeight() != 32) {
            Log.d(TAG, "bitmap resized to 32x32");
            bitmap = Bitmap.createScaledBitmap(inputBitmap, 32, 32, false);
        } else {
            bitmap = inputBitmap;
        }
        /**
         * if bitmap is not ARGB_8888 format, copy bitmap with ARGB_8888 format
         */
        if (!config.equals(Bitmap.Config.ARGB_8888)) {
            Bitmap bitmapARBG = bitmap.copy(Bitmap.Config.ARGB_8888, false);
            buffer = ByteBuffer.allocate(bitmapARBG.getByteCount());
            bitmapARBG.copyPixelsToBuffer(buffer);
            bitmapARBG.recycle();
        } else {
            buffer = ByteBuffer.allocate(bitmap.getByteCount());
            bitmap.copyPixelsToBuffer(buffer);
        }
        return buffer.array();
    }
}