欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java使用字符畫一個海綿寶寶

 更新時間:2022年01月12日 14:24:27   作者:泰然TJ  
這篇文章主要為大家詳細介紹了java使用字符畫一個海綿寶寶,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java使用字符畫一個海綿寶寶的具體代碼,供大家參考,具體內(nèi)容如下

用字符畫一個海綿寶寶

用" “和”*"兩個字符畫出一個海綿寶寶,效果如下:

emm……效果可能不是很好,原圖是這樣的:

下面展示我的代碼

代碼

提示:代碼僅供參考,大部分來自于網(wǎng)絡

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 {
?? ??? ?//需要使用哪種灰度化方式,就去掉那一行的注釋"http://"。
?? ??? ?//grayImage(1,"E:\\image.jpg");//最大值法灰度化?
?? ??? ?//grayImage(2,"E:\\image.jpg");//最小值法灰度化?
?? ??? ?//grayImage(3,"E:\\image.jpg");//平均值法灰度化
?? ??? ?//grayImage(4,"E:\\image.jpg");//加權(quán)法灰度化?
?? ? ?}
?? ?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);// 加權(quán)法灰度化
?? ??? ??? ??? ?}
?? ??? ??? ??? ?if(gray<=128) {
?? ??? ??? ??? ??? ?gray=0;
?? ??? ??? ??? ??? ?System.out.print("*");
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?gray=255;
?? ??? ??? ??? ??? ?System.out.print(" ");
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?System.out.println();
?? ??? ?}
?? ?}
?? ?// 比較三個數(shù)的大小
?? ?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;
?? ??? ?}
?? ?}
?? ?// 比較三個數(shù)的大小取最小數(shù)
?? ?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;
?? ?}
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論