Java Code Examples for android.graphics.Bitmap#hasAlpha()

The following examples show how to use android.graphics.Bitmap#hasAlpha() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JPEGFactory.java    From PdfBox-Android with Apache License 2.0 6 votes vote down vote up
private static PDImageXObject createJPEG(PDDocument document, Bitmap image,
    float quality, int dpi) throws IOException
{
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, (int)(quality * 100), bos);
    byte[] bitmapData = bos.toByteArray();
    ByteArrayInputStream byteStream = new ByteArrayInputStream(bitmapData);

    PDImageXObject pdImage = new PDImageXObject(document, byteStream,
        COSName.DCT_DECODE, image.getWidth(), image.getHeight(),
        8,
        PDDeviceRGB.INSTANCE
    );

    // alpha -> soft mask
    if (image.hasAlpha())
    {
        PDImageXObject xAlpha = createAlphaFromARGBImage(document, image);

        pdImage.getCOSStream().setItem(COSName.SMASK, xAlpha);
    }
    return pdImage;
}
 
Example 2
Source File: BitmapEncoder.java    From giffun with Apache License 2.0 5 votes vote down vote up
private Bitmap.CompressFormat getFormat(Bitmap bitmap) {
    if (compressFormat != null) {
        return compressFormat;
    } else if (bitmap.hasAlpha()) {
        return Bitmap.CompressFormat.PNG;
    } else {
        return Bitmap.CompressFormat.JPEG;
    }
}
 
Example 3
Source File: EncryptedBitmapResourceEncoder.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private Bitmap.CompressFormat getFormat(Bitmap bitmap, Options options) {
  Bitmap.CompressFormat format = options.get(BitmapEncoder.COMPRESSION_FORMAT);

  if (format != null) {
    return format;
  } else if (bitmap.hasAlpha()) {
    return Bitmap.CompressFormat.PNG;
  } else {
    return Bitmap.CompressFormat.JPEG;
  }
}
 
Example 4
Source File: CompressCallable.java    From Flora with MIT License 5 votes vote down vote up
protected String compress(Bitmap bitmap, CompressSpec compressSpec) {
    int quality = compressSpec.options.quality;
    String outfile = null;
    float size = compressSpec.options.maxSize;

    if (quality < 0 || quality > 100)
        quality = CompressSpec.DEFAULT_QUALITY;

    if (bitmap.hasAlpha()) {
        outfile = FileUtil.generateCompressOutfileFormatPNG(compressSpec.dir, null).getAbsolutePath();
    } else {
        outfile = FileUtil.generateCompressOutfileFormatJPEG(compressSpec.dir, null).getAbsolutePath();
    }

    boolean isSuccess = compress(bitmap, outfile, quality);

    if (size > 0 && isSuccess) {
        float outfileSize = (float) FileUtil.getSizeInBytes(outfile) / (float) 1024;
        while (outfileSize > size) {
            if (quality <= 25)
                break;
            quality -= 5;
            isSuccess = compress(bitmap, outfile, quality);
            if (!isSuccess)
                break;
            outfileSize = (float) FileUtil.getSizeInBytes(outfile) / (float) 1024;
        }
    }

    Logger.i("final compress quality: " + quality);

    if (bitmap != mT) {
        bitmap.recycle();
        bitmap = null;
        System.gc();
        System.runFinalization();
    }

    return outfile;
}
 
Example 5
Source File: CompressCallable.java    From Flora with MIT License 5 votes vote down vote up
protected boolean compress(Bitmap bitmap, String outfile, int quality) {
    if (bitmap == null || bitmap.isRecycled())
        return false;

    if (bitmap.hasAlpha()) {
        return compress(bitmap, outfile, quality, Bitmap.CompressFormat.PNG);
    } else {
        return compress(bitmap, outfile, quality, Bitmap.CompressFormat.JPEG);
    }
}
 
Example 6
Source File: XulWorker.java    From starcor.xul with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static Bitmap toRoundCornerBitmap(Canvas canvas, Paint paintClear, Bitmap srcBitmap, float[] roundRadius) {
	if (srcBitmap.isMutable() && srcBitmap.hasAlpha()) {
		return toRoundCornerMutableBitmap(canvas, paintClear, srcBitmap, roundRadius);
	}
	Bitmap output;
	if (false) {
		output = BitmapTools.createBitmapFromRecycledBitmaps(srcBitmap.getWidth(), srcBitmap.getHeight(), XulManager.DEF_PIXEL_FMT);
	} else {
		output = BitmapTools.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), XulManager.DEF_PIXEL_FMT);
	}
	canvas.setBitmap(output);
	Rect rect = new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
	canvas.drawBitmap(srcBitmap, rect, rect, null);
	return toRoundCornerMutableBitmap(canvas, paintClear, output, roundRadius);
}
 
Example 7
Source File: RainbowImage.java    From rainbow with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Construct a new PImage from an Android normalBitmap. The pixels[] array is not
 * initialized, nor is data copied to it, until loadPixels() is called.
 */
public RainbowImage(Bitmap bitmap) {
    this.bitmap = bitmap;
    this.width = bitmap.getWidth();
    this.height = bitmap.getHeight();
    this.pixels = null;
    this.format = bitmap.hasAlpha() ? ARGB : RGB;
}
 
Example 8
Source File: JPEGFactory.java    From PdfBox-Android with Apache License 2.0 5 votes vote down vote up
private static Bitmap getAlphaImage(Bitmap image) throws IOException
{
    if (!image.hasAlpha())
    {
        return null;
    }
    return image.extractAlpha();
}
 
Example 9
Source File: RadioPlayerNotification.java    From monkeyboard-radio-android with GNU General Public License v3.0 5 votes vote down vote up
private int getDominantColor(Bitmap bitmap) {
    if (null == bitmap) return Color.TRANSPARENT;

    int redBucket = 0;
    int greenBucket = 0;
    int blueBucket = 0;
    int alphaBucket = 0;

    boolean hasAlpha = bitmap.hasAlpha();
    int pixelCount = bitmap.getWidth() * bitmap.getHeight();
    int[] pixels = new int[pixelCount];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

    for (int y = 0, h = bitmap.getHeight(); y < h; y++)
    {
        for (int x = 0, w = bitmap.getWidth(); x < w; x++)
        {
            int color = pixels[x + y * w]; // x + y * width
            redBucket += (color >> 16) & 0xFF; // Color.red
            greenBucket += (color >> 8) & 0xFF; // Color.greed
            blueBucket += (color & 0xFF); // Color.blue
            if (hasAlpha) alphaBucket += (color >>> 24); // Color.alpha
        }
    }

    return Color.argb(
            (hasAlpha) ? (alphaBucket / pixelCount) : 255,
            redBucket / pixelCount,
            greenBucket / pixelCount,
            blueBucket / pixelCount);
}
 
Example 10
Source File: PageView.java    From mupdf-android with GNU Affero General Public License v3.0 5 votes vote down vote up
public boolean isBitmapRecycled(Bitmap bitmap) {
    if (android.os.Build.VERSION.SDK_INT < 17) {
        return bitmap.isRecycled();
    } else {
        return bitmap.isRecycled() || (!bitmap.isPremultiplied() && bitmap.getConfig() == Bitmap.Config.ARGB_8888 && bitmap.hasAlpha());
    }
}
 
Example 11
Source File: RainbowImage.java    From rainbow with GNU Lesser General Public License v3.0 5 votes vote down vote up
public RainbowImage(RainbowImage rainbowImage) {
    Bitmap bitmap = Bitmap.createBitmap(rainbowImage.getBitmap(), 0, 0, rainbowImage.width, rainbowImage.height);
    this.bitmap = bitmap;
    this.width = bitmap.getWidth();
    this.height = bitmap.getHeight();
    this.pixels = null;
    this.format = bitmap.hasAlpha() ? ARGB : RGB;
}
 
Example 12
Source File: RoundedBitmapDrawable.java    From adt-leanback-support with Apache License 2.0 5 votes vote down vote up
@Override
public int getOpacity() {
    if (mGravity != Gravity.FILL) {
        return PixelFormat.TRANSLUCENT;
    }
    Bitmap bm = mBitmap;
    return (bm == null
            || bm.hasAlpha()
            || mPaint.getAlpha() < 255
            || isGreaterThanZero(mCornerRadius))
            ? PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE;
}
 
Example 13
Source File: ColorHelper.java    From Noyze with Apache License 2.0 5 votes vote down vote up
public static int getDominantColor(Bitmap bitmap) {
    if (null == bitmap) return Color.TRANSPARENT;

    int redBucket = 0;
    int greenBucket = 0;
    int blueBucket = 0;
    int alphaBucket = 0;

    boolean hasAlpha = bitmap.hasAlpha();
    int pixelCount = bitmap.getWidth() * bitmap.getHeight();
    int[] pixels = new int[pixelCount];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

    for (int y = 0, h = bitmap.getHeight(); y < h; y++)
    {
        for (int x = 0, w = bitmap.getWidth(); x < w; x++)
        {
            int color = pixels[x + y * w]; // x + y * barHeight
            redBucket += (color >> 16) & 0xFF; // Color.red
            greenBucket += (color >> 8) & 0xFF; // Color.greed
            blueBucket += (color & 0xFF); // Color.blue
            if (hasAlpha) alphaBucket += (color >>> 24); // Color.alpha
        }
    }

    return Color.argb(
        (hasAlpha) ? (alphaBucket / pixelCount) : 255,
        redBucket / pixelCount,
        greenBucket / pixelCount,
        blueBucket / pixelCount);
}
 
Example 14
Source File: JPEGFactory.java    From PdfBox-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a grayscale Flate encoded PDImageXObject from the alpha channel
 * of an image.
 *
 * @param document the document where the image will be created.
 * @param image an ARGB image.
 *
 * @return the alpha channel of an image as a grayscale image.
 *
 * @throws IOException if something goes wrong
 */
