📄 tools.java
字号:
} else {
//考虑换行
if (curY + 2 * font.getBaselinePosition() < y + h) {
curY += vDelta;
curX = x + leftMargin;
g.drawChar(c, curX, curY, Graphics.LEFT | Graphics.TOP);
curCharIndex++;
curX += font.charWidth(c);
} else {
break;
}
}
}
}
/**
* 图像的缩放
* @param srcImage Image
* @param newW int
* @param newH int
* @return Image
*/
public static final Image scale(Image srcImage, int newW, int newH) {
int srcW = srcImage.getWidth();
int srcH = srcImage.getHeight();
//先做水平方向上的伸缩变换
Image tmp = Image.createImage(newW, srcH);
Graphics g = tmp.getGraphics();
for (int x = 0; x < newW; x++) {
g.setClip(x, 0, 1, srcH);
//按比例放缩
g.drawImage(srcImage, x - x * srcW / newW, 0,
Graphics.LEFT | Graphics.TOP);
}
//再做垂直方向上的伸缩变换
Image dst = Image.createImage(newW, newH);
g = dst.getGraphics();
for (int y = 0; y < newH; y++) {
g.setClip(0, y, newW, 1);
//按比例放缩
g.drawImage(tmp, 0, y - y * srcH / newH,
Graphics.LEFT | Graphics.TOP);
}
return dst;
}
/**
* 根据秒得到时间字符串 00:00:00
* @param num long
* @return String
*/
public String getTime(long second) {
String str = null;
if(second<0)
{
return "00:00:00";
}
long hour=0,minute=0;
hour = second/3600;//小时
minute = (second%3600)/60;//分钟
second = second%60;
String strH,strM,strS;
if(hour>=24)
{
hour =hour%12;
}
if (hour < 10) {
strH = "0" + hour;
} else {
strH = hour + "";
}
if(minute<10)
{
strM = "0"+minute;
}else{
strM = ""+minute;
}
if(second<10)
{
strS = "0"+second;
}else {
strS =""+second;
}
str = strH+":"+strM+":"+strS;
// System.out.println("str==="+str);
return str;
}
public static int TRANSPARENT = 0;
public Image getAngleImage(Image img,double angle)
{
int w = img.getWidth();
int h = img.getHeight();
int data[] = new int[w*h];
img.getRGB(data,0,w,0,0,w,h);
// img=Image.createImage(rotate(data,w,h,angle),0,rotate(data,w,h,angle).length);
return img;
}
/**
* 解决“坏点”仔细思考一下,坏点出现的主要原因在于,旋转公式的定义域是整个实数域,而像素点显然是离散点,因此,在进行映射时,会产生一定的误差,所以造成本该被定义的像素点,没有被映射到。其实,在图像处理领域,已经存在很多优化的算法,而且理论都基于数学的。
回到我们的问题,为了解决坏点,我们可以这样来考虑:改遍历原图像数组为遍历newPixels数组,这样就能保证所有的newPixels可以被覆盖。具体代码如下:
java 代码
* @param _pixels int[]
* @param _width int
* @param _height int
* @param _angle double
* @return int[]
*/
public static int[] rotate(int[] _pixels,int _width,int _height,double _angle)
{
int i,j;
double radius = Math.sqrt(_width*_width + _height*_height);
int r = (int)radius;
int[] newPixels = new int[r*r];
for(i = 0; i < newPixels.length;i++)
{
newPixels[i] = (TRANSPARENT)<<24;
}
double x1,y1;
int x2,y2;
double cos = Math.cos(_angle);
double sin = Math.sin(_angle);
for(i = 0;i < r;i++)
{
for(j = 0;j < r;j++)
{
x1 = j + (- r)/2;
y1 = i + (- r)/2;
x2 = (int)(x1*cos + y1*sin);
y2 = (int)(-x1*sin + y1*cos);
if(x2 >= -_width/2&&x2< _width/2&&y2 >= -_height/2&&y2< _height/2)
{
int k = (y2 + _height/2)*_width+x2+_width/2;
newPixels[i*r + j] = _pixels[k];
}
}
}
return newPixels;
}
public static int[] rotate2(int[] _pixels,int _width,int _height,double _angle)
{
int i,j;
double radius = Math.sqrt(_width*_width + _height*_height);
int r = (int)radius;
int[] newPixels = new int[r*r];
for(i = 0; i < newPixels.length;i++)
{
newPixels[i] = (TRANSPARENT)<<24;
}
double x1,y1;
int x2,y2;
double cos = Math.cos(_angle);
double sin = Math.sin(_angle);
for(i = 0; i < _height;i++)
{
for(j = 0;j < _width;j++)
{
x1 = j + (- _width)/2;
y1 = i + (- _height)/2;
x2 = (int)(x1*cos - y1*sin);
y2 = (int)(x1*sin + y1*cos);
x2 += r/2;
y2 += r/2;
newPixels[y2*r+x2] = _pixels[i*_width+j];
}
}
return newPixels;
}
/**
* 速度优化
上面这种算法的时间复杂程度是O(n^2),那么对该算法是否可以优化呢?参考了《A Fast Algorithm for Rotating Bitmaps》一文,可以对算法作以下优化:
* @param _pixels int[]
* @param _width int
* @param _height int
* @param _angle double
* @return int[]
*/
public static int[] rotate3(int[] _pixels,int _width,int _height,double _angle)
{
int i,j;
double radius = Math.sqrt(_width*_width + _height*_height);
int r = (int)radius;
int[] newPixels = new int[r*r];
for(i = 0; i < newPixels.length;i++)
{
newPixels[i] = (TRANSPARENT)<<24;
}
double x2,y2;
int x3,y3;
double cos = Math.cos(_angle);
double sin = Math.sin(_angle);
for(i = 0;i < r;i++)
{
x2 = (-r/2)*cos + (i - r/2)*sin;
y2 = (r/2)*sin + (i - r/2)*cos;
x3 = (int)x2;
y3 = (int)y2;
for(j = 0;j < r;j++)
{
if(x3 >= -_width/2&&x3< _width/2&&y3 >= -_height/2&&y3< _height/2)
{
newPixels[i*r +j] = _pixels[(y3 + _height/2)*_width+x3+_width/2];
}
x2 += cos;
y2 -= sin;
x3 = (int)x2;
y3 = (int)y2;
}
}
return newPixels;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -