绘制图像的一部分画面(无加载所有内存) [英] Draw part of image to screen (without loading all to memory)

查看:158
本文介绍了绘制图像的一部分画面(无加载所有内存)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的,是绘制图像屏幕的切口,在我选择的位置。

我可以很容易地加载到一个位图这一点。再画一个小节。

但是,当图像大,这显然排气记忆。

我的屏幕是surfaceview。所以,有一个帆布等。

所以,我怎么能画出图像的一部分,在给定的偏移量,并调整大小。如果没有加载原始内存

我发现,沿着正确的方向看了一个答案,但它不能正常工作。通过使用可绘制来自文件。低于code尝试。除了随机调整大小它产生,这也是不完整的。

例如:

 可绘制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();

    //计算我需要什么样的形象的一部分...
    img.setBounds(0,0,drawWidth,drawHeight);

    //申请帆布矩阵抽签之前移动...?
    img.draw(m_canvas);
 

解决方案

Bitma pregionDe codeR 可用于加载图像的指定区域。这里有一个例子方法设置在两个的ImageView S中的位图。第一个是完整的图像,发送是完整的图像的只是一个区:

 私人无效configureImageViews(){

    字符串路径= externalDirectory()+即File.separatorChar
            +sushi_plate_tokyo_20091119.png;

    ImageView的fullImageView =(ImageView的)findViewById(R.id.fullImageView);
    ImageView的bitma pregionImageView =(ImageView的)findViewById(R.id.bitma pregionImageView);

    位图fullBitmap = NULL;
    位图regionBitmap = NULL;

    尝试 {
        Bitma pregionDe codeR bitma pregionDe codeR = Bitma pregionDe codeR
                .newInstance(路径,FALSE);

        //获取完整图像的宽度和高度
        INT全角= bitma pregionDe coder.getWidth();
        INT fullHeight = bitma pregionDe coder.getHeight();

        //获取整个图像的位图(满盘寿司)
        矩形fullRect =新的Rect(0,0,全角,fullHeight);
        fullBitmap = bitma pregionDe coder.de codeRegion(fullRect,NULL);

        //获取一个区域的位图只(鳗鱼只)
        矩形regionRect =新的矩形(275,545,965,1025);
        regionBitmap = bitma pregionDe coder.de codeRegion(regionRect,NULL);

    }赶上(IOException异常E){
        //处理IOException异常适当
        e.printStackTrace();
    }

    fullImageView.setImageBitmap(fullBitmap);
    bitma pregionImageView.setImageBitmap(regionBitmap);

}

//获取外部存储目录
公共静态字符串externalDirectory(){
    文件fil​​e = Environment.getExternalStorageDirectory();
    返回file.getAbsolutePath();
}
 

结果是完整的图像(顶部)与图像(下图)的只是一个区:

What I want to do, is draw a cutout of an image to screen, at the position of my choosing.

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

But when the image is big, this will obviously exhaust memory.

My screen is a surfaceview. So has a canvas etc.

So how can I draw part of an image, at a given offset, and resized. Without loading the origional to memory

I found an answer that looked along the right lines, but it doesn't work properly. With the use of drawables from file. Code attempt below. Aside from the random resizes it produces, it is also incomplete.

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);

解决方案

BitmapRegionDecoder can be used to load a specified region of an image. Here's an example method setting the bitmap in two ImageViews. The first is the full image, the send is just a region 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();
}

Result is the full image (top) and just a region of the image (bottom):

这篇关于绘制图像的一部分画面(无加载所有内存)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