gpt4 book ai didi

java - 如何复制数组?

转载 作者:行者123 更新时间:2023-11-29 07:44:33 25 4
gpt4 key购买 nike

我想做的是尝试拍摄图像并将其制成平铺图像。起始图像应如下所示。 http://i1146.photobucket.com/albums/o525/walroid/letter_Q_grayscale_zpsd3b567a7.jpg然后将图像变成图 block ,然后它应该如下所示: http://i1146.photobucket.com/albums/o525/walroid/replicate_example_zps5e5248e8.jpg在我的代码中,图片被保存到一个数组中,该数组被调用到方法中。我想要做的是复制该数组,然后将其放入另一个将复制图像的数组中。我怎么做?这是我的全部代码:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;

public class ImageProcessor {
public static void main(String[] args) {


if (args.length < 3) {
System.out.println("Not enough arguments");
System.exit(-1);
}
String function = args[0];
if (function.equals("-reflectV")) {
String inputFileName = args[1];
String outputFileName = args[2];

int[][] imageArr = readGrayscaleImage(inputFileName);
int[][] reflectedArr = reflectV(imageArr);

writeGrayscaleImage(outputFileName, reflectedArr);
} else if (function.equals("-reflectH")) {
String inputFileName = args[1];
String outputFileName = args[2];

int[][] imageArr = readGrayscaleImage(inputFileName);
int[][] reflectedArr = reflectH(imageArr);

writeGrayscaleImage(outputFileName, reflectedArr);
} else if (function.equals("-ascii")) {
String inputFileName = args[1];
String outputFileName = args[2];

int[][] imageArr = readGrayscaleImage(inputFileName);
int[][] reflectedArr = reflectV(imageArr);
try {
PrintStream output = new PrintStream(new File("output.txt"));
} catch (java.io.FileNotFoundException ex) {
System.out.println("Error: File Not Found");
System.exit(-1);
}
} else if (function.equals("-adjustBrightness")) {
String amount = args[1];
int a = Integer.parseInt(amount);
System.out.print(a)

String inputFileName = args[1];
String outputFileName = args[2];

int[][] imageArr = readGrayscaleImage(inputFileName);
int[][] brightnessArr = adjustBrightness(imageArr);

writeGrayscaleImage(outputFileName, brightnessArr);

} else
System.out.println("That is not a valid choice");
system.exit(-1)


public static int[][] reflectV ( int[][] arr){
int[][] reflected = new int[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
reflected[i][j] = arr[i][arr[i].length - 1 - j];
}
}

return reflected;
}

public static int[][] reflectH ( int[][] arr){
int[][] reflected = new int[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
reflected[j][i] = arr[i][arr[j].length - 1 - j];
}
}

return reflected;
}

public static int[][] adjustBrightness ( int[][] arr){
int[][] brightness = new int[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
RGB
}
}

return brightness;
}

public static int[][] readGrayscaleImage (String filename){
int[][] result = null; //create the array
try {
File imageFile = new File(filename); //create the file
BufferedImage image = ImageIO.read(imageFile);
int height = image.getHeight();
int width = image.getWidth();
result = new int[height][width]; //read each pixel value
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;
}


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); //create the image

//set all its pixel values based on values in the input array
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);
}
}

//write the image to a file
File imageFile = new File(filename);
ImageIO.write(image, "jpg", imageFile);
} catch (IOException ioe) {
System.err.println("Problems writing file named " + filename);
System.exit(-1);
}
}
}

最佳答案

您需要对数组进行“深度复制”。简单地将您的数组复制到一个新变量只会分配引用(浅拷贝),如果您操作其中一个数组中的数据,它会同时更改两个数组。

浅拷贝:

String[] myArray2 = myArray1;

这基本上有 2 个引用指向相同的数据。如果您在 myArray2 中更改任何内容,它也会在 myArray1 中更改。

深拷贝:

有多种方法可以进行深拷贝。显而易见的方法是遍历数组并将每个元素一次一个地复制到新数组中。

String[] myArray2 = new String[myArray1.length];
for (int i = 0; i < myArray1.length; i++) {

myArray2[i] = myArray1[i];

}

有时更简单/更快的方法是序列化您的数组,然后在它仍在内存中时反序列化它。这会导致 JVM 将反序列化数组视为一个全新的数组(可以说是“没有附加字符串”)。

这是我的一个旧项目的例子:

/**
* Clones an object creating a brand new
* object by value of input object. Accomplishes this
* by serializing the object, then deservializing it.
*
* @param obj Input Object to clone
* @return a new List<Product> type cloned from original.
* @throws IOException If IOException
* @throws ClassNotFoundException If ClassNotFoundException
*/
private static List<Product> cloneProdList(Object obj) throws IOException, ClassNotFoundException {

java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.io.ObjectOutputStream obj_out = new java.io.ObjectOutputStream(bos);
obj_out.writeObject(obj);

java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream(bos.toByteArray());
java.io.ObjectInputStream obj_in = new java.io.ObjectInputStream(bis);

@SuppressWarnings("unchecked")
List<Product> newObj = (List<Product>)obj_in.readObject();

bos.close();
bis.close();
obj_out.close();
obj_in.close();

return newObj;
}

此代码将 List 类型作为输入(好吧,它实际上转换为 Object 类型),序列化,然后在内存中反序列化,然后转换回 List 对象并从方法中返回新对象。

您可以很容易地将其修改为使用 Array 对象。 (array[] 类型会自动装箱到 Array 中)

关于java - 如何复制数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27070714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com