Java Code Examples for com.caverock.androidsvg.SVG#getFromInputStream()

The following examples show how to use com.caverock.androidsvg.SVG#getFromInputStream() . 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: SvgMediaDecoder.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@NonNull
@Override
public Drawable decode(@Nullable String contentType, @NonNull InputStream inputStream) {

    final SVG svg;
    try {
        svg = SVG.getFromInputStream(inputStream);
    } catch (SVGParseException e) {
        throw new IllegalStateException("Exception decoding SVG", e);
    }

    final float w = svg.getDocumentWidth();
    final float h = svg.getDocumentHeight();
    final float density = resources.getDisplayMetrics().density;

    final int width = (int) (w * density + .5F);
    final int height = (int) (h * density + .5F);

    final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
    final Canvas canvas = new Canvas(bitmap);
    canvas.scale(density, density);
    svg.renderToCanvas(canvas);

    return new BitmapDrawable(resources, bitmap);
}
 
Example 2
Source File: AndroidSvgBitmap.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
public static android.graphics.Bitmap getResourceBitmap(InputStream inputStream, float scaleFactor, float defaultSize, int width, int height, int percent, int color) throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(inputStream);
        Picture picture;
        if (color != 0) {
            RenderOptions renderOpts = RenderOptions.create().css("* { fill: #" + String.format("%06x", color & 0x00ffffff) + "; }");
            picture = svg.renderToPicture(renderOpts);
        } else {
            picture = svg.renderToPicture();
        }

        double scale = scaleFactor / Math.sqrt((picture.getHeight() * picture.getWidth()) / defaultSize);

        float[] bmpSize = GraphicUtils.imageSize(picture.getWidth(), picture.getHeight(), (float) scale, width, height, percent);

        android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bmpSize[0]),
                (int) Math.ceil(bmpSize[1]), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPicture(picture, new RectF(0, 0, bmpSize[0], bmpSize[1]));

        return bitmap;
    } catch (Exception e) {
        throw new IOException(e);
    }
}
 
Example 3
Source File: SvgDecoder.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
public Resource<SVG> decode(InputStream source, int width, int height) throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(source);
        return new SimpleResource<SVG>(svg);
    } catch (SVGParseException ex) {
        throw new IOException("Cannot load SVG from stream", ex);
    }
}
 
Example 4
Source File: SvgDecoder.java    From GlideToVectorYou with Apache License 2.0 5 votes vote down vote up
public Resource<SVG> decode(@NonNull InputStream source, int width, int height,
                            @NonNull Options options)
    throws IOException {
  try {
    SVG svg = SVG.getFromInputStream(source);
    return new SimpleResource<>(svg);
  } catch (SVGParseException ex) {
    throw new IOException("Cannot load SVG from stream", ex);
  }
}
 
Example 5
Source File: SvgUtils.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
public static SVG getSvg(@NonNull File file) throws SVGParseException, IOException {
    if (!file.exists()) {
        throw new FileNotFoundException("File: '" + file.getAbsolutePath() + "' not exists");
    }

    try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
        return SVG.getFromInputStream(is);
    }
}
 
Example 6
Source File: SvgUtils.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
public static SVG getSvg(@NonNull FileDescriptor descriptor)
        throws SVGParseException, IOException {

    try (InputStream is = new BufferedInputStream(new FileInputStream(descriptor))) {
        return SVG.getFromInputStream(is);
    }
}
 
