org.oscim.backend.canvas.Bitmap Java Examples

The following examples show how to use org.oscim.backend.canvas.Bitmap. 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: MBTilesTileDataSource.java    From geopaparazzi with GNU General Public License v3.0 8 votes vote down vote up
@Override
public void query(MapTile tile, ITileDataSink sink) {
    QueryResult res = FAILED;

    try {
        byte[] imageBytes = db.getTile(tile.tileX, tile.tileY, tile.zoomLevel);
        if (transparentColor != null || alpha != null) {
            android.graphics.Bitmap bmp = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
            if (transparentColor != null) {
                bmp = ImageUtilities.makeTransparent(bmp, transparentColor);
            }
            if (alpha != null) {
                bmp = ImageUtilities.makeBitmapTransparent(bmp, alpha);
            }
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bmp.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, bos);
            imageBytes = bos.toByteArray();
        }

        Bitmap bitmap = AndroidGraphics.decodeBitmap(new ByteArrayInputStream(imageBytes));

        sink.setTileImage(bitmap);
        res = QueryResult.SUCCESS;
    } catch (Exception e) {
        log.debug("{} Error: {}", tile, e.getMessage());
    } finally {
        sink.completed(res);
    }
}
 
Example #2
Source File: Utils.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Load a texture from a specified location and optional dimensions.
 */
public static TextureItem loadTexture(String relativePathPrefix, String src, int width, int height, int percent, int color) {
    if (src == null || src.length() == 0)
        return null;

    try {
        Bitmap bitmap = CanvasAdapter.getBitmapAsset(relativePathPrefix, src, width, height, percent, color);
        if (bitmap != null) {
            log.debug("loading {}", src);
            return new TextureItem(potBitmap(bitmap), true);
        }
    } catch (Exception e) {
        log.error("{}: missing file / {}", src, e.getMessage());
    }
    return null;
}
 
Example #3
Source File: AndroidGraphics.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return new AndroidBitmap(((BitmapDrawable) drawable).getBitmap());
    }

    android.graphics.Bitmap bitmap = android.graphics.Bitmap
            .createBitmap(drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight(),
                    Config.ARGB_8888);

    android.graphics.Canvas canvas = new android.graphics.Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return new AndroidBitmap(bitmap);
}
 
Example #4
Source File: BookmarkLayer.java    From geopaparazzi with GNU General Public License v3.0 6 votes vote down vote up
private MarkerSymbol createAdvancedSymbol(MarkerItem item, Bitmap poiBitmap) {
    int bitmapHeight = poiBitmap.getHeight();
    int margin = 3;
    int dist2symbol = (int) Math.round(bitmapHeight / 2.0);

    int symbolWidth = poiBitmap.getWidth();

    int xSize = symbolWidth;
    int ySize = symbolWidth + dist2symbol;

    // markerCanvas, the drawing area for all: title, description and symbol
    Bitmap markerBitmap = CanvasAdapter.newBitmap(xSize, ySize, 0);
    org.oscim.backend.canvas.Canvas markerCanvas = CanvasAdapter.newCanvas();
    markerCanvas.setBitmap(markerBitmap);

    markerCanvas.drawBitmap(poiBitmap, xSize * 0.5f - (symbolWidth * 0.25f), ySize * 0.5f - (symbolWidth * 0.25f));

    return (new MarkerSymbol(markerBitmap, MarkerSymbol.HotspotPlace.CENTER, true));
}
 
Example #5
Source File: BitmapPacker.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
public synchronized Rect add(Object key, Bitmap image) {
    Rect rect = new Rect(0, 0, image.getWidth(), image.getHeight());
    if (rect.width > atlasWidth || rect.height > atlasHeight) {
        if (key == null)
            throw new RuntimeException("PackerAtlasItem size too small for Bitmap.");
        throw new RuntimeException("PackerAtlasItem size too small for Bitmap: " + key);
    }

    PackerAtlasItem packerAtlasItem = packStrategy.pack(this, key, rect);
    if (key != null) {
        packerAtlasItem.rects.put(key, rect);
        packerAtlasItem.addedRects.add(key);
    }

    int rectX = rect.x, rectY = rect.y, rectWidth = rect.width, rectHeight = rect.height;

    packerAtlasItem.drawBitmap(image, rectX,
            flipY ? packerAtlasItem.image.getHeight() - rectY - rectHeight : rectY);

    return rect;
}
 