private static PDImageXObject createAlphaFromARGBImage(PDDocument document, Bitmap image)
    throws IOException
{
    // this implementation makes the assumption that the raster uses
    // SinglePixelPackedSampleModel, i.e. the values can be used 1:1 for
    // the stream.
    // Sadly the type of the databuffer is TYPE_INT and not TYPE_BYTE.
    if (!image.hasAlpha())
    {
        return null;
    }

    int[] pixels = new int[image.getHeight() * image.getWidth()];
    image.getPixels(pixels, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int bpc;
    //        if (image.getTransparency() == Transparency.BITMASK)
    //        {
    //            bpc = 1;
    //            MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos);
    //            int width = alphaRaster.getSampleModel().getWidth();
    //            int p = 0;
    //            for (int pixel : pixels)
    //            {
    //                mcios.writeBit(pixel);
    //                ++p;
    //                if (p % width == 0)
    //                {
    //                    while (mcios.getBitOffset() != 0)
    //                    {
    //                        mcios.writeBit(0);
    //                    }
    //                }
    //            }
    //            mcios.flush();
    //            mcios.close();
    //        }
    //        else
    //        {
    bpc = 8;
    for (int pixel : pixels)
    {
        bos.write(Color.alpha(pixel));
    }
    //        }

    PDImageXObject pdImage = prepareImageXObject(document, bos.toByteArray(),
        image.getWidth(), image.getHeight(), bpc, PDDeviceGray.INSTANCE);
    return pdImage;
}
 
Example 15
Source File: LosslessFactory.java    From PdfBox-Android with Apache License 2.0 4 votes vote down vote up
/**
     * Creates a grayscale Flate encoded PDImageXObject from the alpha channel
     * of an image.
     *
     * @param document the document where the image will be created.
     * @param image an ARGB image.
     *
     * @return the alpha channel of an image as a grayscale image.
     *
     * @throws IOException if something goes wrong
     */
    private static PDImageXObject createAlphaFromARGBImage(PDDocument document, Bitmap image)
            throws IOException
    {
        // this implementation makes the assumption that the raster uses 
        // SinglePixelPackedSampleModel, i.e. the values can be used 1:1 for
        // the stream. 
        // Sadly the type of the databuffer is TYPE_INT and not TYPE_BYTE.
        if (!image.hasAlpha())
        {
            return null;
        }
        
        // extract the alpha information
//        WritableRaster alphaRaster = image.getAlphaRaster();
//        if (alphaRaster == null)
//        {
//            // happens sometimes (PDFBOX-2654) despite colormodel claiming to have alpha
//            return createAlphaFromARGBImage2(document, image);
//        }

//        int[] pixels = alphaRaster.getPixels(0, 0,
//                alphaRaster.getSampleModel().getWidth(),
//                alphaRaster.getSampleModel().getHeight(),
//                (int[]) null);
        int[] pixels = new int[image.getHeight() * image.getWidth()];
        image.getPixels(pixels, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
        
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int bpc;
//        if (image.getTransparency() == Transparency.BITMASK)
//        {
//            bpc = 1;
//            MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos);
//            int width = alphaRaster.getSampleModel().getWidth();
//            int p = 0;
//            for (int pixel : pixels)
//            {
//                mcios.writeBit(pixel);
//                ++p;
//                if (p % width == 0)
//                {
//                    while (mcios.getBitOffset() != 0)
//                    {
//                        mcios.writeBit(0);
//                    }
//                }
//            }
//            mcios.flush();
//            mcios.close();
//        }
//        else
//        {
            bpc = 8;
            for (int pixel : pixels)
            {
                bos.write(Color.alpha(pixel));
            }
//        }

        PDImageXObject pdImage = prepareImageXObject(document, bos.toByteArray(), 
                image.getWidth(), image.getHeight(), bpc, PDDeviceGray.INSTANCE);
        return pdImage;
    }
 
Example 16
Source File: AttachmentView.java    From Dashchan with Apache License 2.0 4 votes vote down vote up
@Override
public void draw(Canvas canvas) {
	Interpolator interpolator = AnimationUtils.DECELERATE_INTERPOLATOR;
	canvas.drawColor(backgroundColor);
	Bitmap bitmap = this.bitmap != null && !this.bitmap.isRecycled() ? this.bitmap : null;
	boolean hasImage = bitmap != null;
	int vw = getWidth(), vh = getHeight();
	int dt = imageApplyTime > 0L ? (int) (System.currentTimeMillis() - imageApplyTime) : Integer.MAX_VALUE;
	float alpha = interpolator.getInterpolation(Math.min(dt / 200f, 1f));
	boolean invalidate = false;
	if (hasImage) {
		int bw = bitmap.getWidth(), bh = bitmap.getHeight();
		float scale, dx, dy;
		if (bw * vh > vw * bh ^ !cropEnabled) {
			scale = (float) vh / (float) bh;
			dx = (int) ((vw - bw * scale) * 0.5f + 0.5f);
			dy = 0f;
		} else {
			scale = (float) vw / (float) bw;
			dx = 0f;
			dy = (int) ((vh - bh * scale) * 0.5f + 0.5f);
		}
		source.set(0, 0, bw, bh);
		destination.set(dx, dy, dx + bw * scale, dy + bh * scale);
		if (bitmap.hasAlpha()) {
			int left = Math.max((int) (destination.left + 0.5f), 0);
			int top = Math.max((int) (destination.top + 0.5f), 0);
			int right = Math.min((int) (destination.right + 0.5f), vw);
			int bottom = Math.min((int) (destination.bottom + 0.5f), vh);
			if (tileDrawable == null) {
				tileDrawable = new TransparentTileDrawable(getContext(), false);
			}
			tileDrawable.setBounds(left, top, right, bottom);
			tileDrawable.draw(canvas);
		}
		bitmapPaint.setAlpha((int) (0xff * alpha));
		if (C.API_LOLLIPOP) {
			float contrast = interpolator.getInterpolation(Math.min(dt / 300f, 1f));
			float saturation = interpolator.getInterpolation(Math.min(dt / 400f, 1f));
			if (saturation < 1f) {
				float[] matrix = workColorMatrix;
				float contrastGain = 1f + 2f * (1f -  contrast);
				float contrastExtra = (1f - contrastGain) * 255f;
				matrix[0] = matrix[6] = matrix[12] = contrastGain;
				matrix[4] = matrix[9] = matrix[14] = contrastExtra;
				colorMatrix2.set(matrix);
				colorMatrix1.setSaturation(saturation);
				colorMatrix1.postConcat(colorMatrix2);
				bitmapPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix1));
			} else {
				bitmapPaint.setColorFilter(null);
			}
			invalidate = saturation < 1f;
		} else {
			invalidate = alpha < 1f;
		}
		canvas.drawBitmap(bitmap, source, destination, bitmapPaint);
	}
	if (sfwMode) {
		canvas.drawColor(backgroundColor & 0x00ffffff | 0xe0000000);
	}
	Drawable overlayDrawable = this.overlayDrawable;
	if (error && !keepOverlayOnError) {
		if (errorOverlayDrawable == null) {
			errorOverlayDrawable = ResourceUtils.getDrawable(getContext(), R.attr.attachmentWarning, 0);
		}
		overlayDrawable = errorOverlayDrawable;
	}
	if (overlayDrawable != null) {
		if (hasImage && !sfwMode) {
			canvas.drawColor((int) (0x66 * alpha) << 24);
		}
		int dw = overlayDrawable.getIntrinsicWidth(), dh = overlayDrawable.getIntrinsicHeight();
		int left = (vw - dw) / 2, top = (vh - dh) / 2, right = left + dw, bottom = top + dh;
		overlayDrawable.setBounds(left, top, right, bottom);
		overlayDrawable.draw(canvas);
	}
	if (drawTouching) {
		super.draw(canvas);
	}
	if (cornersDrawable != null) {
		cornersDrawable.draw(canvas);
	}
	if (invalidate) {
		invalidate();
	}
}
 