Example 7
Source File: ByteBufferSvgDecoder.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
@Override
SVG loadSvg(@NonNull ByteBuffer source, int width, int height, @NonNull Options options) throws SvgParseException {
    try (InputStream is = ByteBufferUtil.toStream(source)) {
        return SVG.getFromInputStream(is);
    } catch (IOException | SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
Example 8
Source File: InputStreamSvgDecoder.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
@Override
SVG loadSvg(@NonNull InputStream source, int width, int height, @NonNull Options options) throws SvgParseException {
    try {
        return SVG.getFromInputStream(source);
    } catch (SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
Example 9
Source File: SVGMediaMetadataRetriever.java    From MediaMetadataRetrieverCompat with MIT License 5 votes vote down vote up
@Override
public void setDataSource(@NonNull DataSource source) throws IOException {
    try {
        mSVG = SVG.getFromInputStream(source.toStream());
    } catch (SVGParseException e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: SvgPictureMediaDecoder.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public Drawable decode(@Nullable String contentType, @NonNull InputStream inputStream) {

    final SVG svg;
    try {
        svg = SVG.getFromInputStream(inputStream);
    } catch (SVGParseException e) {
        throw new IllegalStateException("Exception decoding SVG", e);
    }

    final Picture picture = svg.renderToPicture();
    return new PictureDrawable(picture);
}
 
Example 11
Source File: SvgDecoder.java    From incubator-taverna-mobile with Apache License 2.0 5 votes vote down vote up
public Resource<SVG> decode(InputStream source, int width, int height) throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(source);
        return new SimpleResource<SVG>(svg);
    } catch (SVGParseException ex) {
        throw new IOException("Cannot load SVG from stream", ex);
    }
}
 
Example 12
Source File: SvgDecoderExample.java    From fresco with MIT License 5 votes vote down vote up
@Override
public CloseableImage decode(
    EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  try {
    SVG svg = SVG.getFromInputStream(encodedImage.getInputStream());
    return new CloseableSvgImage(svg);
  } catch (SVGParseException e) {
    e.printStackTrace();
  }
  return null;
}
 
Example 13
Source File: XulSVGDrawable.java    From starcor.xul with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static XulDrawable buildSVGDrawable(InputStream stream, String url, String imageKey, int width, int height) {
	if (stream == null) {
		return null;
	}

	XulSVGDrawable drawable = new XulSVGDrawable();

	try {
		drawable._svg = SVG.getFromInputStream(stream);
	} catch (SVGParseException e) {
		e.printStackTrace();
		return null;
	}

	float offsetX = 0;
	float offsetY = 0;

	float xScalar = 1.0f;
	float yScalar = 1.0f;

	RectF documentViewBox = drawable._svg.getDocumentViewBox();
	if (documentViewBox == null) {
		float documentWidth = drawable._svg.getDocumentWidth();
		float documentHeight = drawable._svg.getDocumentHeight();
		if (documentWidth <= 0 && documentHeight <= 0) {
			documentWidth = 256;
			documentHeight = 256;
			drawable._svg.setDocumentWidth(documentWidth);
			drawable._svg.setDocumentHeight(documentHeight);
		}
		drawable._svg.setDocumentViewBox(0, 0, documentWidth, documentHeight);
		documentViewBox = drawable._svg.getDocumentViewBox();
	}

	int docWidth = XulUtils.roundToInt(documentViewBox.width());
	int docHeight = XulUtils.roundToInt(documentViewBox.height());
	if (width == 0 && height == 0) {
		width = docWidth;
		height = docHeight;
	} else if (width == 0) {
		xScalar = yScalar = (float)(height)/docHeight;
		width = XulUtils.roundToInt(docWidth*xScalar);
		docWidth = width;
		docHeight = height;
	} else if (height == 0) {
		xScalar = yScalar = (float)(width)/docWidth;
		height = XulUtils.roundToInt(docHeight*xScalar);
		docWidth = width;
		docHeight = height;
	} else {
		float wScalar = (float) width / docWidth;
		float hScalar = (float) height / docHeight;
		if (wScalar > hScalar) {
			docWidth *= wScalar;
			docHeight *= wScalar;
			xScalar = yScalar = wScalar;
		} else {
			docWidth *= hScalar;
			docHeight *= hScalar;
			xScalar = yScalar = hScalar;
		}
		offsetX = (docWidth - width) / 2.0f;
		offsetY = (docHeight - height) / 2.0f;
	}

	XulUtils.ticketMarker ticketMarker = new XulUtils.ticketMarker("BENCH!!!", true);
	ticketMarker.mark();

	drawable._cachedBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
	Canvas c = new Canvas(drawable._cachedBmp);
	c.translate(-offsetX, -offsetY);
	c.scale(xScalar, yScalar);
	drawable._svg.renderToCanvas(c, documentViewBox);

	ticketMarker.mark("svg");
	Log.d("BENCH", ticketMarker.toString());

	drawable._url = url;
	drawable._key = imageKey;
	return drawable;
}