Example #6
Source File: BitmapBucket.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
public void setBitmap(Bitmap bitmap, int w, int h, TexturePool pool) {

        mWidth = w;
        mHeight = h;

        mBitmap = bitmap;
        if (textures == null) {
            if (pool == null)
                textures = new TextureItem(mBitmap);
            else {
                textures = pool.get(mBitmap);
            }
        }

        TextureItem t = textures;
        t.indices = TextureBucket.INDICES_PER_SPRITE;
    }
 
Example #7
Source File: XmlMapsforgeThemeBuilder.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
private void createAtlas(String elementName, Attributes attributes) throws IOException {
    String img = null;

    for (int i = 0; i < attributes.getLength(); i++) {
        String name = attributes.getLocalName(i);
        String value = attributes.getValue(i);

        if ("img".equals(name)) {
            img = value;
        } else {
            logUnknownAttribute(elementName, name, value, i);
        }
    }
    validateExists("img", img, elementName);

    Bitmap bitmap = CanvasAdapter.getBitmapAsset(mTheme.getRelativePathPrefix(), img);
    if (bitmap != null)
        mTextureAtlas = new TextureAtlas(bitmap);
}
 
Example #8
Source File: SymbolStyle.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
private SymbolStyle(Bitmap bitmap, TextureRegion texture, int hash) {
    this.bitmap = bitmap;
    this.texture = texture;
    this.hash = hash;
    this.src = null;

    this.symbolWidth = 0;
    this.symbolHeight = 0;
    this.symbolPercent = 100;
    this.symbolColor = 0;

    this.billboard = false;
    this.rotate = false;
    this.inverse = false;
    this.mandatory = false;

    this.repeat = false;
    this.repeatStart = 0;
    this.repeatGap = 0;
    this.mergeGap = -1;
    this.mergeGroup = null;
    this.mergeGroupGap = -1;
    this.textOverlap = true;
}
 
Example #9
Source File: ClusterMarkerRenderer.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets a bitmap for a given cluster size
 *
 * @param size The cluster size. Can be greater than CLUSTER_MAXSIZE.
 * @return A somewhat cool bitmap to be used as the cluster marker
 */
protected Bitmap getClusterBitmap(int size) {
    final String strValue;

    if (size >= CLUSTER_MAXSIZE) {
        // restrict cluster indicator size. Bigger clusters will show as "+" instead of ie. "45"
        size = CLUSTER_MAXSIZE;
        strValue = "+";
    } else {
        strValue = String.valueOf(size);
    }

    // return cached bitmap if exists. cache hit !
    if (mClusterBitmaps[size] != null)
        return mClusterBitmaps[size];

    // create and cache bitmap. This is unacceptable inside the GL thread,
    // so we'll call this routine at the beginning to pre-cache all bitmaps

    ScreenUtils.ClusterDrawable drawable = new ScreenUtils.ClusterDrawable(
            MAP_MARKER_CLUSTER_SIZE_DP - CLUSTER_MAXSIZE + size, // make size dependent on cluster size
            mStyleForeground,
            mStyleBackground,
            strValue
    );

    mClusterBitmaps[size] = drawable.getBitmap();
    return mClusterBitmaps[size];
}
 
Example #10
Source File: XmlAtlasThemeBuilder.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
@Override
SymbolStyle buildSymbol(SymbolBuilder<?> b, String src, Bitmap bitmap) {
    // we need to hash with the width/height included as the same symbol could be required
    // in a different size and must be cached with a size-specific hash
    String absoluteName = CanvasAdapter.getAbsoluteFile(mTheme.getRelativePathPrefix(), src).getAbsolutePath();
    int hash = (absoluteName + b.symbolWidth + b.symbolHeight + b.symbolPercent + b.symbolColor).hashCode();
    bitmapMap.put(hash, bitmap);
    return b.hash(hash).build();
}
 
Example #11
Source File: ScreenUtils.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
public Bitmap getBitmap() {
    int width = mSize, height = mSize;
    width = width > 0 ? width : 1;
    height = height > 0 ? height : 1;

    Bitmap bitmap = CanvasAdapter.newBitmap(width, height, 0);
    Canvas canvas = CanvasAdapter.newCanvas();
    canvas.setBitmap(bitmap);
    draw(canvas);

    return bitmap;
}
 
