Example usage for android.graphics Picture draw

List of usage examples for android.graphics Picture draw

Introduction

In this page you can find the example usage for android.graphics Picture draw.

Prototype

public void draw(Canvas canvas) 

Source Link

Document

Draw this picture on the canvas.

Usage

From source file:Main.java

public static Bitmap pictureDrawable2Bitmap(Picture picture) {
    Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    picture.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap captureWebView(WebView webView) {
    Picture snapShot = webView.capturePicture();
    Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(), snapShot.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);
    snapShot.draw(canvas);
    return bmp;//from  w  w w. ja  va 2  s .  c  o m
}

From source file:Main.java

/**
 * @brief      Converting picture to bitmap array.
 * /* w ww .  ja va  2 s .co m*/
 * @author     daiping.zhao
 * @param      bm   [IN]  bitmap object
 * 
 * @return     Return converted bytes array
 */
public static Bitmap pictureToBitmap(Picture picture, int width, int height) {
    Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(b);

    // May need to tweak these values to determine what is the
    // best scale factor
    int picWidth = picture.getWidth();
    int picHeight = picture.getHeight();
    float scaleFactorX = 1.0f;
    float scaleFactorY = 1.0f;
    if (picWidth > 0) {
        scaleFactorX = (float) width / (float) picWidth;
    } else {
        return null;
    }

    if (picWidth > picHeight) {
        // If the device is in landscape and the page is shorter
        // than the height of the view, stretch the thumbnail to fill the
        // space.
        scaleFactorY = (float) height / (float) picHeight;
    } else {
        // In the portrait case, this looks nice.
        scaleFactorY = scaleFactorX;
    }

    canvas.scale(scaleFactorX, scaleFactorY);

    picture.draw(canvas);
    return b;
}

From source file:nirwan.cordova.plugin.printer.Printer.java

private Bitmap takeScreenshot(WebView page) {
    Picture picture = page.capturePicture();
    Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    picture.draw(canvas);

    return bitmap;
}

From source file:com.google.appinventor.components.runtime.util.NativeOpenStreetMapController.java

private Drawable rasterizeSVG(MapMarker aiMarker, SVG markerSvg) {
    SVG.Svg svg = markerSvg.getRootElement();
    final float density = view.getContext().getResources().getDisplayMetrics().density;
    float height = aiMarker.Height() <= 0 ? getBestGuessHeight(svg) : aiMarker.Height();
    float width = aiMarker.Width() <= 0 ? getBestGuessWidth(svg) : aiMarker.Width();
    float scaleH = height / getBestGuessHeight(svg);
    float scaleW = width / getBestGuessWidth(svg);
    float scale = (float) Math.sqrt(scaleH * scaleH + scaleW * scaleW);

    // update fill color of SVG <path>
    Paint fillPaint = new Paint();
    Paint strokePaint = new Paint();
    PaintUtil.changePaint(fillPaint, aiMarker.FillColor());
    PaintUtil.changePaint(strokePaint, aiMarker.StrokeColor());
    SVG.Length strokeWidth = new SVG.Length(aiMarker.StrokeWidth() / scale);
    for (SVG.SvgObject element : svg.getChildren()) {
        if (element instanceof SVG.SvgConditionalElement) {
            SVG.SvgConditionalElement path = (SVG.SvgConditionalElement) element;
            path.baseStyle.fill = new SVG.Colour(fillPaint.getColor());
            path.baseStyle.fillOpacity = fillPaint.getAlpha() / 255.0f;
            path.baseStyle.stroke = new SVG.Colour(strokePaint.getColor());
            path.baseStyle.strokeOpacity = strokePaint.getAlpha() / 255.0f;
            path.baseStyle.strokeWidth = strokeWidth;
            if (path.style != null) {
                if ((path.style.specifiedFlags & SPECIFIED_FILL) == 0) {
                    path.style.fill = new SVG.Colour(fillPaint.getColor());
                    path.style.specifiedFlags |= SPECIFIED_FILL;
                }/*w  w w  . ja  v a2 s  .c  o m*/
                if ((path.style.specifiedFlags & SPECIFIED_FILL_OPACITY) == 0) {
                    path.style.fillOpacity = fillPaint.getAlpha() / 255.0f;
                    path.style.specifiedFlags |= SPECIFIED_FILL_OPACITY;
                }
                if ((path.style.specifiedFlags & SPECIFIED_STROKE) == 0) {
                    path.style.stroke = new SVG.Colour(strokePaint.getColor());
                    path.style.specifiedFlags |= SPECIFIED_STROKE;
                }
                if ((path.style.specifiedFlags & SPECIFIED_STROKE_OPACITY) == 0) {
                    path.style.strokeOpacity = strokePaint.getAlpha() / 255.0f;
                    path.style.specifiedFlags |= SPECIFIED_STROKE_OPACITY;
                }
                if ((path.style.specifiedFlags & SPECIFIED_STROKE_WIDTH) == 0) {
                    path.style.strokeWidth = strokeWidth;
                    path.style.specifiedFlags |= SPECIFIED_STROKE_WIDTH;
                }
            }
        }
    }

    // draw SVG to Picture and create a BitmapDrawable for rendering
    Picture picture = markerSvg.renderToPicture();
    Picture scaledPicture = new Picture();
    Canvas canvas = scaledPicture.beginRecording((int) ((width + 2.0f * aiMarker.StrokeWidth()) * density),
            (int) ((height + 2.0f * aiMarker.StrokeWidth()) * density));
    canvas.scale(density * scaleW, density * scaleH);
    canvas.translate(strokeWidth.floatValue(), strokeWidth.floatValue());
    picture.draw(canvas);
    scaledPicture.endRecording();
    return new PictureDrawable(scaledPicture);
}

From source file:android.webkit.cts.WebViewTest.java

private boolean isPictureFilledWithColor(Picture picture, int color) {
    if (picture.getWidth() == 0 || picture.getHeight() == 0)
        return false;

    Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Config.ARGB_8888);
    picture.draw(new Canvas(bitmap));

    for (int i = 0; i < bitmap.getWidth(); i++) {
        for (int j = 0; j < bitmap.getHeight(); j++) {
            if (color != bitmap.getPixel(i, j)) {
                return false;
            }/*from  w ww. j a  v a  2 s .  c  o  m*/
        }
    }
    return true;
}