有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在普通java编码中插入图像

import java.io.*;
import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.image.*;
import java.awt.Image;



class comparision{

  public static void main(String args[]) throws IOException{
long start = System.currentTimeMillis();
    File file= new File("C:\\Users\\Asus\\Desktop\\projek\\projekcodingjava\\JavaApplication22\\src\\training_1_D.png");



    BufferedImage image = ImageIO.read(file);
 int width = image.getWidth(null);
    int height = image.getHeight(null);
int[][] clr=  new int[width][height];
File files= new File("C:\\Users\\Asus\\Desktop\\projek\\projekcodingjava\\JavaApplication22\\src\\training_1_DB.png");
    BufferedImage images = ImageIO.read(files);
 int widthe = images.getWidth(null);
    int heighte = images.getHeight(null);
int[][] clre=  new int[widthe][heighte];
int smw=0;
int smh=0;
int p=0;


//CALUCLATING THE SMALLEST VALUE AMONG WIDTH AND HEIGHT
if(width>widthe){ smw =widthe;}
else {smw=width;}
if(height>heighte){smh=heighte;}
else {smh=height;}



//CHECKING NUMBER OF PIXELS SIMILARITY
for(int a=0;a<smw;a++){
for(int b=0;b<smh;b++){
clre[a][b]=images.getRGB(a,b);
clr[a][b]=image.getRGB(a,b);
if(clr[a][b]==clre[a][b]) {
    p=p+1;
/*              { //to set if color match between two image (image A and Image B), appear BLACK
                    red = 0;
                         green = 0;
                            blue = 0;

                }
                else
                { //to set if color dont match between two image, appear WHITE
                    red = 255;
                            green = 255;
                                blue = 255;
                }

                pixel_data[color++] = (red << 16) | (green << 8 ) | blue);*/
}
}}

float w,h=0;
if(width>widthe) {w=width;}
else {w=widthe;}
if(height>heighte){ h = height;}
else{h = heighte;}
float s = (smw*smh);
//CALUCLATING PERCENTAGE
float x =(100*p)/s;

System.out.println("THE PERCENTAGE SIMILARITY IS APPROXIMATELY ="+x+"%");
long stop = System.currentTimeMillis();
System.out.println("TIME TAKEN IS ="+(stop-start));

  }

   //I WANT TO APPEAR THE IMAGE HERE. RETURN BLACK PIXEL IF PIXEL MATCH BETWEEN TWO IMAGE (IMAGE A AND IMAGE B) AND RETURN WHITE IF PIXEL DOSNT MATCH



}

我想在这个编码的输出中插入图片。。 该编码计算比较百分比&;我想在百分比输出下面添加图像

该图像将由以下规则生成: 1.如果两幅图像的比较匹配,将出现黑色像素。 2.如果两幅图像的比较不匹配,则会出现白色像素

如何使用java插入由该规则生成的图像


共 (1) 个答案

  1. # 1 楼答案

    以下是一个函数,您可以将图像文件的路径传递到该函数,以将其输出:-

    // the imports required
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.*;
    import java.awt.*;
    
    //paste this function in your class and call it when you want to display image
    
    public void showImage(String path_to_image)
    {
        JFrame frame = new JFrame("Image");
        frame.setLayout(new BorderLayout());
        frame.setResizable(false);
        ImageIcon myImage = new ImageIcon(path_to_image);
        frame.getContentPane().add(new JLabel(myImage));
    
        frame.pack();
        frame.setVisible(true);
    }
    

    拍摄两张黑白图像,然后在最后显示(//我想在这里显示图像。如果两张图像(图像A和图像B)之间的像素匹配,则返回黑色像素;如果像素不匹配,则返回白色)。放置一条检查匹配百分比的IF语句,即:

    if(x == 100) // means 100 % match as per your code
    {
      showImage("Black.png"); // path to black pic
    }
    else
    {
      showImage("White.png"); // path to white pic
    }