java使用字符畫一個海綿寶寶
本文實例為大家分享了java使用字符畫一個海綿寶寶的具體代碼,供大家參考,具體內(nèi)容如下
用字符畫一個海綿寶寶
用" “和”*"兩個字符畫出一個海綿寶寶,效果如下:

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

下面展示我的代碼
代碼
提示:代碼僅供參考,大部分來自于網(wǎng)絡(luò)
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入剖析springBoot中的@Scheduled執(zhí)行原理
這篇文章主要介紹了springBoot中的@Scheduled執(zhí)行原理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Elasticsearch?計數(shù)分詞中的token使用實例
這篇文章主要為大家介紹了Elasticsearch?計數(shù)分詞中的token使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Spring?Boot中@Autowired注入為空的原因以及解決方法
最近在開發(fā)中遇到了使用@Autowired注解自動裝配時會報空指針,發(fā)現(xiàn)對象并沒有裝配進(jìn)來,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot中@Autowired注入為空的原因以及解決方法,需要的朋友可以參考下2024-01-01
SpringBoot啟動時自動執(zhí)行指定方法的幾種實現(xiàn)方式
在Spring Boot應(yīng)用程序中,要實現(xiàn)在應(yīng)用啟動時自動執(zhí)行某些代碼,本文主要介紹了SpringBoot啟動時自動執(zhí)行指定方法的幾種方式,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2024-03-03
使用Java編寫導(dǎo)出不確定行數(shù)列數(shù)數(shù)據(jù)的工具類
這篇文章主要為大家詳細(xì)介紹了如何使用Java編寫導(dǎo)出不確定行數(shù)列數(shù)數(shù)據(jù)的工具類,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
關(guān)于重寫equals()方法和hashCode()方法及其簡單的應(yīng)用
這篇文章主要介紹了關(guān)于重寫equals()方法和hashCode()方法及其簡單的應(yīng)用,網(wǎng)上的知識有些可能是錯誤的,關(guān)于?equals()?方法的理解,大家討論不一樣,需要的朋友可以參考下2023-04-04

