[7315] | 1 | package org.greenstone.gatherer.feedback;
|
---|
| 2 |
|
---|
| 3 | import java.io.*;
|
---|
| 4 | import javax.imageio.*;
|
---|
| 5 | import java.awt.*;
|
---|
| 6 | import java.awt.image.*;
|
---|
| 7 | import com.sun.image.codec.jpeg.*;
|
---|
| 8 | import javax.swing.*;
|
---|
| 9 |
|
---|
| 10 | /**
|
---|
| 11 | * This method will separate 1 image file into 2 image with the help of
|
---|
| 12 | * another file and sace the difference into a jpeg files.
|
---|
| 13 | * For example : We have 1 image file which is a screen shot and another image file
|
---|
| 14 | * is the screenshot with some line in it.
|
---|
| 15 | * This class will separate the screen shot and the line into 2 separates
|
---|
| 16 | * images.And then it will save the line into a jpeg files.
|
---|
| 17 | *
|
---|
| 18 | * @author Veronica Liesaputra
|
---|
| 19 | */
|
---|
| 20 | public class Separating
|
---|
| 21 | {
|
---|
| 22 | /**
|
---|
| 23 | * This is the BufferedImage of image inside the reference image file.
|
---|
| 24 | */
|
---|
| 25 | private BufferedImage bi;
|
---|
| 26 | /**
|
---|
| 27 | * This is the BufferedImage of image that we want to separate into
|
---|
| 28 | * 2 different image using the reference file.
|
---|
| 29 | */
|
---|
| 30 | private BufferedImage src_bi;
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * It will read whats inside the 2 given filenames and put it in the
|
---|
| 34 | * right BufferedImage.
|
---|
| 35 | * @param filename1 this is the file name of the reference image file.
|
---|
| 36 | * @param filename2 this is the file name of the file we want to separate into 2.
|
---|
| 37 | */
|
---|
| 38 | public Separating(String filename1,String filename2)
|
---|
| 39 | {
|
---|
| 40 | try
|
---|
| 41 | {
|
---|
| 42 | File f1 = new File(filename1);
|
---|
| 43 | File f2 = new File(filename2);
|
---|
| 44 | bi = ImageIO.read(f1);
|
---|
| 45 | src_bi = ImageIO.read(f2);
|
---|
| 46 | }
|
---|
| 47 | catch(IOException exp) {}
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * This method will get the difference that exist between bi and src_bi and
|
---|
| 52 | * will save the difference inside a jpeg file with the specified file name.
|
---|
| 53 | * @param file1 is the name of the file where we want to save the image.
|
---|
| 54 | */
|
---|
| 55 | public void separate(String file1)
|
---|
| 56 | {
|
---|
| 57 | BufferedImage bi1;
|
---|
| 58 |
|
---|
| 59 | int width = bi.getWidth();
|
---|
| 60 | int height = bi.getHeight();
|
---|
| 61 |
|
---|
| 62 | bi1 = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
|
---|
| 63 |
|
---|
| 64 | for (int y = 0 ; y < height ; y++)
|
---|
| 65 | {
|
---|
| 66 | for (int x = 0 ; x < width ; x++)
|
---|
| 67 | {
|
---|
| 68 | int rgb = bi.getRGB(x,y);
|
---|
| 69 | int src_rgb = src_bi.getRGB(x,y);
|
---|
| 70 |
|
---|
| 71 | if (src_rgb != rgb)
|
---|
| 72 | {
|
---|
| 73 | bi1.setRGB(x,y,rgb);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | save(bi1,file1);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * This method will save a BufferedImage to a jpeg file with the specified filename.
|
---|
| 83 | * @param b is the BufferedImage to be saved.
|
---|
| 84 | * @param filename is the name of the jpeg file.
|
---|
| 85 | */
|
---|
| 86 | public void save(BufferedImage b,String filename)
|
---|
| 87 | {
|
---|
| 88 | try
|
---|
| 89 | {
|
---|
| 90 | File file = new File(filename);
|
---|
| 91 | FileOutputStream out = new FileOutputStream(file);
|
---|
| 92 |
|
---|
| 93 | JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
|
---|
| 94 | JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(b);
|
---|
| 95 | param.setQuality(1.0f, false);
|
---|
| 96 | encoder.setJPEGEncodeParam(param);
|
---|
| 97 | encoder.encode(b);
|
---|
| 98 |
|
---|
| 99 | } catch (Exception ex) {}
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 |
|
---|