所以这个程序需要对图像做一些事情。我开始编写称为replicate的方法,它需要做的是接受num1和num2,然后将图像左右复制num1次,然后从上到下复制图像num2次。因此,同样的小图像将充当桌面壁纸,它重复自己。我不允许使用图像缓冲区。谢谢
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
public class ImageTool {
// THIS METHOD MAY BE CALLED, BUT MUST NOT BE MODIFIED!
//
public static int[][] readGrayscaleImage(String filename) {
int [][] result = null;
try {
File imageFile = new File(filename);
BufferedImage image = ImageIO.read(imageFile);
int height = image.getHeight();
int width = image.getWidth();
result = new int[height][width];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int rgb = image.getRGB(x, y);
result[y][x] = rgb & 0xff;
}
}
}
catch (IOException ioe) {
System.err.println("Problems reading file named " + filename);
System.exit(1);
}
return result;
}
// THIS METHOD MAY BE CALLED, BUT MUST NOT BE MODIFIED!
//
public static void writeGrayscaleImage(String filename, int[][] array) {
int width = array[0].length;
int height = array.length;
try {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int rgb = array[y][x];
rgb |= rgb << 8;
rgb |= rgb << 16;
image.setRGB(x, y, rgb);
}
}
File imageFile = new File(filename);
ImageIO.write(image, "jpg", imageFile);
}
catch (IOException ioe) {
System.err.println("Problems writing file named " + filename);
System.exit(1);
}
}
public static void main (String [] args) {
if (args.length < 1) {
System.out.println("Invalid program execution");
System.out.println("Please provide command");
System.exit(-1);
}
if (args[0].equals("--dump")){
String fileName = args[1];
int [][] image = readGrayscaleImage(fileName);
print2dArray(image);
} else if (args[0].equals("--reflectV")){
String fileName = args[1];
int [][] image = readGrayscaleImage(fileName);
int [][] reflected = reflectV(image);
String outputFilename = args[2];
writeGrayscaleImage(outputFilename, reflected);
} else if (args[0].equals("--reflectH")){
String fileName = args[1];
int [][] image = readGrayscaleImage(fileName);
int [][] reflected = reflectH(image);
String outputFilename = args[2];
writeGrayscaleImage(outputFilename, reflected);
} else if (args[0].equals("--ascii")){
String fileName=args[1];
int [][] image = readGrayscaleImage(fileName);
ascii(image);
} else if (args[0].equals("--replicate")) {
String fileName = args[2];
int [][] image = readGrayscaleImage(fileName);
double factor = args[0];
String outputFilename = args[3];
writeGrayscaleImage(outputFilename, reflected);
}
}
public static void replicate(double num1, double num2, int[][]arr) {
double length = num1 * arr.length;
double height = num2 * arr[0].length;
int[][]newArr = new int[length][height];
for(int i = 0; i <= newArr.length; i++){
for(int j = 0; j <= arr.length; j++){
newArr[i][j]=arr[i][j];
}
}
}
public static void ascii (int[][]arr) {
int rows = arr.length;
int cols = arr[0].length;
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(arr[i][j] >= 0 && arr[i][j] <= 25){
System.out.print("M");
} else if(arr[i][j]>=26 && arr[i][j] <=50){
System.out.print("$");
} else if(arr[i][j]>=51 && arr[i][j] <= 76){
System.out.print("0");
} else if(arr[i][j]>=77 && arr[i][j] <=102){
System.out.print("|");
} else if(arr[i][j]>=103 && arr[i][j]<=127){
System.out.print("*");
} else if (arr[i][j]>=128 && arr[i][j]<=152){
System.out.print(":");
} else if (arr[i][j]>=153 && arr[i][j]<=178){
System.out.print("=");
} else if (arr[i][j]>=179 && arr[i][j]<=204){
System.out.print("\'");
} else if (arr[i][j]>=205 && arr[i][j]<=230){
System.out.print(".");
} else if (arr[i][j]>=231 && arr[i][j]<=255){
System.out.print(" ");
}
}
System.out.println();
}
}
public static void print2dArray(int[][] arr) {
for (int i = 0; i <arr.length; i++){
//System.out.println(Arrays.toString(arr[i]));
for (int j = 0; j< arr[i].length; j++){
System.out.format("%3d, " , arr[i][j]);
}
System.out.println();
}
}
public static int [][] reflectV (int [][] arr) {
int rows = arr.length;
int cols = arr[0].length;
int [][] reflected = new int[rows][cols];
for (int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++) {
reflected [i][j] = arr[i][cols-j-1];
}
}
//go through arr and reverse values in each row
return reflected;
}
public static int [][] reflectH (int [][] arr) {
int rows = arr.length;
int cols = arr[0].length;
int [][] reflected = new int[rows][cols];
for (int i = 0; i < cols; i++){
for(int j = 0; j < rows; j++) {
reflected [j][i] = arr[cols-j-1][i];
}
}
//go through arr and reverse values in each row
return reflected;
}
}
答案 0 :(得分:0)
这应该是解决方案:
public static int[][] replicate(double num1, double num2, int[][]arr) {
double length = num1 * arr.length;
double height = num2 * arr[0].length;
int[][]newArr = new int[(int) length][(int) height];
for(int i = 0; i < newArr.length; i++){
for(int j = 0; j < newArr[i].length; j++){
newArr[i][j]=arr[i % arr.length][j % arr[0].length];
}
}
return newArr;
}
测试输入:
1 2 3
4 5 6
7 8 9
该方法按如下方式计算输出数组(num1=1.8d
和num2=1.8d
):
1 2 3 1 2
4 5 6 4 5
7 8 9 7 8
1 2 3 1 2
4 5 6 4 5
请注意我已更改方法的签名。它现在返回结果数组。