📄 balance.java
字号:
import java.util.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
public class Balance {
public static void main(String[] args) {
DrawFrame f = new DrawFrame();
}
}
class DrawFrame extends JFrame {
Container p;
drawpanel upleft;
drawpanelright upright;
//JPanel downleft;
//JPanel downright;
JPanel pup;
Image img;
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem menuitem = new JMenuItem("OPEN");
JMenuItem convert1 = new JMenuItem("convert");
class Filter extends javax.swing.filechooser.FileFilter {
String s;
Filter(String s) {
this.s = s;
}
public boolean accept(File fileobj) {
String extension = "";
if (fileobj.getPath().lastIndexOf('.') > 0)
extension =
fileobj
.getPath()
.substring(fileobj.getPath().lastIndexOf('.') + 1)
.toLowerCase();
return (fileobj.isDirectory() || extension.equals(s));
}
public String getDescription() {
return s.toUpperCase() + " Files(*." + s + ")";
}
}
public DrawFrame() {
p = this.getContentPane();
pup = new JPanel();
pup.setLayout(new GridLayout(1, 1));
upleft = new drawpanel();
//img=upleft.getNewImage();
upright = new drawpanelright();
//downleft=new JPanel();
//downright=new JPanel();
//img=this.getToolkit().getImage("F:\\drivers.bmp");
//upleft.getGraphics().drawImage(img,0,0,this);
menuitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
Filter bmp = new Filter("bmp");
Filter jpg = new Filter("jpg");
File filename;
c.addChoosableFileFilter(jpg);
c.addChoosableFileFilter(bmp);
int rVal = c.showOpenDialog(c);
if (rVal == JFileChooser.APPROVE_OPTION) {
filename = c.getSelectedFile();
upleft.setImage(filename);
img = upleft.getNewImage();
}
}
});
convert1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
upright.setImage(img, upleft.width, upleft.height);
}
});
menu.add(menuitem);
menu.add(convert1);
menubar.add(menu);
p.add(menubar, BorderLayout.NORTH);
pup.add(upleft);
pup.add(upright);
//pup.add(downleft);
//pup.add(downright);
p.add(pup, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(512, 590);
this.setVisible(true);
this.show();
}
}
class drawpanelright extends JPanel {
Image img;
int[] pixels;
int[] newpixels;
int[] rcolor = new int[256]; //灰度为I的象素的个数
double[] pr = new double[256];
int[] scolor = new int[256];
int[] stempcolor = new int[256];
int[] rrcolor = new int[256];
double[] ps = new double[256];
int width;
int height;
int color;
int newcolor;
Image newimg;
int i = 1;
boolean hasfile = false;
boolean candrawloc = false;
int staticx = 10;
int staticy;
public void setImage(Image img, int weight, int height) {
this.img = img;
hasfile = true;
this.width = weight;
this.height = height;
pixels = new int[width * height];
newpixels = new int[width * height];
staticy = height * 2;
PixelGrabber gg =
new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
try {
gg.grabPixels();
} catch (Exception e) {
}
int max = 0;
for (int i = 0; i < width * height; i++) {
int temp = pixels[i];
color = 0xff & temp;
if (color > max)
max = color;
rcolor[color]++;
// if(color>255) color=255;
//pixels[i]=0xff000000 | (color<<16)|(color<<8)|(color);
}
for (int i = 0; i < 256; i++) {
rrcolor[i] = 0;
pr[i] = (double) rcolor[i] / (double) (width * height);
ps[i] = pr[i];
if (i > 0)
ps[i] += ps[i - 1];
stempcolor[i] = (int) (ps[i] * 255);
System.out.println(
"number :"
+ i
+ " "
+ rcolor[i]
+ ":"
+ pr[i]
+ ":"
+ ps[i]
+ ": "
+ stempcolor[i]);
}
for (int i = 0; i < width * height; i++) {
int temp = pixels[i];
color = 0xff & temp;
newcolor = stempcolor[color];
rrcolor[newcolor]++;
newpixels[i] =
0xff000000 | (newcolor << 16) | (newcolor << 8) | (newcolor);
}
newimg =
this.createImage(
new MemoryImageSource(width, height, newpixels, 0, width));
i = 2;
this.candrawloc = true;
repaint();
}
public drawpanelright() {
}
public void paintComponent(Graphics g) {
if (hasfile) {
g.drawImage(newimg, 0, 0, this);
}
if (this.candrawloc) {
g.drawLine(staticx, staticy, staticx + 280, staticy);
g.drawLine(staticx + 280, staticy, staticx + 275, staticy + 5);
g.drawLine(staticx + 280, staticy, staticx + 275, staticy - 5);
g.drawLine(staticx, staticy, staticx, staticy - 200);
g.drawLine(staticx, staticy - 200, staticx + 5, staticy - 200 + 5);
g.drawLine(staticx, staticy - 200, staticx - 5, staticy - 200 + 5);
for (int i = 0; i < 256; i++) {
g.drawLine(
staticx + i-100,
staticy-100,
staticx + i-100,
staticy
- (int) ((rrcolor[i] / (float) (width * height))
* 10000));
}
}
}
}
class drawpanel extends JPanel {
Image img, newimg;
int[] pixels;
int width;
int height;
File bmpfile;
boolean hasnewfile = false;
int[] rcolor = new int[256];
int staticx;
int staticy;
boolean candrawloc = false;
public drawpanel() {
for (int i = 0; i < 256; i++)
rcolor[i] = 0;
}
public void setImage(File file) {
bmpfile = file;
Bmp graph = new Bmp(file);
img = graph.getImage();
width = graph.getWidth();
height = graph.getHeight();
staticx = 10;
staticy = height * 2;
pixels = graph.getPixels(img, width, height);
for (int i = 0; i < width * height; i++) {
int temp = 0xff & pixels[i];
rcolor[temp]++;
}
this.candrawloc = true;
newimg =
this.createImage(
new MemoryImageSource(width, height, pixels, 0, width));
this.hasnewfile = true;
repaint();
}
public Image getNewImage() {
return newimg;
}
public void paintComponent(Graphics g) {
if (this.hasnewfile) {
g.drawImage(newimg, 0, 0, this);
}
if (this.candrawloc) {
g.drawLine(staticx, staticy, staticx + 280, staticy);
g.drawLine(staticx + 280, staticy, staticx + 275, staticy + 5);
g.drawLine(staticx + 280, staticy, staticx + 275, staticy - 5);
g.drawLine(staticx, staticy, staticx, staticy - 200);
g.drawLine(staticx, staticy - 200, staticx + 5, staticy - 200 + 5);
g.drawLine(staticx, staticy - 200, staticx - 5, staticy - 200 + 5);
for (int i = 0; i < 256; i++) {
g.drawLine(
staticx + i,
staticy,
staticx + i,
staticy
- (int) ((rcolor[i] / (float) (width * height)) * 10000));
}
}
}
}
class Bmp {
/*loadbitmap originally written by Jeff West, and published at
*http://www.javaworld.com/javaworld/javatips/jw-javatip43.html
*modified slightly by Jeb Thorley.
*/
int width;
int height;
Image image;
public Bmp(File sfile) {
System.out.println("loading:" + sfile);
try {
FileInputStream fs = new FileInputStream(sfile);
int bflen = 14; // 14 byte BITMAPFILEHEADER
byte bf[] = new byte[bflen];
fs.read(bf, 0, bflen);
int bilen = 40; // 40-byte BITMAPINFOHEADER
byte bi[] = new byte[bilen];
fs.read(bi, 0, bilen);
// Interperet data.
int nsize =
(((int) bf[5] & 0xff) << 24)
| (((int) bf[4] & 0xff) << 16)
| (((int) bf[3] & 0xff) << 8)
| (int) bf[2]
& 0xff;
//System.out.println("File type is :"+(char)bf[0]+(char)bf[1]);
//System.out.println("Size of file is :"+nsize);
int nbisize =
(((int) bi[3] & 0xff) << 24)
| (((int) bi[2] & 0xff) << 16)
| (((int) bi[1] & 0xff) << 8)
| (int) bi[0]
& 0xff;
//System.out.println("Size of bitmapinfoheader is :"+nbisize);
int nwidth =
(((int) bi[7] & 0xff) << 24)
| (((int) bi[6] & 0xff) << 16)
| (((int) bi[5] & 0xff) << 8)
| (int) bi[4]
& 0xff;
//System.out.println("Width is :"+nwidth);
width = nwidth;
int nheight =
(((int) bi[11] & 0xff) << 24)
| (((int) bi[10] & 0xff) << 16)
| (((int) bi[9] & 0xff) << 8)
| (int) bi[8]
& 0xff;
//System.out.println("Height is :"+nheight);
height = nheight;
int nplanes = (((int) bi[13] & 0xff) << 8) | (int) bi[12] & 0xff;
//System.out.println("Planes is :"+nplanes);
int nbitcount = (((int) bi[15] & 0xff) << 8) | (int) bi[14] & 0xff;
//System.out.println("BitCount is :"+nbitcount);
// Look for non-zero values to indicate compression
int ncompression =
(((int) bi[19]) << 24)
| (((int) bi[18]) << 16)
| (((int) bi[17]) << 8)
| (int) bi[16];
//System.out.println("Compression is :"+ncompression);
int nsizeimage =
(((int) bi[23] & 0xff) << 24)
| (((int) bi[22] & 0xff) << 16)
| (((int) bi[21] & 0xff) << 8)
| (int) bi[20]
& 0xff;
//System.out.println("SizeImage is :"+nsizeimage);
int nxpm =
(((int) bi[27] & 0xff) << 24)
| (((int) bi[26] & 0xff) << 16)
| (((int) bi[25] & 0xff) << 8)
| (int) bi[24]
& 0xff;
//System.out.println("X-Pixels per meter is :"+nxpm);
int nypm =
(((int) bi[31] & 0xff) << 24)
| (((int) bi[30] & 0xff) << 16)
| (((int) bi[29] & 0xff) << 8)
| (int) bi[28]
& 0xff;
//System.out.println("Y-Pixels per meter is :"+nypm);
int nclrused =
(((int) bi[35] & 0xff) << 24)
| (((int) bi[34] & 0xff) << 16)
| (((int) bi[33] & 0xff) << 8)
| (int) bi[32]
& 0xff;
//System.out.println("Colors used are :"+nclrused);
int nclrimp =
(((int) bi[39] & 0xff) << 24)
| (((int) bi[38] & 0xff) << 16)
| (((int) bi[37] & 0xff) << 8)
| (int) bi[36]
& 0xff;
//System.out.println("Colors important are :"+nclrimp);
if (nbitcount == 24) {
// No Palatte data for 24-bit format but scan lines are
// padded out to even 4-byte boundaries.
int npad = (nsizeimage / nheight) - nwidth * 3;
//added for Bug correction
if (npad == 4) {
npad = 0;
}
int ndata[] = new int[nheight * nwidth];
byte brgb[] = new byte[(nwidth + npad) * 3 * nheight];
fs.read(brgb, 0, (nwidth + npad) * 3 * nheight);
int nindex = 0;
for (int j = 0; j < nheight; j++) {
for (int i = 0; i < nwidth; i++) {
ndata[nwidth * (nheight - j - 1) + i] =
(255 & 0xff)
<< 24
| (((int) brgb[nindex + 2] & 0xff) << 16)
| (((int) brgb[nindex + 1] & 0xff) << 8)
| (int) brgb[nindex]
& 0xff;
nindex += 3;
}
nindex += npad;
}
image =
Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(
nwidth,
nheight,
ndata,
0,
nwidth));
} else if (nbitcount == 8) {
// Have to determine the number of colors, the clrsused
// parameter is dominant if it is greater than zero. If
// zero, calculate colors based on bitsperpixel.
int nNumColors = 0;
if (nclrused > 0) {
nNumColors = nclrused;
} else {
nNumColors = (1 & 0xff) << nbitcount;
}
//System.out.println("The number of Colors is"+nNumColors);
// Some bitmaps do not have the sizeimage field calculated
// Ferret out these cases and fix 'em.
if (nsizeimage == 0) {
nsizeimage = ((((nwidth * nbitcount) + 31) & ~31) >> 3);
nsizeimage *= nheight;
//System.out.println("nsizeimage (backup) is"+nsizeimage);
}
// Read the palatte colors.
int npalette[] = new int[nNumColors];
byte bpalette[] = new byte[nNumColors * 4];
fs.read(bpalette, 0, nNumColors * 4);
int nindex8 = 0;
for (int n = 0; n < nNumColors; n++) {
npalette[n] =
(255 & 0xff)
<< 24
| (((int) bpalette[nindex8 + 2] & 0xff) << 16)
| (((int) bpalette[nindex8 + 1] & 0xff) << 8)
| (int) bpalette[nindex8]
& 0xff;
nindex8 += 4;
}
// Read the image data (actually indices into the palette)
// Scan lines are still padded out to even 4-byte
// boundaries.
int npad8 = (nsizeimage / nheight) - nwidth;
//System.out.println("nPad is:"+npad8);
int ndata8[] = new int[nwidth * nheight];
byte bdata[] = new byte[(nwidth + npad8) * nheight];
fs.read(bdata, 0, (nwidth + npad8) * nheight);
nindex8 = 0;
for (int j8 = 0; j8 < nheight; j8++) {
for (int i8 = 0; i8 < nwidth; i8++) {
ndata8[nwidth * (nheight - j8 - 1) + i8] =
npalette[((int) bdata[nindex8] & 0xff)];
nindex8++;
}
nindex8 += npad8;
}
image =
Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(
nwidth,
nheight,
ndata8,
0,
nwidth));
} else {
System.out.println(
"Not a 24-bit or 8-bit Windows Bitmap, aborting...");
image = (Image) null;
}
fs.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Caught exception in loadbitmap!");
}
}
public Image getImage() {
return image;
}
public int[] getPixels(Image parImage, int parWidth, int parHeight) {
int[] bitmap = new int[parWidth * parHeight];
PixelGrabber pg =
new PixelGrabber(
parImage,
0,
0,
parWidth,
parHeight,
bitmap,
0,
parWidth);
try {
pg.grabPixels();
} catch (InterruptedException e) {
e.printStackTrace();
}
return bitmap;
}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -