Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.ByteArrayOutputStream;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;

import android.widget.ImageView;

public class Main {
    /***
     * 
     * @param imageId The image from layout
     * @param resources From which activity
     * @return Serialized image
     */
    public static byte[] ImageToByte(int imageId, Resources resources) {
        Bitmap bitmap = BitmapFactory.decodeResource(resources, imageId);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    public static byte[] ImageToByte(ImageView image) {
        Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
        Bitmap resized = Bitmap.createScaledBitmap(bitmap, 150, 150, true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        resized.compress(Bitmap.CompressFormat.PNG, 100, baos);

        return baos.toByteArray();
    }
}