Android的 - 调整大小的位图削减它,而不是扩展它的 [英] Android - Resizing bitmap cuts it instead of scaling it

查看:181
本文介绍了Android的 - 调整大小的位图削减它,而不是扩展它的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,用户可以绘制到其尺寸为W =匹配家长和H = 250dp景色。

I have an application where the user can draw to a view whose dimensions are W = Match Parent and H = 250dp.

我需要将其调整为W = 399pixels和H = 266pixels这样我就可以适当地通过蓝牙热敏打印机打印。我得到了调整后的图像所需要的尺寸,但是,输出我得到的是其尺寸缩放的尺寸我要原来的切碎版本。

I need to resize it to W = 399pixels and H = 266pixels so I can print it properly via a bluetooth thermal printer. I am getting the desired dimensions of the resized image, however, the output I get is a chopped version of the original whose dimensions are the scaled dimensions I want.

这是code我用它来得到视图中的数据,并调整其大小。

This is the code I use to get the data from the view and resize it.

Bitmap bitmap = Bitmap.createBitmap(mView.getWidth(), mView.getHeight(), Bitmap.Config.ARGB_8888);
ByteArrayOutputStream stream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);

//Resize the image
Bitmap resizedImage = Bitmap.createScaledBitmap(bitmap, 399, 266, false);


//code for resized
Canvas c = new Canvas(resizedImage);
c.drawColor(Color.WHITE);
c.drawBitmap(resizedImage, 0, 0, null);
mView.draw(c);

resizedImage.compress(Bitmap.CompressFormat.PNG, 90, stream);

我在做什么错在这里?

What am I doing wrong here?

编辑:
我认为这个问题是画布,我绘制调整图像大,出于某种原因,绘图命令不工作,每当我打印,它的打印该画布的原始内容。

I think the problem is that the canvas I'm drawing the resized image to is large, and for some reason, the draw command does not work and whenever I print, it's printing the original contents of that canvas.

推荐答案

的问题是,调整大小后的图像的位图被创建,获取用户输入的位图的右后实例化。这是我的code奏效。

The problem is that the Bitmap of the resized image is instantiated right after the creation of the bitmap that gets the user input. This was my code that worked.

不过,请注意,我做了一个ImageView的保持调整后的图像。

However, note that I made an imageView to hold the resized image.

    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    //get the user input and store in a bitmap
    Bitmap bitmap = Bitmap.createBitmap(mView.getWidth(), mView.getHeight(), Bitmap.Config.ARGB_8888);


    //Create a canvas containing white background and draw the user input on it
    //this is necessary because the png format thinks that white is transparent
    Canvas c = new Canvas(bitmap);
    c.drawColor(Color.WHITE);
    c.drawBitmap(bitmap, 0, 0, null);

    //redraw 
    mView.draw(c);

    //create resized image and display
    Bitmap resizedImage = Bitmap.createScaledBitmap(bitmap, 399, 266, true);
    imageView.setImageBitmap(resizedImage);

这篇关于Android的 - 调整大小的位图削减它,而不是扩展它的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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