@Override public Image newImage(String fileName, ImageFormat format) { Config config = null; if (format == ImageFormat.RGB565) config = Config.RGB_565; else if (format == ImageFormat.ARGB4444) config = Config.ARGB_4444; else config = Config.ARGB_8888; Options options = new Options(); options.inPreferredConfig = config; InputStream in = null; Bitmap bitmap = null; try { in = assets.open(fileName); bitmap = BitmapFactory.decodeStream(in, null, options); if (bitmap == null) throw new RuntimeException("Couldn't load bitmap from asset '" + fileName + "'"); } catch (IOException e) { throw new RuntimeException("Couldn't load bitmap from asset '" + fileName + "'"); } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } } if (bitmap.getConfig() == Config.RGB_565) format = ImageFormat.RGB565; else if (bitmap.getConfig() == Config.ARGB_4444) format = ImageFormat.ARGB4444; else format = ImageFormat.ARGB8888; return new AndroidImage(bitmap, format); }
@Override public Pixmap newPixmap(String fileName, PixmapFormat format) { Config config = null; if (format == PixmapFormat.RGB565) config = Config.RGB_565; else if (format == PixmapFormat.ARGB4444) config = Config.ARGB_4444; else config = Config.ARGB_8888; Options options = new Options(); options.inPreferredConfig = config; InputStream in = null; Bitmap bitmap = null; try { in = assets.open(fileName); bitmap = BitmapFactory.decodeStream(in); if (bitmap == null) throw new RuntimeException("No se ha podido cargar bitmap desde asset '" + fileName + "'"); } catch (IOException e) { throw new RuntimeException("No se ha podido cargar bitmap desde asset '" + fileName + "'"); } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } } if (bitmap.getConfig() == Config.RGB_565) format = PixmapFormat.RGB565; else if (bitmap.getConfig() == Config.ARGB_4444) format = PixmapFormat.ARGB4444; else format = PixmapFormat.ARGB8888; return new AndroidPixmap(bitmap, format); }
public boolean prepareRenderscriptAllocations(Bitmap bitmap) { RenderScript RS = getRenderScriptContext(); boolean needsUpdate = false; if (mOutPixelsAllocation == null || mInPixelsAllocation == null || bitmap.getWidth() != mWidth || bitmap.getHeight() != mHeight) { destroyPixelAllocations(); Bitmap bitmapBuffer = bitmap; if (bitmap.getConfig() == null || bitmap.getConfig() != BITMAP_CONFIG) { bitmapBuffer = bitmap.copy(BITMAP_CONFIG, true); } mOutPixelsAllocation = Allocation.createFromBitmap( RS, bitmapBuffer, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); mInPixelsAllocation = Allocation.createTyped(RS, mOutPixelsAllocation.getType()); needsUpdate = true; } if (RS != null) { mInPixelsAllocation.copyFrom(bitmap); } if (bitmap.getWidth() != mWidth || bitmap.getHeight() != mHeight) { mWidth = bitmap.getWidth(); mHeight = bitmap.getHeight(); needsUpdate = true; } if (DEBUG) { Log.v(LOGTAG, "prepareRenderscriptAllocations: " + needsUpdate + " in " + getName()); } return needsUpdate; }
/** * Assert that two bitmaps are equal. * * @param Bitmap b1 the first bitmap which needs to compare. * @param Bitmap b2 the second bitmap which needs to compare. */ public static void assertEquals(Bitmap b1, Bitmap b2) { if (b1 == b2) { return; } if (b1 == null || b2 == null) { Assert.fail("the bitmaps are not equal"); } // b1 and b2 are all not null. if (b1.getWidth() != b2.getWidth() || b1.getHeight() != b2.getHeight() || b1.getConfig() != b2.getConfig()) { Assert.fail("the bitmaps are not equal"); } int w = b1.getWidth(); int h = b1.getHeight(); int s = w * h; int[] pixels1 = new int[s]; int[] pixels2 = new int[s]; b1.getPixels(pixels1, 0, w, 0, 0, w, h); b2.getPixels(pixels2, 0, w, 0, 0, w, h); for (int i = 0; i < s; i++) { if (pixels1[i] != pixels2[i]) { Assert.fail("the bitmaps are not equal"); } } }
@Override public Bitmap transform(Bitmap source) { int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); if (squaredBitmap != source) { source.recycle(); } if (source.getConfig() == null) { return source; } Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); paint.setShader(shader); paint.setAntiAlias(true); float r = size / 2f; canvas.drawCircle(r, r, r, paint); squaredBitmap.recycle(); return bitmap; }
@Override public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) { Bitmap source = resource.get(); int width = source.getWidth(); int height = source.getHeight(); Bitmap.Config config = source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888; Bitmap bitmap = mBitmapPool.get(width, height, config); if (bitmap == null) { bitmap = Bitmap.createBitmap(width, height, config); } Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); canvas.drawRoundRect( new RectF(margin, margin, width - margin, height - margin), radius, radius, paint); source.recycle(); return BitmapResource.obtain(bitmap, mBitmapPool); }
public void loadFromCroppedColorArray(int x, int y, int width, int height) { int[] pixels = new int[width * height]; mOriginalBitmap.getPixels(pixels, 0, width, x, y, width, height); CloseableReference<Bitmap> bitmapRef = mBitmapFactory.createBitmap(pixels, width, height, mOriginalBitmap.getConfig()); setImageBitmap(bitmapRef); checkValid(bitmapRef, Bitmap.createBitmap(pixels, width, height, mOriginalBitmap.getConfig())); }
@Override public void put(Bitmap bitmap) { int size = Util.getBitmapByteSize(bitmap); Key key = keyPool.get(size, bitmap.getConfig()); groupedMap.put(key, bitmap); NavigableMap<Integer, Integer> sizes = getSizesForConfig(bitmap.getConfig()); Integer current = sizes.get(key.size); sizes.put(key.size, current == null ? 1 : current + 1); }
public void loadFromColorArray() { int[] pixels = new int[mOriginalWidth * mOriginalHeight]; mOriginalBitmap.getPixels(pixels, 0, mOriginalWidth, 0, 0, mOriginalWidth, mOriginalHeight); CloseableReference<Bitmap> bitmapRef = mBitmapFactory.createBitmap( pixels, mOriginalWidth, mOriginalHeight, mOriginalBitmap.getConfig()); setImageBitmap(bitmapRef); checkValid( bitmapRef, Bitmap.createBitmap(pixels, mOriginalWidth, mOriginalHeight, mOriginalBitmap.getConfig())); }
@Override @Nullable public Bitmap get(int width, int height, Bitmap.Config config) { int size = Util.getBitmapByteSize(width, height, config); Key bestKey = findBestKey(size, config); Bitmap result = groupedMap.get(bestKey); if (result != null) { // Decrement must be called before reconfigure. decrementBitmapOfSize(bestKey.size, result); result.reconfigure( width, height, result.getConfig() != null ? result.getConfig() : Bitmap.Config.ARGB_8888); } return result; }
@Test public void testFitCenterHandlesBitmapsWithNullConfigs() { Bitmap toFit = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565); Shadows.shadowOf(toFit).setConfig(null); Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, toFit, 50, 50); assertEquals(Bitmap.Config.ARGB_8888, transformed.getConfig()); }
@Override public Bitmap filter(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Logger.v(name + ", multiplier: " + multiplier + ", divider: " + divider + ", bias: " + bias); if (bitmap == null || divider == 0 || (multiplier == 1 && divider == 1 && bias == 0)) { Logger.e("parameter error (bitmap null, divider is zero, or given filter has no effect)"); return null; } int[] bitmapBytes = new int[width * height]; bitmap.getPixels(bitmapBytes, 0, width, 0, 0, width, height); int color; for (int i = 0; i < bitmapBytes.length; i++) { color = bitmapBytes[i]; bitmapBytes[i] = Color.rgb( forcepin(0, r(color) * multiplier / divider + bias, 255), forcepin(0, g(color) * multiplier / divider + bias, 255), forcepin(0, b(color) * multiplier / divider + bias, 255)); } return Bitmap.createBitmap(bitmapBytes, width, height, bitmap.getConfig()); }
public static void getAvatarForFancyQrCode(GetAvatarListener getAvatarListener) { Paint paint = new Paint(); paint.setAntiAlias(true); Resources res = BitherApplication.mContext.getResources(); Bitmap shape = BitmapFactory.decodeResource(res, R.drawable.avatar_for_fancy_qr_code_shape); Bitmap result = Bitmap.createBitmap(shape.getWidth(), shape.getHeight(), shape.getConfig()); Canvas c = new Canvas(result); c.drawBitmap(shape, 0, 0, paint); Paint avatarPaint = new Paint(); avatarPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); avatarPaint.setAntiAlias(true); Bitmap avatarBit = getAvatarBit(); if (avatarBit == null) { if (getAvatarListener != null) { getAvatarListener.fileNoExist(); } return; } c.drawBitmap( avatarBit, null, new Rect(0, 0, result.getWidth(), result.getHeight()), avatarPaint); Bitmap overlay = BitmapFactory.decodeResource(res, R.drawable.avatar_for_fancy_qr_code_overlay); c.drawBitmap(overlay, null, new Rect(0, 0, result.getWidth(), result.getHeight()), paint); if (result != null) { if (getAvatarListener != null) { getAvatarListener.success(result); } } }
public void writeToParcel(Parcel paramParcel, int paramInt) { Bitmap localBitmap; Object localObject; byte[] arrayOfByte; if (zzDy == null) { localBitmap = zzaiT; localObject = ByteBuffer.allocate(localBitmap.getRowBytes() * localBitmap.getHeight()); localBitmap.copyPixelsToBuffer((Buffer) localObject); arrayOfByte = ((ByteBuffer) localObject).array(); localObject = new DataOutputStream(zzpf()); } try { ((DataOutputStream) localObject).writeInt(arrayOfByte.length); ((DataOutputStream) localObject).writeInt(localBitmap.getWidth()); ((DataOutputStream) localObject).writeInt(localBitmap.getHeight()); ((DataOutputStream) localObject).writeUTF(localBitmap.getConfig().toString()); ((DataOutputStream) localObject).write(arrayOfByte); zza((Closeable) localObject); zza.zza(this, paramParcel, paramInt | 0x1); zzDy = null; return; } catch (IOException paramParcel) { throw new IllegalStateException("Could not write into unlinked file", paramParcel); } finally { zza((Closeable) localObject); } throw paramParcel; }
@Override public Bitmap transform(Bitmap source) { int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); if (squaredBitmap != source) { source.recycle(); } Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); Canvas canvas = new Canvas(bitmap); Paint avatarPaint = new Paint(); BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); avatarPaint.setShader(shader); Paint outlinePaint = new Paint(); outlinePaint.setColor(Color.WHITE); outlinePaint.setStyle(Paint.Style.STROKE); outlinePaint.setStrokeWidth(STROKE_WIDTH); outlinePaint.setAntiAlias(true); float r = size / 2f; canvas.drawCircle(r, r, r, avatarPaint); canvas.drawCircle(r, r, r - STROKE_WIDTH / 2, outlinePaint); squaredBitmap.recycle(); return bitmap; }
@Override public void onResult(Bitmap bitmap) { int[][] se = new int[][] { {1, 1}, {1, 1} }; Bitmap bmFinal = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); BitmapUtils.erosion(bitmap, se); int m = BitmapUtils.dilate(bitmap, bmFinal, se); // int m = BitmapUtils.countSE(bitmap, se); int c = BitmapUtils.getPixelsCount(bmFinal); int pc = BitmapUtils.getContactCount(bmFinal); ImageView iv = ((ImageView) getView().findViewById(R.id.image2)); iv.setImageBitmap(bmFinal); iv.invalidate(); int r = c + m - pc; ((TextView) getView().findViewById(R.id.textView1)) .setText("Segments: " + r + " = " + c + " + " + m + " - " + pc); }
private Bitmap FrameProcessingBitmap() { Bitmap bm1 = null; Bitmap newBitmap = null; try { bm1 = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(path)); int w = bm1.getWidth(); int h = bm1.getHeight(); Bitmap.Config config = bm1.getConfig(); if (config == null) { config = Bitmap.Config.ARGB_8888; } newBitmap = Bitmap.createBitmap(w, h, config); Canvas newCanvas = new Canvas(newBitmap); newCanvas.drawColor(Color.WHITE); Paint paint = new Paint(); paint.setColor(Color.BLACK); Rect frame = new Rect((int) (w * 0.05), (int) (w * 0.05), (int) (w * 0.95), (int) (h * 0.95)); RectF frameF = new RectF(frame); newCanvas.drawRoundRect(frameF, (float) (w * 0.05), (float) (h * 0.05), paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN)); newCanvas.drawBitmap(bm1, 0, 0, paint); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return newBitmap; }
/** * Blurs the given Bitmap using RenderScript. * * @param context * @param bitmap * @return */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) private static Bitmap blurUsingRenderScript(final Context context, final Bitmap bitmap) { // Create another bitmap that will hold the results of the filter. Bitmap blurredBitmap = bitmap.copy(bitmap.getConfig(), true); if (blurredBitmap != null) { // Create the Renderscript instance that will do the work. RenderScript rs = RenderScript.create(context); // Allocate memory for Renderscript to work with Allocation input = Allocation.createFromBitmap( rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SCRIPT); Allocation output = Allocation.createTyped(rs, input.getType()); // Load up an instance of the specific script that we want to use. ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setInput(input); script.setRadius(4); // Set the blur radius script.forEach(output); // Start the ScriptIntrinisicBlur output.copyTo(blurredBitmap); // Copy the output to the blurred bitmap } return blurredBitmap; }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (resultCode == RESULT_OK) { Uri imageFileUri = intent.getData(); try { BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); bmpFactoryOptions.inJustDecodeBounds = true; bmp = BitmapFactory.decodeStream( getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions); bmpFactoryOptions.inJustDecodeBounds = false; bmp = BitmapFactory.decodeStream( getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions); alteredBitmap = Bitmap.createBitmap(drawingArea.getWidth(), drawingArea.getHeight(), bmp.getConfig()); canvas = new Canvas(alteredBitmap); paint = new Paint(); paint.setColor(Color.GREEN); paint.setStrokeWidth(5); matrix = new Matrix(); canvas.drawBitmap(bmp, matrix, paint); drawingArea.cache = alteredBitmap; } catch (Exception e) { Log.v("ERROR", e.toString()); } } }
public static Bitmap drawTextToBitmap(Context mContext, Bitmap bitmap, Location location) { Resources resources = mContext.getResources(); float scale = resources.getDisplayMetrics().density; Bitmap.Config bitmapConfig = bitmap.getConfig(); if (bitmapConfig == null) { bitmapConfig = Bitmap.Config.ARGB_8888; } bitmap = bitmap.copy(bitmapConfig, true); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(mContext.getResources().getColor(R.color.white)); paint.setTextSize((int) 13); paint.setStyle(Paint.Style.FILL_AND_STROKE); Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG); paint1.setColor(mContext.getResources().getColor(R.color.green)); String text = getText(location); Paint.FontMetrics fm = new Paint.FontMetrics(); paint1.setTextAlign(Paint.Align.CENTER); paint1.getFontMetrics(fm); int width = bitmap.getWidth(); int height = bitmap.getHeight(); if (height > width) { canvas.drawRect(0, height - 40, height - 40, height, paint1); canvas.drawText(text, 5, height - 10, paint); } else { canvas.drawRect(0, 0, width, 40, paint1); canvas.drawText(text, 5, 25, paint); } Log.i(LOGTAG, "Check: Text drawn on bitmap"); return bitmap; }
/** * Returns a Texture2D object given an CGImageRef image If the image was not previously loaded, it * will create a new CCTexture2D object and it will return it. Otherwise it will return a * reference of a previously loaded image The "key" parameter will be used as the "key" for the * cache. If "key" is nil, then a new texture will be created each time. * * <p>BE AWARE OF the fact that copy of image is stored in memory, use assets method if you can. * * @since v0.8 */ public CCTexture2D addImage(Bitmap image, String key) { assert (image != null) : "TextureCache: image must not be null"; CCTexture2D tex = null; if (key != null && (tex = textures.get(key)) != null) { return tex; } final Bitmap copy = image.copy(image.getConfig(), false); if (copy != null) { final CCTexture2D texNew = new CCTexture2D(); texNew.setLoader( new GLResourceHelper.GLResourceLoader() { @Override public void load() { Bitmap initImage = copy.copy(copy.getConfig(), false); texNew.initWithImage(initImage); } }); if (key != null) { textures.put(key, texNew); } return texNew; } else { ccMacros.CCLOG("cocos2d", "Couldn't add Bitmap in CCTextureCache"); return null; } }
private void createSingleImageFromMultipleImages(Bitmap bitmap, int mCounter) { if (mCounter == 0) { try { this.mManyBitmapSuperposition = Bitmap.createBitmap( DeviceInfo.mScreenWidthForPortrait, DeviceInfo.mScreenHeightForPortrait, bitmap.getConfig()); this.mCanvas = new Canvas(this.mManyBitmapSuperposition); } catch (OutOfMemoryError error) { error.printStackTrace(); System.gc(); } finally { } } if (this.mCanvas != null) { int number = DeviceInfo.mScreenHeightForPortrait / 64; if (mCounter >= (mCounter / number) * number && mCounter < ((mCounter / number) + SNOW_BLOCK) * number) { this.mCanvas.drawBitmap( bitmap, (float) ((mCounter / number) * 64), (float) ((mCounter % number) * 64), null); } } }
public static LayerDrawable GetLayeredImg( Context c, String rid1, String product, String url, boolean spotters) { Bitmap bitmap; String cod_url; final String scale_type = "cod"; if (url.equals("")) { cod_url = GetCOD(rid1, product); bitmap = UtilityDownload.getBitmapFromURL(cod_url); } else { bitmap = UtilityDownload.getBitmapFromURL(url); } if (!MyApplication.black_bg) { bitmap = UtilityImg.BM_BlackToWhite(bitmap); } if ((!product.equals("OHA")) && (!product.equals("NVW"))) { android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig(); if (bitmapConfig == null) { bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; } bitmap = bitmap.copy(bitmapConfig, true); UtilityCanvasMain.AddCanvasItems(bitmap, scale_type, rid1, 0, spotters, false, 22); } return UtilityImg.BitmapToLayerDrawable(c, bitmap); }
@Test public void decodeResource_shouldSetDefaultBitmapConfig() throws Exception { Bitmap bitmap = BitmapFactory.decodeResource(Robolectric.application.getResources(), R.drawable.an_image); assertThat(bitmap.getConfig()).isEqualTo(Bitmap.Config.ARGB_8888); assertThat(bitmap.getRowBytes()).isNotZero(); }
/** * Get the white icon corresponding to a poiType. * * @param poiType the PoiType or null for notes. * @return The white icon. */ public Drawable getIconWhite(PoiType poiType) { Bitmap myBitmap = BitmapFactory.decodeResource( context.getResources(), poiType == null ? R.drawable.open_book : getIconDrawableId(poiType)); myBitmap = myBitmap.copy(myBitmap.getConfig(), true); int[] allpixels = new int[myBitmap.getHeight() * myBitmap.getWidth()]; myBitmap.getPixels( allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight()); for (int i = 0; i < myBitmap.getHeight() * myBitmap.getWidth(); i++) { if (allpixels[i] != 0) { int A = Color.alpha(allpixels[i]); // inverting byte for each R/G/B channel int R = 255 - Color.red(allpixels[i]); int G = 255 - Color.green(allpixels[i]); int B = 255 - Color.blue(allpixels[i]); // set newly-inverted pixel to output image allpixels[i] = Color.argb(A, R, G, B); } } myBitmap.setPixels( allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight()); return new BitmapDrawable(context.getResources(), myBitmap); }
public static Bitmap doColorFilter(Bitmap src, double red, double green, double blue) { // image size int width = src.getWidth(); int height = src.getHeight(); // create output bitmap Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig()); // color information int A, R, G, B; int pixel; // scan through all pixels for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { // get pixel color pixel = src.getPixel(x, y); // apply filtering on each channel R, G, B A = Color.alpha(pixel); R = (int) (Color.red(pixel) * red); G = (int) (Color.green(pixel) * green); B = (int) (Color.blue(pixel) * blue); // set new color pixel to output bitmap bmOut.setPixel(x, y, Color.argb(A, R, G, B)); } } // return final image return bmOut; }
public void writeToParcel(Parcel parcel, int i) { Object obj; Bitmap bitmap; byte abyte0[]; if (zzEo != null) { break MISSING_BLOCK_LABEL_100; } bitmap = zzaba; obj = ByteBuffer.allocate(bitmap.getRowBytes() * bitmap.getHeight()); bitmap.copyPixelsToBuffer(((java.nio.Buffer) (obj))); abyte0 = ((ByteBuffer) (obj)).array(); obj = new DataOutputStream(zznR()); ((DataOutputStream) (obj)).writeInt(abyte0.length); ((DataOutputStream) (obj)).writeInt(bitmap.getWidth()); ((DataOutputStream) (obj)).writeInt(bitmap.getHeight()); ((DataOutputStream) (obj)).writeUTF(bitmap.getConfig().toString()); ((DataOutputStream) (obj)).write(abyte0); zza(((Closeable) (obj))); com.google.android.gms.common.data.zza.zza(this, parcel, i | 1); zzEo = null; return; parcel; throw new IllegalStateException("Could not write into unlinked file", parcel); parcel; zza(((Closeable) (obj))); throw parcel; }
private static Config getConfig(Bitmap bitmap) { Config config = bitmap.getConfig(); if (config == null) { config = Config.ARGB_8888; } return config; }
/** Returns the in memory size of the given {@link Bitmap} in bytes. */ @TargetApi(Build.VERSION_CODES.KITKAT) public static int getBitmapByteSize(Bitmap bitmap) { // The return value of getAllocationByteCount silently changes for recycled bitmaps from the // internal buffer size to row bytes * height. To avoid random inconsistencies in caches, we // instead assert here. if (bitmap.isRecycled()) { throw new IllegalStateException( "Cannot obtain size for recycled Bitmap: " + bitmap + "[" + bitmap.getWidth() + "x" + bitmap.getHeight() + "] " + bitmap.getConfig()); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // Workaround for KitKat initial release NPE in Bitmap, fixed in MR1. See issue #148. try { return bitmap.getAllocationByteCount(); } catch (NullPointerException e) { // Do nothing. } } return bitmap.getHeight() * bitmap.getRowBytes(); }
public static Bitmap drawTextToBitmap(Context context, int resId, String text) { Resources resources = context.getResources(); Bitmap bitmap = BitmapFactory.decodeResource(resources, resId); Bitmap.Config bitmapConfig = bitmap.getConfig(); // set default bitmap config if none if (bitmapConfig == null) { bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; } // resource bitmaps are immutable, // so we need to convert it to mutable one bitmap = bitmap.copy(bitmapConfig, true); // new antialised Paint Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // text color paint.setColor(colors900[random.nextInt(colors900.length)]); // text size in pixels paint.setTextSize((int) (30 * resources.getDisplayMetrics().density)); // text shadow paint.setShadowLayer(1f, 0f, 1f, Color.WHITE); // draw text to the Canvas center Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); int x = (bitmap.getWidth() - bounds.width()) / 2; int y = (bitmap.getHeight() + bounds.height()) / 2; new Canvas(bitmap).drawText(text, x, y, paint); return bitmap; }