Example #12
Source File: MarkerSymbol.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
public MarkerSymbol(Bitmap bitmap, HotspotPlace hotspot, boolean billboard) {

        switch (hotspot) {
            case BOTTOM_CENTER:
                mOffset = new PointF(0.5f, 1);
                break;
            case TOP_CENTER:
                mOffset = new PointF(0.5f, 0);
                break;
            case RIGHT_CENTER:
                mOffset = new PointF(1, 0.5f);
                break;
            case LEFT_CENTER:
                mOffset = new PointF(0, 0.5f);
                break;
            case UPPER_RIGHT_CORNER:
                mOffset = new PointF(1, 0);
                break;
            case LOWER_RIGHT_CORNER:
                mOffset = new PointF(1, 1);
                break;
            case UPPER_LEFT_CORNER:
                mOffset = new PointF(0, 0);
                break;
            case LOWER_LEFT_CORNER:
                mOffset = new PointF(0, 1);
                break;
            default:
                mOffset = new PointF(0.5f, 0.5f);
        }

        mBitmap = bitmap;
        mBillboard = billboard;
        mTextureRegion = null;
    }
 
Example #13
Source File: TextureAtlas.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
public TextureAtlas(Bitmap bitmap) {
    texture = new TextureItem(bitmap);
    mWidth = texture.width;
    mHeight = texture.height;

    mRegions = new HashMap<Object, TextureRegion>();
}
 
Example #14
Source File: SymbolItem.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
public void set(float x, float y, Bitmap bitmap, int hash, float rotation, boolean billboard, int mergeGap, String mergeGroup, int mergeGroupGap, boolean textOverlap) {
    this.x = x;
    this.y = y;
    this.bitmap = bitmap;
    this.hash = hash;
    this.rotation = rotation;
    this.billboard = billboard;
    this.mergeGap = mergeGap;
    this.mergeGroup = mergeGroup;
    this.mergeGroupGap = mergeGroupGap;
    this.textOverlap = textOverlap;
}
 
Example #15
Source File: BitmapPacker.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
public void sort(ArrayList<Bitmap> images) {
    if (comparator == null) {
        comparator = new Comparator<Bitmap>() {
            public int compare(Bitmap o1, Bitmap o2) {
                return o1.getHeight() - o2.getHeight();
            }
        };
    }
    Collections.sort(images, comparator);
}
 
Example #16
Source File: BitmapPacker.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
public void sort(ArrayList<Bitmap> Bitmaps) {
    if (comparator == null) {
        comparator = new Comparator<Bitmap>() {
            public int compare(Bitmap o1, Bitmap o2) {
                return Math.max(o1.getWidth(), o1.getHeight()) - Math.max(o2.getWidth(), o2.getHeight());
            }
        };
    }
    Collections.sort(Bitmaps, comparator);
}
 
Example #17
Source File: MapHandler.java    From PocketMaps with MIT License 5 votes vote down vote up
private MarkerItem createMarkerItem(Context appContext, GeoPoint p, int resource, float offsetX, float offsetY) {
//      Drawable drawable = activity.getDrawable(resource); // Since API21
      Drawable drawable = ContextCompat.getDrawable(appContext, resource);
      Bitmap bitmap = AndroidGraphics.drawableToBitmap(drawable);
      MarkerSymbol markerSymbol = new MarkerSymbol(bitmap, offsetX, offsetY);
      MarkerItem markerItem = new MarkerItem("", "", p);
      markerItem.setMarker(markerSymbol);
      return markerItem;
  }
 
Example #18
Source File: BitmapRenderer.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param bitmap with dimension being power of two
 * @param width  width used
 * @param height height used
 */
public synchronized void setBitmap(Bitmap bitmap, int width, int height) {
    mBitmap = bitmap;
    mWidth = width;
    mHeight = height;
    initialized = false;
}
 
Example #19
Source File: BitmapTileLoader.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setTileImage(Bitmap bitmap) {
    if (isCanceled() || !mTile.state(LOADING)) {
        bitmap.recycle();
        return;
    }

    BitmapBucket l = new BitmapBucket(false);
    l.setBitmap(bitmap, Tile.SIZE, Tile.SIZE, mLayer.pool);

    RenderBuckets buckets = new RenderBuckets();
    buckets.set(l);
    mTile.data = buckets;
}
 
Example #20
Source File: AndroidCanvas.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void drawBitmapScaled(Bitmap bitmap) {
    android.graphics.Bitmap scaledBitmap = android.graphics.Bitmap.createScaledBitmap(
            ((AndroidBitmap) bitmap).mBitmap, canvas.getWidth(), canvas.getHeight(), true);
    canvas.drawBitmap(scaledBitmap, 0, 0, null);
    scaledBitmap.recycle();
}
 
