java - 如何复制数组?

标签 java arrays image processing replicate

我想做的是尝试拍摄图像并将其制成平铺图像。起始图像应如下所示。 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/

相关文章:

Java 执行无法使用 -classpath

java - 如何在 Delphi 对象或字符串中隐式使用 ToString()?

java - 带有 "and"/"or"JAVA 的多变量 Switch 语句

javascript - Flex/JavaScript 中的图像编辑器组件

html - 图片 :first-child not working for my first image

使用java读取json文件时出现java.lang.ClassCastException

javascript - 如何使属性动态化(在执行代码之前未知)?

c - 将指针数组结构传递给函数

C - 从特定的大范围中获取素数

javascript - NodeJS 将 Base64 字符串转换为 jpeg 图像 - NodeJS 或 mysql