所以基本上这是程序的一部分,它应该将图像的灰度值分别转换为下面的ascii代码,但是有错误说"方法必须返回一种char [] [] "我成功完成了第一个("返回null"),它打印了空格。
public static char[][] imageToASCII(Image img)
{
BufferedImage bufImg = convert(img);
for(int j=0; j<bufImg.getHeight(); j++)
{
for(int i=0; i<bufImg.getWidth(); i++)
{
int values=bufImg.getRGB(i,j);
Color oldColor = new Color(values);
int red = oldColor.getRed(); // get red value
int green = oldColor.getGreen(); // get green value
int blue = oldColor.getBlue();
double grayVal = 0.299*red + 0.587*green + 0.114*blue;
Color newColor = new Color((int)grayVal, (int)grayVal, (int)grayVal);
if(grayVal >= 230)
{
return null;
}
else if(grayVal >= 200 && grayVal < 230)
{
return .;
}
else if(grayVal >= 180 && grayVal < 200)
{
return *;
}
else if(grayVal >= 160 && grayVal < 180)
{
return :;
}
else if(grayVal >= 130 && grayVal < 160)
{
return o;
}
else if(grayVal >= 100 && grayVal < 130)
{
return &;
}
else if(grayVal >= 70 && grayVal < 100)
{
return 8;
}
else if(grayVal >=50 && grayVal < 70)
{
return #;
}
else
return @
}
}
}}
答案 0 :(得分:1)
首先,要返回一个char,你应该将它括在单引号中。 其次,你实际上并不想要返回一个角色,而是将它设置在一个字符矩阵中。以下是对您的代码的修改:
import java.awt.image.BufferedImage;
import java.awt.Color;
public class Test {
public static char[][] imageToASCII(BufferedImage img) {
int w = img.getWidth();
int h = img.getHeight();
char[][] res = new char[h][w];
for(int j=0; j<h; j++) {
for(int i=0; i<w; i++) {
int values = img.getRGB(i,j);
Color oldColor = new Color(values);
int red = oldColor.getRed(); // get red value
int green = oldColor.getGreen(); // get green value
int blue = oldColor.getBlue();
double grayVal = 0.299*red + 0.587*green + 0.114*blue;
if(grayVal >= 230) {
res[j][i] = ' ';
} else if(grayVal >= 200 && grayVal < 230) {
res[j][i] = '.';
} else if(grayVal >= 180 && grayVal < 200) {
res[j][i] = '*';
} else if(grayVal >= 160 && grayVal < 180) {
res[j][i] = ':';
} else if(grayVal >= 130 && grayVal < 160) {
res[j][i] = 'o';
} else if(grayVal >= 100 && grayVal < 130) {
res[j][i] = '&';
} else if(grayVal >= 70 && grayVal < 100) {
res[j][i] = '8';
} else if(grayVal >=50 && grayVal < 70) {
res[j][i] = '#';
} else {
res[j][i] = '@';
}
}
}
return res;
}
}
此外,此代码在性能方面效率非常低。对于W x H>的图像。 255,最好避免顺序ifs。这可以通过在初始化时创建一个数组来完成,该数组将每个可能的灰度值映射到相应的char,然后在循环遍历图像时解除引用这样的数组:
import java.awt.image.BufferedImage;
import java.awt.Color;
public class Test {
private static char[] map;
private static void initMap() {
map = new char[256];
int[][] ranges = new int[][] {
{0, 50}, {50, 70}, {70, 100}, {100, 130},
{130, 160}, {160, 180}, {180, 200},
{200, 230}, {230, 256}
};
char[] v = {' ', '#', '8', '&', 'o', ':', '*', '.', ' '};
for(int i=0; i<v.length; i++) {
for(int j=ranges[i][0]; j < ranges[i][1]; j++) {
map[j] = v[i];
}
}
}
public static char[][] imageToASCII(BufferedImage img) {
if (map == null) {
initMap();
}
int w = img.getWidth();
int h = img.getHeight();
char[][] res = new char[h][w];
for(int j=0; j<h; j++) {
for(int i=0; i<w; i++) {
int values = img.getRGB(i,j);
Color oldColor = new Color(values);
int red = oldColor.getRed(); // get red value
int green = oldColor.getGreen(); // get green value
int blue = oldColor.getBlue();
int grayVal = (int)(0.299*red + 0.587*green + 0.114*blue);
if (grayVal > 255) {
grayVal = 255;
}
res[j][i] = map[grayVal];
}
}
return res;
}
}
答案 1 :(得分:0)
这是一个课程,向您展示应该返回的内容的结构,以及如何在控制台中显示它:
package stackOverflow;
public class ImageToGrayscale
{
public static void main(String[] args) {
char[][] image = return2DChars(30, 10);
for (int i = 0; i < image.length; i++) {
char[] row = image[i];
for (int j = 0; j < row.length; j++) {
System.out.print(row[j]);
}
System.out.println();
}
}
public static char[][] return2DChars(int width, int height) {
char[][] image = new char[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
image[i][j] = (Math.random() < 0.5) ? ':' : '#';
}
}
return image;
}
}
输出:
#:#:###:::::#:#::#:::##:#::###
####:#::###:::#:###:##:::#::##
##:#:##:##:::#:##::#:###::::::
:::#:::###:#::########:#::##:#
:#::#:####:::#::#:::##::#:#:::
####::####:#:::##:##::::::####
::####:##::#:#::::#::#:#:#::#:
#::###::##:::####:::#:#:##::##
#:####::::#####:#####::###:###
::##:#:###:#:::#::####:::##:#: