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.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.ImageFormat;

import android.graphics.Rect;
import android.graphics.YuvImage;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicYuvToRGB;
import android.renderscript.Type;
import android.util.TimingLogger;

public class Main {
    static RenderScript mRS = null;
    static ScriptIntrinsicYuvToRGB mYuvToRgb = null;
    static Allocation ain = null;
    static Allocation aOut = null;
    static Bitmap bitmap = null;
    static final String TIMING_LOG_TAG = "CvUtils timing";

    @SuppressLint("NewApi")
    public static Bitmap NV21ToRGBABitmap(byte[] nv21, int width, int height, Context context) {

        TimingLogger timings = new TimingLogger(TIMING_LOG_TAG, "NV21ToRGBABitmap");

        Rect rect = new Rect(0, 0, width, height);

        try {
            Class.forName("android.renderscript.Element$DataKind").getField("PIXEL_YUV");
            Class.forName("android.renderscript.ScriptIntrinsicYuvToRGB");
            byte[] imageData = nv21;
            if (mRS == null) {
                mRS = RenderScript.create(context);
                mYuvToRgb = ScriptIntrinsicYuvToRGB.create(mRS, Element.U8_4(mRS));
                Type.Builder tb = new Type.Builder(mRS,
                        Element.createPixel(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV));
                tb.setX(width);
                tb.setY(height);
                tb.setMipmaps(false);
                tb.setYuvFormat(ImageFormat.NV21);
                ain = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
                timings.addSplit("Prepare for ain");
                Type.Builder tb2 = new Type.Builder(mRS, Element.RGBA_8888(mRS));
                tb2.setX(width);
                tb2.setY(height);
                tb2.setMipmaps(false);
                aOut = Allocation.createTyped(mRS, tb2.create(), Allocation.USAGE_SCRIPT & Allocation.USAGE_SHARED);
                timings.addSplit("Prepare for aOut");
                bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                timings.addSplit("Create Bitmap");
            }
            ain.copyFrom(imageData);
            timings.addSplit("ain copyFrom");
            mYuvToRgb.setInput(ain);
            timings.addSplit("setInput ain");
            mYuvToRgb.forEach(aOut);
            timings.addSplit("NV21 to ARGB forEach");
            aOut.copyTo(bitmap);
            timings.addSplit("Allocation to Bitmap");
        } catch (Exception e) {
            YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
            timings.addSplit("NV21 bytes to YuvImage");

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            yuvImage.compressToJpeg(rect, 90, baos);
            byte[] cur = baos.toByteArray();
            timings.addSplit("YuvImage crop and compress to Jpeg Bytes");

            bitmap = BitmapFactory.decodeByteArray(cur, 0, cur.length);
            timings.addSplit("Jpeg Bytes to Bitmap");
        }

        timings.dumpToLog();
        return bitmap;
    }

    static public Bitmap NV21ToRGBABitmap(byte[] nv21, int width, int height) {
        YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, baos);
        byte[] cur = baos.toByteArray();
        return BitmapFactory.decodeByteArray(cur, 0, cur.length);
    }
}