// method derived from a Snippet from http://snippets.dzone.com/posts/show/3257
// scales an image according to the ratios given as parameters
private Image rescaleImage(Image image, double XRatio, double YRatio)
{
// the old height and width
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
// what the new height and width should be
int newWidth = (int)(XRatio * sourceWidth);
int newHeight = (int)(YRatio * sourceHeight);
Image newImage = Image.createImage(newWidth, newHeight);
Graphics g = newImage.getGraphics();
for (int y = 0; y < newHeight; y++)
{
for (int x = 0; x < newWidth; x++)
{
g.setClip(x, y, 1, 1);
int dx = (x * sourceWidth) / newWidth;
int dy = (y * sourceHeight) / newHeight;
g.drawImage(image, (x - dx), (y - dy), Graphics.LEFT | Graphics.TOP);
}
}
return Image.createImage(newImage);
}
它正确地缩放图像,遗憾的是我似乎失去了该方法返回的图像的透明度。我对这些概念相当新,任何帮助都将不胜感激!请注意,要在任何支持java的移动设备上正确显示,重新缩放需要在代码中完成,而不是在任何类型的图像编辑器中完成。
提前致谢!
答案 0 :(得分:4)
感谢所有一直在寻找这个看似广泛且未解决的问题的解决方案的人。我设法在http://willperone.net/Code/codescaling.php
找到了一个很好的解决方案您只需将createRGBImage参数中的“false”更改为true即可。这标记了将每个像素的高位作为alpha值处理的方法。这是我的实现,与上面的原始链接没有太大的变化。
初始化画布时,XRatio和YRatio被声明为常量,其中XRatio = this.getWidth()(当前手机的屏幕宽度)除以原始背景图像宽度,YRatio与getHeight()/原始背景图像高度
// RESCALEIMAGE
// scales an image according to the ratios given as parameters
// derived from http://willperone.net/Code/codescaling.php
public static Image rescaleImage(Image original, double XRatio, double YRatio)
{
// the original width and height
int originalWidth = original.getWidth();
int originalHeight = original.getHeight();
// the target width and height
int newWidth = (int)(XRatio * originalWidth);
int newHeight = (int)(YRatio * originalHeight);
// create and fill the pixel array from the original image
int[] rawInput = new int[originalHeight * originalWidth];
original.getRGB(rawInput, 0, originalWidth, 0, 0, originalWidth, originalHeight);
// pixel array for the target image
int[] rawOutput = new int[newWidth*newHeight];
// YD compensates for the x loop by subtracting the width back out
int YD = (originalHeight / newHeight) * originalWidth - originalWidth;
int YR = originalHeight % newHeight;
int XD = originalWidth / newWidth;
int XR = originalWidth % newWidth;
int outOffset= 0;
int inOffset= 0;
for (int y = newHeight, YE = 0; y > 0; y--)
{
for (int x = newWidth, XE = 0; x > 0; x--)
{
rawOutput[outOffset++] = rawInput[inOffset];
inOffset += XD;
XE += XR;
if (XE >= newWidth)
{
XE -= newWidth;
inOffset++;
}
}
inOffset += YD;
YE += YR;
if (YE >= newHeight)
{
YE -= newHeight;
inOffset += originalWidth;
}
}
return Image.createRGBImage(rawOutput, newWidth, newHeight, true);
}
答案 1 :(得分:1)
可能是因为您没有区分像素值中的alpha。您可以做的是添加一个额外的例程来处理带有alpha的例程,以便保持它们可能的alpha值。
基本上它会检查每个像素,看它是否有alpha,如果有,检查它是否会在调整大小的图像上,如果是,请用它的alpha应用它,如果没有,丢弃它。
答案 2 :(得分:0)
也许是Image类的方法:
Image getScaledInstance(int width, int height, int hints)
非常适合您的需求..?
来自:http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/awt/Image.html