Example 17
Source File: BitmapRef.java    From document-viewer with GNU General Public License v3.0 4 votes vote down vote up
BitmapRef(final Bitmap bitmap, final long generation) {
    super(bitmap.getConfig(), bitmap.hasAlpha(), bitmap.getWidth(), bitmap.getHeight(), generation);
    this.bitmap = bitmap;
}
 
Example 18
Source File: ThumbnailLoaderDrawable.java    From libcommon with Apache License 2.0 4 votes vote down vote up
@Override
public int getOpacity() {
       final Bitmap bm = mBitmap;
       return (bm == null || bm.hasAlpha() || mPaint.getAlpha() < 255) ?
               PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE;
}
 
Example 19
Source File: LoaderDrawable.java    From libcommon with Apache License 2.0 4 votes vote down vote up
@Override
public int getOpacity() {
       final Bitmap bm = mBitmap;
       return (bm == null || bm.hasAlpha() || mPaint.getAlpha() < 255) ?
               PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE;
}
 
Example 20
Source File: GlideBitmapDrawable.java    From giffun with Apache License 2.0 4 votes vote down vote up
@Override
public int getOpacity() {
    Bitmap bm = state.bitmap;
    return bm == null || bm.hasAlpha() || state.paint.getAlpha() < 255
            ? PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE;
}