Android Open Source - BinaryContent Random Image






From Project

Back to project page BinaryContent.

License

The source code is released under:

MIT License

If you think the Android project BinaryContent listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.sourcewalker.binary;
//from   w ww .ja  v a2 s . c om
import java.nio.ByteBuffer;
import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

public class RandomImage {

    private static final int DEFAULT_WIDTH = 96;
    private static final int DEFAULT_HEIGHT = 96;
    private static final int PIXEL_SIZE = 4;

    private Random rnd;
    private int width;
    private int height;
    private Bitmap bitmap;

    public RandomImage(int width, int height, long seed) {
        this.rnd = new Random(seed);
        this.width = width;
        this.height = height;

        createImage();
    }

    public RandomImage(long seed) {
        this(DEFAULT_WIDTH, DEFAULT_HEIGHT, seed);
    }

    public Bitmap getBitmap() {
        return bitmap;
    }

    private void createImage() {
        int pixels = width * height;
        ByteBuffer buffer = ByteBuffer.allocate(pixels * PIXEL_SIZE);
        for (int i = 0; i < pixels; i++) {
            buffer.put((byte) rnd.nextInt(255));
            buffer.put((byte) rnd.nextInt(255));
            buffer.put((byte) rnd.nextInt(255));
            buffer.put((byte) 255);
        }
        buffer.rewind();
        bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        bitmap.copyPixelsFromBuffer(buffer);
    }

}




Java Source Code List

net.sourcewalker.binary.ImageCreateService.java
net.sourcewalker.binary.ImageListActivity.java
net.sourcewalker.binary.ImagesProvider.java
net.sourcewalker.binary.RandomImage.java