Example #21
Source File: SymbolBucket.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
private TextureItem getTexture(Bitmap bitmap) {
    TextureItem t;

    for (t = prevTextures; t != null; t = t.next) {
        if (t.bitmap == bitmap) {
            prevTextures = Inlist.remove(prevTextures, t);
            textures = Inlist.appendItem(textures, t);

            t.offset = 0;
            t.indices = 0;
            return t;
        }
    }
    return null;
}
 
Example #22
Source File: XmlMapsforgeAtlasThemeBuilder.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
@Override
SymbolStyle buildSymbol(SymbolBuilder<?> b, String src, Bitmap bitmap) {
    // we need to hash with the width/height included as the same symbol could be required
    // in a different size and must be cached with a size-specific hash
    String absoluteName = CanvasAdapter.getAbsoluteFile(mTheme.getRelativePathPrefix(), src).getAbsolutePath();
    int hash = (absoluteName + b.symbolWidth + b.symbolHeight + b.symbolPercent + b.symbolColor).hashCode();
    bitmapMap.put(hash, bitmap);
    return b.hash(hash).build();
}
 
Example #23
Source File: GpsPositionLayer.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
public GpsPositionLayer(GPMapView mapView) throws IOException {
    super(mapView.map());
    getName(mapView.getContext());

    peferences = PreferenceManager.getDefaultSharedPreferences(mapView.getContext());
    this.mapView = mapView;

    // set billboard rendering for TextureRegion (false is default)
    locationRenderer.setBillboard(false);

    Bitmap activeGpsBitmap = VtmUtilities.getBitmapFromResource(mapView.getContext(), eu.geopaparazzi.library.R.drawable.ic_my_location_moving_24dp);
    locationRenderer.setBitmapMarker(activeGpsBitmap);
    locationRenderer.setBitmapArrow(activeGpsBitmap);
}
 
Example #24
Source File: CanvasAdapter.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
protected static Bitmap createBitmap(String relativePathPrefix, String src, int width, int height, int percent, int color) throws IOException {
    if (src == null || src.length() == 0) {
        // no image source defined
        return null;
    }

    InputStream inputStream;
    if (src.startsWith(PREFIX_ASSETS)) {
        src = src.substring(PREFIX_ASSETS.length());
        inputStream = inputStreamFromAssets(relativePathPrefix, src);
    } else if (src.startsWith(PREFIX_FILE)) {
        src = src.substring(PREFIX_FILE.length());
        inputStream = inputStreamFromFile(relativePathPrefix, src);
    } else {
        inputStream = inputStreamFromFile(relativePathPrefix, src);

        if (inputStream == null)
            inputStream = inputStreamFromAssets(relativePathPrefix, src);
    }

    // Fallback to internal resources
    if (inputStream == null) {
        inputStream = inputStreamFromAssets("", src);
        if (inputStream != null)
            log.info("internal resource: " + src);
    }

    if (inputStream == null) {
        log.error("invalid resource: " + src);
        return null;
    }

    Bitmap bitmap;
    if (src.toLowerCase(Locale.ENGLISH).endsWith(".svg"))
        bitmap = decodeSvgBitmap(inputStream, width, height, percent, color);
    else
        bitmap = decodeBitmap(inputStream, width, height, percent, color);
    inputStream.close();
    return bitmap;
}
 
Example #25
Source File: Ruler.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onStart() {
    super.onStart();
    mRouteLayer = new RouteLayer(mMapHolder.getMap(), Color.RED, 5, mRoute);
    mMapHolder.getMap().layers().add(mRouteLayer);
    Bitmap bitmap = new AndroidBitmap(MarkerFactory.getMarkerSymbol(getContext(), R.drawable.dot_black, Color.RED));
    MarkerSymbol symbol = new MarkerSymbol(bitmap, MarkerItem.HotspotPlace.CENTER);
    ArrayList<MarkerItem> items = new ArrayList<>(mRoute.length());
    for (GeoPoint point : mRoute.getCoordinates()) {
        items.add(new MarkerItem(point, null, null, point));
    }
    mPointLayer = new ItemizedLayer<>(mMapHolder.getMap(), items, symbol, MapTrek.density, this);
    mMapHolder.getMap().layers().add(mPointLayer);
}
 
