Java 类android.renderscript.ScriptIntrinsicYuvToRGB 实例源码

项目:hella-renderscript    文件:RsCameraPreviewRenderer.java   
/**
 * @param rs
 * @param rsRenderer
 * @param x
 * @param y
 * @param renderHandler
 */
public RsCameraPreviewRenderer(RenderScript rs, RsRenderer rsRenderer, int x, int y,
        Handler renderHandler) {
    this.rs = rs;
    this.rsRenderer = rsRenderer;

    if (renderHandler == null) {
        this.renderThread = new HandlerThread(TAG);
        this.renderThread.start();
        this.renderHandler = new Handler(renderThread.getLooper());
    } else {
        this.renderThread = null;
        this.renderHandler = renderHandler;
    }

    Log.i(TAG,
            "Setting up RsCameraPreviewRenderer with " + rsRenderer.getName() + " (" + x + "," +
                    y + ")");

    yuvInAlloc = RsUtil.createYuvIoInputAlloc(rs, x, y, ImageFormat.YUV_420_888);
    yuvInAlloc.setOnBufferAvailableListener(this);

    rgbInAlloc = RsUtil.createRgbAlloc(rs, x, y);
    rgbOutAlloc = RsUtil.createRgbIoOutputAlloc(rs, x, y);

    yuvToRGBScript = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(rs));
    yuvToRGBScript.setInput(yuvInAlloc);
}
项目:AARemu    文件:Camera2CameraRenderer.java   
public Camera2Callback()
//----------------------
{
   rs = RenderScript.create(activity);
   switch (fileFormat)
   {
      case YUV_420:
         YUVToRGBA = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(rs));
         Type.Builder yuvTypeBuilder = new Type.Builder(rs, Element.YUV(rs));
         yuvTypeBuilder.setX(previewWidth).setY(previewHeight).setYuvFormat(ImageFormat.YUV_420_888);
         ain = Allocation.createTyped(rs, yuvTypeBuilder.create(), Allocation.USAGE_SCRIPT);

         Type.Builder rgbTypeBuilder = new Type.Builder(rs, Element.RGBA_8888(rs));
         rgbTypeBuilder.setX(previewWidth).setY(previewHeight);
         aOut = Allocation.createTyped(rs, rgbTypeBuilder.create(), Allocation.USAGE_SCRIPT);
         break;
      case NV21:
         YUVToRGBA = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
         Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(previewWidth).
               setY(previewHeight).setMipmaps(false).setYuvFormat(ImageFormat.NV21);
         Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(previewWidth).
               setY(previewHeight).setMipmaps(false);
         ain = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
         aOut = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
         break;
   }
}
项目:AARemu    文件:PreviewCamera.java   
public byte[] toRGBA(Context context, byte[] frame, int previewWidth, int previewHeight, int rgbaSize, byte[] grey)
//----------------------------------------------------------------------------------------------------------------
{
   try
   {
      final int inputFormat = ImageFormat.YUV_420_888;
      Type.Builder yuvTypeBuilder = new Type.Builder(renderscript, Element.YUV(renderscript));
      yuvTypeBuilder.setX(previewWidth).setY(previewHeight).setYuvFormat(inputFormat);
      Allocation allocYUVIn = Allocation.createTyped(renderscript, yuvTypeBuilder.create(), Allocation.USAGE_SCRIPT);

      Type.Builder rgbTypeBuilder = new Type.Builder(renderscript, Element.RGBA_8888(renderscript));
      rgbTypeBuilder.setX(previewWidth).setY(previewHeight);
      Allocation allocRGBAOut = Allocation.createTyped(renderscript, rgbTypeBuilder.create(), Allocation.USAGE_SCRIPT);

      ScriptIntrinsicYuvToRGB YUVToRGB = ScriptIntrinsicYuvToRGB.create(renderscript, Element.RGBA_8888(renderscript));
      allocYUVIn.copyFrom(frame);
      YUVToRGB.setInput(allocYUVIn);
      YUVToRGB.forEach(allocRGBAOut);
      byte[] rgbaBuffer = new byte[rgbaSize];
      allocRGBAOut.copyTo(rgbaBuffer);
      if (grey != null)
      {
         Type.Builder greyTypeBuilder = new Type.Builder(renderscript, Element.U8(renderscript));
         greyTypeBuilder.setX(cameraWidth).setY(cameraHeight);
         Allocation allocGrayOut = Allocation.createTyped(renderscript, greyTypeBuilder.create(), Allocation.USAGE_SCRIPT);
         ScriptC_yuv2grey rsYUVtoGrey = new ScriptC_yuv2grey(renderscript);
         allocYUVIn.copyFrom(frame);
         rsYUVtoGrey.set_in(allocYUVIn);
         rsYUVtoGrey.forEach_yuv2grey(allocGrayOut);
         allocGrayOut.copyTo(grey);
      }
      return rgbaBuffer;
   }
   catch (Exception e)
   {
      Log.e(LOGTAG, "", e);
      return null;
   }
}
项目:Fatigue-Detection    文件:STUtils.java   
@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;
}
项目:renderscript_examples    文件:MainActivity.java   
private void instantiateRS() {
    mRS = RenderScript.create(this); //, RenderScript.ContextType.DEBUG);

    timings.setTimingCallback(new Timings.TimingCallback() {
        @Override
        public void run() {
            mRS.finish();
        }
    });

    inputImageSize = cameraHandler.getCameraSize();

    // Initialize holder for NDK FAST extraction implementation
    setImageSize(inputImageSize.width, inputImageSize.height);

    // Initialize RenderScript scripts
    scriptIntrinsicYuvToRGB = ScriptIntrinsicYuvToRGB.create(mRS, Element.RGBA_8888(mRS));
    customYUVToGrayscaleConverter = new ScriptC_customYUVToGrayscaleConverter(mRS);
    scriptCFastNoOptimization = new ScriptC_fast_no_optimization(mRS);
    scriptCFast = new ScriptC_fast(mRS);
    scriptCFastOpenCV = new ScriptC_fast_opencv(mRS);
    scriptCUtil = new ScriptC_util(mRS);

    // Build type for YUV input image
    Type.Builder tb;
    tb = new Type.Builder(mRS, Element.createPixel(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV));
    tb.setX(inputImageSize.width);
    tb.setY(inputImageSize.height);
    tb.setYuvFormat(android.graphics.ImageFormat.NV21);

    // Create input allocation, that will receive the camera frame
    inputAllocation = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
    scriptIntrinsicYuvToRGB.setInput(inputAllocation);
    customYUVToGrayscaleConverter.invoke_setInputImageSize(inputImageSize.width, inputImageSize.height);
    customYUVToGrayscaleConverter.set_inputAllocation(inputAllocation);

    // Build type for converted image (YUV to RGBA)
    tb = new Type.Builder(mRS, Element.RGBA_8888(mRS)).setX(inputImageSize.width).setY(inputImageSize.height);
    Type rgbaType = tb.create();
    // Build type for converted image (RGBA to GRAY)
    tb = new Type.Builder(mRS, Element.U8(mRS)).setX(inputImageSize.width).setY(inputImageSize.height);
    Type grayType = tb.create();

    // Define all allocations
    rgbAllocation = Allocation.createTyped(mRS, rgbaType, Allocation.USAGE_SCRIPT);
    grayAllocation = Allocation.createTyped(mRS, grayType, Allocation.USAGE_SCRIPT);
    fastKpAllocation = Allocation.createTyped(mRS, grayType, Allocation.USAGE_SCRIPT);
    outputAllocation = Allocation.createTyped(mRS, rgbaType, Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT);

    // Tells the output allocation which surface is its
    outputAllocation.setSurface(rsResultSurface);

    // Prepare RS scripts
    scriptCFastNoOptimization.set_grayAllocation(grayAllocation);
    //scriptCFast.set_grayAllocation(grayAllocation);
    scriptCFast.set_grayAllocation(grayAllocation);
    scriptCFastOpenCV.set_grayAllocation(grayAllocation);

    // Defines limits for RS kernels execution, as FAST extraction requires
    // a border of 3 pixels to operate and harris score requires 4 of them, so
    // the maximum is chosen.
    fastLaunchOptions = new Script.LaunchOptions();
    fastLaunchOptions.setX(3, inputImageSize.width - 3);
    fastLaunchOptions.setY(3, inputImageSize.height - 3);

    // Settings this to true tells the camera preview callback that the RS code
    // can be executed
    rsInstantiated = true;
}
项目:Sketchfit    文件:CameraView.java   
public CameraView(int idealPreviewWidth, int idealPreviewHeight, ImageView CameraPreview, boolean processingEnabled, TextView textFps, Context context)
{
    Camera.Size suitableSize = FindSuitableCameraSize(idealPreviewWidth, idealPreviewHeight);
    if (suitableSize != null) {
        previewWidth = suitableSize.width;
        previewHeight = suitableSize.height;
    } else {
        outWidth = idealPreviewWidth;
        outHeight = idealPreviewHeight;
    }
    previewWidth = 2 * outWidth;
    previewHeight = 2 * outHeight;

    this.MyCameraPreview = CameraPreview;
    this.processingEnabled = processingEnabled;
    this.textFps = textFps;

    previousTime = 0;
    textFps.setText("x");

    outputBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);

    Bitmap tmpBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);

    kernelContext = RenderScript.create(context);
    scriptIP = new ScriptC_imageprocessing(kernelContext);
    allocationOut = Allocation.createFromBitmap(kernelContext, outputBitmap);
    allocationIn =  Allocation.createFromBitmap(kernelContext, tmpBitmap);

    Type.Builder typeYUV = new Type.Builder(kernelContext, Element.createPixel(kernelContext, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV));
    typeYUV.setYuvFormat(ImageFormat.NV21);
    // allocation for the YUV input from the camera
    allocationYUV = Allocation.createTyped(kernelContext, typeYUV.setX(previewWidth).setY(previewHeight).create(), Allocation.USAGE_SCRIPT);
    // allocations for image processing
    allocationTmp1 = Allocation.createTyped(kernelContext, Type.createXY(kernelContext, Element.F32_3(kernelContext), previewWidth, previewHeight), Allocation.USAGE_SCRIPT);
    allocationTmp2 = Allocation.createTyped(kernelContext, allocationTmp1.getType(), Allocation.USAGE_SCRIPT);
    allocationTmp5 = Allocation.createTyped(kernelContext, allocationTmp1.getType(), Allocation.USAGE_SCRIPT);
    allocationEdges = Allocation.createTyped(kernelContext, Type.createXY(kernelContext, Element.F32(kernelContext), previewWidth, previewHeight), Allocation.USAGE_SCRIPT);
    allocationTmp3 = Allocation.createTyped(kernelContext, Type.createXY(kernelContext, Element.F32_3(kernelContext), outWidth, outHeight), Allocation.USAGE_SCRIPT);
    allocationTmp4 = Allocation.createTyped(kernelContext, allocationTmp3.getType(), Allocation.USAGE_SCRIPT);
    //create the instance of the YUV2RGB (built-in) RS intrinsic
    scriptYuvToRGB = ScriptIntrinsicYuvToRGB.create(kernelContext, Element.U8_4(kernelContext));

    // set initial RenderScript global values
    scriptIP.set_edgeBuffer(allocationEdges);
    scriptIP.set_imageWidth(previewWidth);
    scriptIP.set_imageHeight(previewHeight);
    scriptIP.set_sImageWidth(outWidth);
    scriptIP.set_sImageHeight(outHeight);
}
项目:AARemu    文件:LegacyPreviewCamera.java   
@Override
public byte[] toRGBA(Context context, byte[] frame, int previewWidth, int previewHeight, int rgbaSize, byte[] grey)
//-----------------------------------------------------------------------------------------------------------------
{
   ScriptIntrinsicYuvToRGB YUVToRGB = null;
   byte[] rgbaBuffer = null;
   try
   {
      Type.Builder yuvType = new Type.Builder(renderscript, Element.U8(renderscript)).setX(previewWidth).
            setY(previewHeight).setMipmaps(false);
      yuvType.setYuvFormat(ImageFormat.NV21);
      Allocation ain = Allocation.createTyped(renderscript, yuvType.create(), Allocation.USAGE_SCRIPT);

      Type.Builder rgbType = null;
      YUVToRGB = ScriptIntrinsicYuvToRGB.create(renderscript, Element.U8_4(renderscript));
      rgbType = new Type.Builder(renderscript, Element.RGBA_8888(renderscript));
      rgbaBuffer = new byte[rgbaSize];
      rgbType.setX(previewWidth).setY(previewHeight).setMipmaps(false);
      Allocation aOut = Allocation.createTyped(renderscript, rgbType.create(), Allocation.USAGE_SCRIPT);
      ain.copyFrom(frame);
      YUVToRGB.setInput(ain);
      YUVToRGB.forEach(aOut);
      aOut.copyTo(rgbaBuffer);
      if (grey != null)
      {
         Allocation allocGrayOut = null;
         ScriptC_yuv2grey rsYUVtoGrey = null;
         Type.Builder greyTypeBuilder = new Type.Builder(renderscript, Element.U8(renderscript));
         greyTypeBuilder.setX(previewWidth).setY(previewHeight);
         allocGrayOut = Allocation.createTyped(renderscript, greyTypeBuilder.create(), Allocation.USAGE_SCRIPT);
         rsYUVtoGrey = new ScriptC_yuv2grey(renderscript);
         Allocation ainbw = Allocation.createTyped(renderscript, yuvType.create(), Allocation.USAGE_SCRIPT);
         ainbw.copyFrom(frame);
         rsYUVtoGrey.set_in(ainbw);
         rsYUVtoGrey.forEach_yuv2grey(allocGrayOut);
         allocGrayOut.copyTo(grey);
      }
      return rgbaBuffer;
   }
   catch (Exception e)
   {
      Log.e(LOGTAG, "", e);
      return null;
   }
}