📄 picture.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package transformace;import java.awt.Color;import java.awt.Graphics;import java.awt.GraphicsConfiguration;import java.awt.GraphicsDevice;import java.awt.GraphicsEnvironment;import java.awt.HeadlessException;import java.awt.Image;import java.awt.Toolkit;import java.awt.Transparency;import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.awt.image.MemoryImageSource;import javax.swing.ImageIcon;/** * * @author xbalak00 */public class Picture { private Image image; private BufferedImage bufferedImage; int height = 0; int width = 0; public Picture(String cesta){ this.image = new ImageIcon(cesta).getImage(); this.bufferedImage = this.toBufferedImage(image); } public static BufferedImage toBufferedImage(Image image) { if (image instanceof BufferedImage) { return (BufferedImage) image; } image = new ImageIcon(image).getImage(); BufferedImage bimage = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); try { int transparency = Transparency.OPAQUE; GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); } catch (HeadlessException e) { } if (bimage == null) { int type = BufferedImage.TYPE_INT_RGB; bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); } Graphics g = bimage.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return bimage; } public double[][][] getRGB() { ColorModel colormodel = bufferedImage.getColorModel(); this.height = bufferedImage.getHeight(); this.width = bufferedImage.getWidth(); double[][][] rgb = new double[3][this.width][this.height]; for (int i = 0; i < this.width; i++) { for (int j = 0; j < this.height; j++) { rgb[0][i][j] = colormodel.getRed(bufferedImage.getRGB(i, j)); rgb[1][i][j] = colormodel.getGreen(bufferedImage.getRGB(i, j)); rgb[2][i][j] = colormodel.getBlue(bufferedImage.getRGB(i, j)); } } return (rgb); } public Image createImage(double[][][] rgb) { Image newImage = null; Color color; int pom = 0; int[] pixels = new int[this.width * this.height]; for (int j = 0; j < this.height; j++) { for (int i = 0; i < this.width; i++) { color = new Color((int) rgb[0][i][j], (int) rgb[1][i][j], (int) rgb[2][i][j]); pixels[pom] = color.getRGB(); pom++; } } Toolkit tk = Toolkit.getDefaultToolkit(); newImage = tk.createImage(new MemoryImageSource(this.width, this.height, pixels, 0, this.width)); return (newImage); } public double[][][] rgb2yuv(double[][][] rgb){ double[][][] yuv = new double[3][this.width][this.height]; for (int j = 0; j < this.height; j++) { for (int i = 0; i < this.width; i++) { yuv[0][i][j] = (0.299 * rgb[0][i][j]) + (0.587 * rgb[1][i][j]) + (0.114 * rgb[2][i][j]); yuv[1][i][j] = 0.492 * ( rgb[2][i][j] - yuv[0][i][j]); yuv[2][i][j] = 0.877 * ( rgb[0][i][j] - yuv[0][i][j]); } } return yuv; } public double[][][] yuv2rgb(double[][][] yuv){ double[][][] rgb = new double[3][this.width][this.height]; for (int j = 0; j < this.height; j++) { for (int i = 0; i < this.width; i++) { rgb[0][i][j] = yuv[0][i][j] + (1.140 * yuv[2][i][j]); rgb[1][i][j] = yuv[0][i][j] - (0.395 * yuv[1][i][j]) - (0.581 * yuv[2][i][j]); rgb[2][i][j] = yuv[0][i][j] + (2.032 * yuv[1][i][j]); if(rgb[0][i][j]>255)rgb[0][i][j]=255; if(rgb[1][i][j]>255)rgb[1][i][j]=255; if(rgb[2][i][j]>255)rgb[2][i][j]=255; } } return rgb; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -