Code example for Bitmap

Methods: getConfiggetHeightgetPixelsgetWidthrecycle

0
	 
	public static native void nativeLoadResizedJpegBitmap(byte[] jpegData, int size, int maxPixels);
	public static native void nativeResizeBitmap(int newWidth, int newHeight);
	 
	private static void sendBitmapToNative(Bitmap bitmap) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		nativeInitBitmap(width, height);
		int[] pixels = new int[width];
		for (int y = 0; y < height; y++) {
			bitmap.getPixels(pixels, 0, width, 0, y, width, 1);
			nativeSetBitmapRow(y, pixels);
		} 
	} 
	 
	private static Bitmap getBitmapFromNative(Bitmap bitmap) {
		int width = nativeGetBitmapWidth();
		int height = nativeGetBitmapHeight();
		 
		if (bitmap == null || width != bitmap.getWidth() || height != bitmap.getHeight() || !bitmap.isMutable()) { //in case it was rotated and the dimensions changed
			Config config = Config.ARGB_8888;
			if (bitmap != null) {
				config = bitmap.getConfig();
				bitmap.recycle();
			} 
			bitmap = Bitmap.createBitmap(width, height, config);
		} 
 
		int[] pixels = new int[width];
		for (int y = 0; y < height; y++) {
			nativeGetBitmapRow(y, pixels);
			bitmap.setPixels(pixels, 0, width, 0, y, width, 1);
		} 
				 
		return bitmap;
	} 
	 
	public static Bitmap makeBitmapMutable(Bitmap bitmap) {
		sendBitmapToNative(bitmap);
		return getBitmapFromNative(bitmap);
	} 
	 
	public static Bitmap rotate(Bitmap bitmap, int angle) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		Config config = bitmap.getConfig();
		nativeInitBitmap(width, height);
		sendBitmapToNative(bitmap);
		 
		if (angle == 90) {
			nativeRotate90(); 
			bitmap.recycle();
			bitmap = Bitmap.createBitmap(height, width, config);
			bitmap = getBitmapFromNative(bitmap);
			nativeDeleteBitmap(); 
		} else if (angle == 180) {
			nativeRotate180(); 
			bitmap.recycle();
			bitmap = Bitmap.createBitmap(width, height, config);
			bitmap = getBitmapFromNative(bitmap);
			nativeDeleteBitmap(); 
		} else if (angle == 270) {
			nativeRotate180(); 
			nativeRotate90(); 
			bitmap.recycle();
			bitmap = Bitmap.createBitmap(height, width, config);
			bitmap = getBitmapFromNative(bitmap);
			nativeDeleteBitmap(); 
		} 
		 
		return bitmap;
	} 
	 
	public static Bitmap flipHorizontally(Bitmap bitmap) {
		nativeInitBitmap(bitmap.getWidth(), bitmap.getHeight());
		sendBitmapToNative(bitmap);
		nativeFlipHorizontally(); 
		bitmap = getBitmapFromNative(bitmap);
		nativeDeleteBitmap(); 
		return bitmap;
Experience pair programming with AI  Get Codota for Java