Draw part of the image on the screen (without loading everything into memory)

What I want to do is draw a cutout of the image on the screen, in my choice.

I can easily load this into a bitmap. and then draw a subsection.

But when the image is large, it will obviously run out of memory.

My screen is a surface view. So there is canvas, etc.

So, how can I draw part of the image with a given offset and resize. Without loading the original into memory

I found an answer that looked at the correct lines, but it does not work properly. Using drawables from a file. The following is sample code. Besides the random sizes that it produces, it is also incomplete.

Example:

Example

Drawable img = Drawable.createFromPath(Files.SDCARD + image.rasterName); int drawWidth = (int) (image.GetOSXWidth()/(maxX - minX)) * m_canvas.getWidth(); int drawHeight = (int)(image.GetOSYHeight()/(maxY - minY)) * m_canvas.getHeight(); // Calculate what part of image I need... img.setBounds(0, 0, drawWidth, drawHeight); // apply canvas matrix to move before draw...? img.draw(m_canvas); 
+6
source share
1 answer

BitmapRegionDecoder can be used to load a specified area of ​​an image. Here is an example of a method that sets a raster image to two ImageView s. The first is the full image, send is just the area of ​​the full image:

 private void configureImageViews() { String path = externalDirectory() + File.separatorChar + "sushi_plate_tokyo_20091119.png"; ImageView fullImageView = (ImageView) findViewById(R.id.fullImageView); ImageView bitmapRegionImageView = (ImageView) findViewById(R.id.bitmapRegionImageView); Bitmap fullBitmap = null; Bitmap regionBitmap = null; try { BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder .newInstance(path, false); // Get the width and height of the full image int fullWidth = bitmapRegionDecoder.getWidth(); int fullHeight = bitmapRegionDecoder.getHeight(); // Get a bitmap of the entire image (full plate of sushi) Rect fullRect = new Rect(0, 0, fullWidth, fullHeight); fullBitmap = bitmapRegionDecoder.decodeRegion(fullRect, null); // Get a bitmap of a region only (eel only) Rect regionRect = new Rect(275, 545, 965, 1025); regionBitmap = bitmapRegionDecoder.decodeRegion(regionRect, null); } catch (IOException e) { // Handle IOException as appropriate e.printStackTrace(); } fullImageView.setImageBitmap(fullBitmap); bitmapRegionImageView.setImageBitmap(regionBitmap); } // Get the external storage directory public static String externalDirectory() { File file = Environment.getExternalStorageDirectory(); return file.getAbsolutePath(); } 

The result is a full image (above) and only the image area (below):

enter image description here

+5
source

All Articles