Example #26
Source File: MainActivity.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
private void addWaypointMarker(Waypoint waypoint) {
    MarkerItem marker = new MarkerItem(waypoint, waypoint.name, waypoint.description, waypoint.coordinates);
    if (waypoint.style.color != 0 && waypoint.style.color != MarkerStyle.DEFAULT_COLOR) {
        Bitmap bitmap = new AndroidBitmap(MarkerFactory.getMarkerSymbol(this, waypoint.style.color));
        marker.setMarker(new MarkerSymbol(bitmap, MarkerItem.HotspotPlace.BOTTOM_CENTER));
    }
    mMarkerLayer.addItem(marker);
}
 
Example #27
Source File: MapCoverageLayer.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
private static Bitmap getHillshadesBitmap(int color) {
    Bitmap bitmap;
    try {
        bitmap = CanvasAdapter.getBitmapAsset("", "symbols/hillshades.svg", 0, 0, 70, 0);
    } catch (IOException e) {
        log.error("Failed to read bitmap", e);
        return null;
    }
    android.graphics.Bitmap bitmapResult = android.graphics.Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), android.graphics.Bitmap.Config.ARGB_8888);
    android.graphics.Canvas canvas = new android.graphics.Canvas(bitmapResult);
    android.graphics.Paint paint = new android.graphics.Paint();
    paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(AndroidGraphics.getBitmap(bitmap), 0, 0, paint);
    return new AndroidBitmap(bitmapResult);
}
 
Example #28
Source File: OsmcSymbolFactory.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
private void drawSymbol(Canvas canvas, String foreground, int size) {
    if (foreground.length() == 0)
        return;

    Integer foregroundColor = null;
    String symbol = null;
    if (VALID_FOREGROUNDS.contains(foreground)) {
        // foreground is encoded as symbol without color
        logger.debug("  foreground: black {}", foreground);
        if ("shell".equals(foreground) || "shell_modern".equals(foreground))
            foregroundColor = Color.YELLOW;
        else
            foregroundColor = Color.BLACK;
        symbol = foreground;

    } else {
        // foreground should contain coloured symbol
        String[] foreground_parts = foreground.trim().split("\\s*_\\s*", 2);
        logger.debug("  foreground: {}", Arrays.toString(foreground_parts));
        if (foreground_parts.length == 2) {
            foregroundColor = ColorsCSS.get(foreground_parts[0]);
            if (VALID_FOREGROUNDS.contains(foreground_parts[1]))
                symbol = foreground_parts[1];
        }
        if (BuildConfig.DEBUG && foregroundColor == null)
            logger.error("Unknown foreground color: {}", foreground_parts[0]);
        if (BuildConfig.DEBUG && symbol == null)
            logger.error("Unknown foreground symbol: {}", foreground_parts.length == 1 ?
                    foreground_parts[0] : foreground_parts[1]);
    }
    if (foregroundColor != null && symbol != null) {
        try {
            Bitmap symbolBitmap = CanvasAdapter.getBitmapAsset("symbols/osmc", symbol + ".svg", size, size, 100, foregroundColor);
            canvas.drawBitmap(AndroidGraphics.getBitmap(symbolBitmap), 0, 0, null);
        } catch (IOException e) {
            logger.error("Failed to load bitmap for " + symbol, e);
        }
    }
}
 
Example #29
Source File: BitmapCache.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
public void clear() {
    synchronized (cache) {
        for (Bitmap bitmap : cache.values())
            bitmap.recycle();
        cache.clear();
    }
}
 
Example #30
Source File: GeopackageTileDataSource.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void query(MapTile tile, ITileDataSink sink) {
    QueryResult res = FAILED;

    try {
        byte[] imageBytes = adb.getTile(tableName, tile.tileX, tile.tileY, tile.zoomLevel);
        if (transparentColor != null || alpha != null) {
            android.graphics.Bitmap bmp = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
            if (transparentColor != null) {
                bmp = ImageUtilities.makeTransparent(bmp, transparentColor);
            }
            if (alpha != null) {
                bmp = ImageUtilities.makeBitmapTransparent(bmp, alpha);
            }
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bmp.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, bos);
            imageBytes = bos.toByteArray();
        }

        Bitmap bitmap = AndroidGraphics.decodeBitmap(new ByteArrayInputStream(imageBytes));

        sink.setTileImage(bitmap);
        res = QueryResult.SUCCESS;
    } catch (Exception e) {
        log.debug("{} Error: {}", tile, e.getMessage());
    } finally {
        sink.completed(res);
    }
}