1

我正在尝试做的是我正在尝试拍摄图像并将其设为平铺图像。起始图像应如下所示。 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);
        }
    }
}
4

3 回答 3

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类型),在内存中进行序列化然后反序列化,然后转换回一个List对象并从方法中返回新对象。

您可以很容易地修改它以使用Array对象。(一个 array[] 类型会Array为你自动装箱)

于 2014-11-21T21:36:36.643 回答
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 从一个数组复制到另一个数组。

于 2014-11-21T21:40:08.830 回答
1

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

于 2014-11-21T22:07:59.380 回答