如何复制数组?

时间:2014-11-21 21:31:06

标签: java arrays image processing replicate

我正在尝试做的是尝试拍摄图像并使其成为平铺图像。起始图像应如下所示。 http://i1146.photobucket.com/albums/o525/walroid/letter_Q_grayscale_zpsd3b567a7.jpg 然后将图像转换为图块然后它应该如下所示: 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);
        }
    }
}

3 个答案:

答案 0 :(得分:3)

您需要对数组执行“deep copy”。简单地将数组复制到一个新变量只会分配引用(shallow copy),如果你操纵其中一个数组中的数据,它将改变它们。

浅拷贝:

String[] myArray2 = myArray1;

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

深层复制:

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

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

    myArray2[i] = myArray1[i];

}

有时更简单/更快的方法是serialize您的数组,然后在它仍在内存中时对其进行反序列化。这会导致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类型),序列化然后反序列化,同时仍在内存中,然后转换回{{ 1}} object并将新对象返回到方法之外。

您可以轻松修改它以使用List对象。 (数组[]类型将自动装箱到Array为你)

答案 1 :(得分:1)

https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29

您可以在for循环中使用System.arraycopy从一个数组复制到另一个数组。

答案 2 :(得分:1)

使用Array类并调用静态方法Array.copyOf(array,array.length)非常方便,如果myArray1是前一个数组而myArray2是新数组,那么 myArray2 = Array.copyOf(myArray1, myArray1.length)