java使用字符画一个海绵宝宝
更新时间:2022年01月12日 14:24:27 作者:泰然TJ
这篇文章主要为大家详细介绍了java使用字符画一个海绵宝宝,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了java使用字符画一个海绵宝宝的具体代码,供大家参考,具体内容如下
用字符画一个海绵宝宝
用" “和”*"两个字符画出一个海绵宝宝,效果如下:

emm……效果可能不是很好,原图是这样的:

下面展示我的代码
代码
提示:代码仅供参考,大部分来自于网络
package package1;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageDraw {
public static void main(String[] args) throws IOException {
//需要使用哪种灰度化方式,就去掉那一行的注释"//"。
//grayImage(1,"E:\\image.jpg");//最大值法灰度化
//grayImage(2,"E:\\image.jpg");//最小值法灰度化
//grayImage(3,"E:\\image.jpg");//平均值法灰度化
//grayImage(4,"E:\\image.jpg");//加权法灰度化
}
public static void grayImage(int status, String imagePath) throws IOException {
File file = new File(imagePath);
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
BufferedImage grayImage = new BufferedImage(width, height, image.getType());
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int color = image.getRGB(j, i);
final int r = (color >> 16) & 0xff;
final int g = (color >> 8) & 0xff;
final int b = color & 0xff;
int gray = 0;
if (status == 1) {
gray = getBigger(r, g, b);// 最大值法灰度化
} else if (status == 2) {
gray = getSmall(r, g, b);// 最小值法灰度化
} else if (status == 3) {
gray = getAvg(r, g, b);// 均值法灰度化
} else if (status == 4) {
gray = (int) (0.3 * r + 0.59 * g + 0.11 * b);// 加权法灰度化
}
if(gray<=128) {
gray=0;
System.out.print("*");
}else {
gray=255;
System.out.print(" ");
}
}
System.out.println();
}
}
// 比较三个数的大小
public static int getBigger(int x, int y, int z) {
if (x >= y && x >= z) {
return x;
} else if (y >= x && y >= z) {
return y;
} else if (z >= x && z >= y) {
return z;
} else {
return 0;
}
}
// 比较三个数的大小取最小数
public static int getSmall(int x, int y, int z) {
if (x <= y && x <= z) {
return x;
} else if (y >= x && y >= z) {
return y;
} else if (z >= x && z >= y) {
return z;
} else {
return 0;
}
}
// 均值法
public static int getAvg(int x, int y, int z) {
int avg = (x + y + z) / 3;
return avg;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章
深入剖析springBoot中的@Scheduled执行原理
这篇文章主要介绍了springBoot中的@Scheduled执行原理,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-11-11
Spring Boot中@Autowired注入为空的原因以及解决方法
最近在开发中遇到了使用@Autowired注解自动装配时会报空指针,发现对象并没有装配进来,下面这篇文章主要给大家介绍了关于Spring Boot中@Autowired注入为空的原因以及解决方法,需要的朋友可以参考下2024-01-01
关于重写equals()方法和hashCode()方法及其简单的应用
这篇文章主要介绍了关于重写equals()方法和hashCode()方法及其简单的应用,网上的知识有些可能是错误的,关于 equals() 方法的理解,大家讨论不一样,需要的朋友可以参考下2023-04-04


最新评论