import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.nio.Buffer; //创建窗口来显示图片,添加按钮(目前还没用功能) public class ImageUI extends JFrame { public void showUI() { setTitle("美颜相机"); setSize(1200, 800); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); FlowLayout fl = new FlowLayout(); setLayout(fl); JButton button1 = new JButton("功能1"); button1.setBounds(20, 20, 100, 35); add(button1); } //创建画笔和二维数组,将图片的各像素值存入二维数组中,再用循环让画笔在窗口中画出 public void paint(Graphics g) { super.paint(g); int[][] imgArr = getImagePix("C:\\Users\\husheng\\Desktop\\dd1361becb3621b78892e1effac07b80.jpg"); int width = imgArr.length; int high = imgArr[0].length; for (int i = 0; i < width; i++) { for (int j = 0; j < high; j++) { int rgb = imgArr[i][j]; Color color = new Color(rgb); g.setColor(color); g.fillRect(100 + i, 100 + j, 1, 1); } } //循环读取二维数组,rgb变量存储数组中rgb值,用方法读取数中red,green,blue的值 再对颜色进行调节,达到渲染的目的。以下是反色代码 for (int i = 0; i < width; i += 1) { for (int j = 0; j < high; j += 1) { int rgb = imgArr[i][j]; Color color = new Color(rgb); int red = color.getRed(); int green = color.getGreen(); int blue = color.getBlue(); Color color1 = new Color(255 - red, 255 - green, 255 - blue); g.setColor(color1); g.fillRect(100 + i, 100 + j, 1, 1); } } } //创建二维数组,用file读取文件信息,并写方法读取图片中的像素值,最后存入二维数组中 public int[][] getImagePix(String imagePath) { File file = new File(imagePath); BufferedImage img = null; try { img = ImageIO.read(file); } catch (IOException e) { throw new RuntimeException(e); } int width = img.getWidth(); int high = img.getHeight(); int[][] imgSArr = new int[width][high]; for (int i = 0; i < width; i += 1) { for (int j = 0; j < high; j += 1) { imgSArr[i][j] = img.getRGB(i, j); } } return imgSArr; } public static void main(String[] args) { ImageUI imageUI = new ImageUI(); imageUI.showUI(); } }
【无标题】美颜相机的初步开发
最新推荐文章于 2024-09-09 15:52:04 发布