Render (dynamically) an image from Java Bitmap

Hi All,

Do you have an example or some hints on how we could render a bitmap in a Panel, starting from a Java native Bitmap object? (obtained dynamically depending on the user)

Thanks in advance for your support

Hey!

I whipped the following together. It’s a bit hacky and requires some format conversions and whatnot, but it should give you an idea of how it can be done:

JavaBitmap.uno:

using Uno;
using Uno.Compiler.ExportTargetInterop;
using Uno.Graphics;
using Fuse.Resources;

[ForeignInclude(Language.Java, "android.graphics.*")]
public class JavaBitmap
{
	public JavaBitmap()
	{
		MainView.textureImageSource.Texture = CreateANiceTexture();
	}

	public texture2D CreateANiceTexture()
	{
		if defined(Android)
			return CreateTextureFromJavaBitmap(CreateANiceBitmap());
		throw new Exception("no");
	}

	[Foreign(Language.Java)]
	static extern(Android) Java.Object CreateANiceBitmap()
	@{
		int width = 640;
		int height = 480;
		int[] colors = new int[width * height];
		for (int x = 0; x < width; ++x)
		for (int y = 0; y < height; ++y)
		{
			colors[x + y * width]
				= height / 5 * 2 < y && y < height / 5 * 3
				|| width / 5 * 1 < x && x < width / 5 * 2
				? Color.YELLOW
				: Color.BLUE;
		}
		return Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);
	@}

	[Foreign(Language.Java)]
	static extern(Android) int GetBitmapWidth(Java.Object bitmap) @{ return ((Bitmap)bitmap).getWidth(); @}
	[Foreign(Language.Java)]
	static extern(Android) int GetBitmapHeight(Java.Object bitmap) @{ return ((Bitmap)bitmap).getHeight(); @}

	[Foreign(Language.Java)]
	static extern(Android) void GetBitmapPixels(Java.Object bitmapHandle, int[] output)
	@{
		Bitmap bitmap = (Bitmap)bitmapHandle;
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		for (int x = 0; x < width; ++x)
		for (int y = 0; y < height; ++y)
		{
			output.set(x + y * width, bitmap.getPixel(x, y));
		}
	@}

	static extern(Android) texture2D CreateTextureFromJavaBitmap(Java.Object bitmap)
	{
		var width = GetBitmapWidth(bitmap);
		var height = GetBitmapHeight(bitmap);
		var bitmapData = new int[width * height];
		GetBitmapPixels(bitmap, bitmapData);
		var buffer = new Buffer(sizeof(int) * bitmapData.Length);
		for (int i = 0; i < bitmapData.Length; ++i)
		{
			var argb = bitmapData[i];
			var a = (byte)(argb >> 24);
			var r = (byte)(argb >> 16);
			var g = (byte)(argb >> 8);
			var b = (byte)(argb >> 0);
			buffer.Set(sizeof(int) * i + 0, r);
			buffer.Set(sizeof(int) * i + 1, g);
			buffer.Set(sizeof(int) * i + 2, b);
			buffer.Set(sizeof(int) * i + 3, a);
		}
		var result = new texture2D(int2(width, height), Format.RGBA8888, false);
		result.Update(buffer);
		return result;
	}
}

MainView.ux:

<App>
  <Panel>
    <TextureImageSource ux:Global="textureImageSource"/>
    <JavaBitmap ux:Global="javaBitmap"/>
    <Image Source="textureImageSource" />
  </Panel>
</App>

Hope that helps!

Hi Olle and thanks a lot for the answer :smiley: