CFプレイ時に公式サイトでマウススライドで画像切り替えを制御するもの

22793 ワード


 
以前CFを游んでいた时、公式サイトにマウスが入って左右に2枚の画像を移动して切り替えることができて、とてもきれいに见えました.
デジタル画像処理の授業ではBMP画像を処理し、C++を使う必要があるので、この機会にBMPを専門に理解し、画像がテキストと異なる2進法ファイルを理解し、C++を理解した.
その後はBMP画像の情報を1バイトずつテストして、画像を切り替えることができるものを作って終わりました.
BMP自身のテスト理解では、上位54バイトはビットマップ情報であるファイルのいくつかの情報であり、その後、画像の深さが24ビット未満のカラーパレットがあればカラークエリーとも呼ばれ、その後はインデックスを格納する配列であり、24ビットのものはカラーパレットがないものであり、各画素のB、G、R、の配列を直接3バイトで保存する.専門的に共有されているドキュメントがあります.
 
テストコードは次のとおりです.
/**
		 * 14      :     
		 */
		
		System.out.println("   :  " + (char) dis.readByte());
		System.out.println("   ( 2  ):  " + (char) dis.readByte());

		//       windows            
		System.out.println("    4  ---   :  " + dis.readByte());
		System.out.println("    4  ---    : " + dis.readByte());
		System.out.println("    4  ---    : " + dis.readByte());
		System.out.println("    4  ---   :  " + dis.readByte());
		System.out.println("      --           :      525430");

		System.out.println("      --      : " + dis.readInt());

		System.out.println("    4  ---   :  " + dis.readByte());
		System.out.println("    4  ---    : " + dis.readByte());
		System.out.println("    4  ---    : " + dis.readByte());
		System.out.println("    4  ---   :  " + dis.readByte());
		System.out.println("      --           :      118  ");
		// 16     64  +   14+     40=118
		/**
		 * 40        
		 */
		System.out.println("        4  ---   :  " + dis.readByte());
		System.out.println("        4  ---    : " + dis.readByte());
		System.out.println("        4  ---    : " + dis.readByte());
		System.out.println("        4  ---   :  " + dis.readByte());
		System.out.println("     --         :            40  ");
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("     --         :            1366  ");
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("     --         :            768  ");
		System.out.println("     --   2     ---    :" + dis.readByte());
		System.out.println("     --   2    ---    :" + dis.readByte());
		System.out.println("     --             :           1");

		System.out.println("     --    2  :---   :" + dis.readByte());
		System.out.println("     --    2  :---   :" + dis.readByte());
		System.out.println("     --             :           4");

		System.out.println("     --    4  :---   " + dis.readByte());
		System.out.println("     --    4  :---   " + dis.readByte());
		System.out.println("     --    4  :---   " + dis.readByte());
		System.out.println("     --    4  :---   " + dis.readByte());
		System.out.println("4   0     :-------   ");

		System.out.println("            4  ---   :  " + dis.readByte());
		System.out.println("            4  ---   :  " + dis.readByte());
		System.out.println("            4  ---   :  " + dis.readByte());
		System.out.println("            4  ---   :  " + dis.readByte());
		System.out.println("                   525312+     +     +   =    ");

		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("                    :  4 0 ");

		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("                    :4 0   ");

		System.out.println("              4  ---   :  " + dis.readByte());
		System.out.println("              4  ---   :  " + dis.readByte());
		System.out.println("              4  ---   :  " + dis.readByte());
		System.out.println("              4  ---   :  " + dis.readByte());
		System.out.println("                4 0");

		System.out.println("                4  ---   :  " + dis.readByte());
		System.out.println("                4  ---   :  " + dis.readByte());
		System.out.println("                4  ---   :  " + dis.readByte());
		System.out.println("                4  ---   :  " + dis.readByte());
		System.out.println("                4 0          ");

 
 
 
上記は24ビット前の54バイトの情報テストで、ファイルサイズは4バイトで、int 1の場合、画像に格納されているデータは00000000億1,000,000,000,000,000,000バイト単位で逆順に排出されます.
 
次に、深さ24ビットのBMPピクチャ切替コードを示します.
 
package com.wlh.BMPPhoto;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.io.*;

import javax.swing.*;

public class BMPPhoto_1_24 extends JFrame {

	int width = 1366;
	int height = 768;
	//   rgb  
	int[][] R=new int[height][width];
	int[][] G=new int[height][width];
	int[][] B=new int[height][width];
	
	int[][] R1=new int[height][width];
	int[][] G1=new int[height][width];
	int[][] B1=new int[height][width];


	int count = 0;
	int count1=0;
	//               paint      
	boolean state=false;
	private Graphics gra;
    int myTest_X=1366; 
	//     
	public BMPPhoto_1_24() {
		this.setTitle("BMP  ");
		this.setSize(1366, 760);
		this.setVisible(true);
		this.setAlwaysOnTop(true);
		this.setDefaultCloseOperation(3);
		gra = this.getGraphics();
		
	

	}

	public static void main(String args[]) {
		BMPPhoto_1_24 bmp = new BMPPhoto_1_24();
		try {
			bmp.readBMP();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	//   BMP     
	public void readBMP() throws IOException {

		InputStream in = new FileInputStream("F:\\1.bmp");
		DataInputStream dis = new DataInputStream(in);
		
		InputStream in_1 = new FileInputStream("F:\\22.bmp");
		DataInputStream dis_1 = new DataInputStream(in_1);
		/**
		 * 14      :     
		 */
		
		System.out.println("   :  " + (char) dis.readByte());
		System.out.println("   ( 2  ):  " + (char) dis.readByte());

		//       windows            
		System.out.println("    4  ---   :  " + dis.readByte());
		System.out.println("    4  ---    : " + dis.readByte());
		System.out.println("    4  ---    : " + dis.readByte());
		System.out.println("    4  ---   :  " + dis.readByte());
		System.out.println("      --           :      525430");

		System.out.println("      --      : " + dis.readInt());

		System.out.println("    4  ---   :  " + dis.readByte());
		System.out.println("    4  ---    : " + dis.readByte());
		System.out.println("    4  ---    : " + dis.readByte());
		System.out.println("    4  ---   :  " + dis.readByte());
		System.out.println("      --           :      118  ");
		// 16     64  +   14+     40=118
		/**
		 * 40        
		 */
		System.out.println("        4  ---   :  " + dis.readByte());
		System.out.println("        4  ---    : " + dis.readByte());
		System.out.println("        4  ---    : " + dis.readByte());
		System.out.println("        4  ---   :  " + dis.readByte());
		System.out.println("     --         :            40  ");
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("     --         :            1366  ");
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("          4  ---   :  " + dis.readByte());
		System.out.println("     --         :            768  ");
		System.out.println("     --   2     ---    :" + dis.readByte());
		System.out.println("     --   2    ---    :" + dis.readByte());
		System.out.println("     --             :           1");

		System.out.println("     --    2  :---   :" + dis.readByte());
		System.out.println("     --    2  :---   :" + dis.readByte());
		System.out.println("     --             :           4");

		System.out.println("     --    4  :---   " + dis.readByte());
		System.out.println("     --    4  :---   " + dis.readByte());
		System.out.println("     --    4  :---   " + dis.readByte());
		System.out.println("     --    4  :---   " + dis.readByte());
		System.out.println("4   0     :-------   ");

		System.out.println("            4  ---   :  " + dis.readByte());
		System.out.println("            4  ---   :  " + dis.readByte());
		System.out.println("            4  ---   :  " + dis.readByte());
		System.out.println("            4  ---   :  " + dis.readByte());
		System.out.println("                   525312+     +     +   =    ");

		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("                    :  4 0 ");

		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("           4  ---   :  " + dis.readByte());
		System.out.println("                    :4 0   ");

		System.out.println("              4  ---   :  " + dis.readByte());
		System.out.println("              4  ---   :  " + dis.readByte());
		System.out.println("              4  ---   :  " + dis.readByte());
		System.out.println("              4  ---   :  " + dis.readByte());
		System.out.println("                4 0");

		System.out.println("                4  ---   :  " + dis.readByte());
		System.out.println("                4  ---   :  " + dis.readByte());
		System.out.println("                4  ---   :  " + dis.readByte());
		System.out.println("                4  ---   :  " + dis.readByte());
		System.out.println("                4 0          ");

		/**
		 *           width=1366     24 bmp           2   
		 */

		//          
		int b;
		int g;
		int r;
		for (int i = height - 1; i >= 0; i--) {
			for (int j = 0; j < width; j++) {
				count++;

				//    1366                ======   1366         
				if (count % 1366 == 0) {
					dis.readByte();
					dis.readByte();
				}

				//  RGB
				b = dis.readByte();
				g = dis.readByte();
				r = dis.readByte();
				
				if(b<0){
					b = 256 + b;
				}
					
				if(g<0){
					g= 256 + g;
				}
				
				if(r<0){
					r=256+r;
				}
				B[i][j]=b;
				G[i][j]=g;
				R[i][j]=r; 
				//  JFrame 
				this.gra.setColor(new Color(r, g, b));
				this.gra.drawLine(j, i, j, i);
			}
			
		}
		
		dis_1.skip(54);
		for (int i = height - 1; i >= 0; i--) {
			for (int j = 0; j < width; j++) {
				count1++;

				//    1366                ======   1366         
				if (count1 % 1366 == 0) {
					dis_1.readByte();
					dis_1.readByte();
				}

				//  RGB
				b = dis_1.readByte();
				g = dis_1.readByte();
				r = dis_1.readByte();
				
				if(b<0){
					b = 256 + b;
				}
					
				if(g<0){
					g= 256 + g;
				}
				
				if(r<0){
					r=256+r;
				}
				B1[i][j]=b;
				G1[i][j]=g;
				R1[i][j]=r; 
				
			}
		}
			state=true;
System.out.println("        ......");
this.addMouseMotionListener(new MouseAdapter(){
	
	public void mouseMoved(MouseEvent e){
		if(state){
	    int x=e.getX();
	    System.out.println("   X:="+x);
	    if(x<myTest_X){
	    	for(int i=0;i<height;i++){
	    		for(int j=x;j<myTest_X;j++){
	    			gra.setColor(new Color(R1[i][j], G1[i][j], B1[i][j]));
	    			gra.drawLine(j, i, j, i);
	    		}
	    	}
	    }else{
	    	for(int i=0;i<height;i++){
	    		for(int j=myTest_X;j<x;j++){
	    			gra.setColor(new Color(R[i][j], G[i][j], B[i][j]));
	    			gra.drawLine(j, i, j, i);
	    		}
	    	}
	    }
	    
	    myTest_X=x;
		}
	}
});	
	}

	public void paint(Graphics g){
		if(state){
		for(int i=0;i<height;i++){
			for(int j=0;j<width;j++){
				gra.setColor(new Color(R[i][j], G[i][j], B[i][j]));
				gra.drawLine(j, i, j, i);
			}
		}
	}
	}

}

 
以下は深さが8ビットで、24ビットとは異なり、ファイルのパレット情報を先に読み取り、配列内のインデックスに基づいてBGRを表示する必要があります.
package com.wlh.BMPPhoto;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;

public class BMPPhoto_1_256 extends JFrame {
	/**
	 *      8  BMP    
	 */

	//   256     
	BMPColor[] colorSelector_1 = new BMPColor[256];
	BMPColor[] colorSelector_2 = new BMPColor[256];
	//                
	//   1
	int weith_1 = 1048;//                      
	int height_1 = 468;//           =     
	int[][] data_1 = new int[height_1][weith_1];
	//   2
	int weith_2 = weith_1;
	int height_2 = height_1;
	int[][] data_2 = new int[height_2][weith_2];
	//          paint                   
	boolean state = false;

	//  
	private Graphics G;
	//             
	private int myText_X=weith_1;
	public static void main(String[] args) {
		BMPPhoto_1_256 bmp = new BMPPhoto_1_256();
	}

	public BMPPhoto_1_256() {

		this.setTitle("BMP     8   ");
		this.setAlwaysOnTop(true);
		this.setSize(1050, 470);
		this.setDefaultCloseOperation(3);
		this.setVisible(true);

		try {
			//          
			InputStream in_1 = new FileInputStream("F:\\b1.bmp");
			DataInputStream dis_1 = new DataInputStream(in_1);
			

			/**
			 *        
			 */
			//                    

			dis_1.skipBytes(54);
			for (int i = 0; i < 256; i++) {
				/**
				 *       
				 */
				colorSelector_1[i] = new BMPColor();
				int b = dis_1.readByte();
				int g = dis_1.readByte();
				int r = dis_1.readByte();
				dis_1.readByte();

				if (g < 0) {
					//     DataInputStream   readByte     -128——127        
					g += 256;
					colorSelector_1[i].g = g;
				} else {
					colorSelector_1[i].g = g;
				}
				
				if (r < 0) {
					r += 256;
					colorSelector_1[i].r = r;
				} else {
					colorSelector_1[i].r = r;
				}

				if (b < 0) {
					b += 256;
					colorSelector_1[i].b = b;
				} else {
					colorSelector_1[i].b = b;
				}

			}
			//         
			for (int i = data_1.length - 1; i > 0; i--) {// 468  

				for (int j = 0; j < data_1[i].length; j++) {// 1048 
					byte index = dis_1.readByte();
					if (index < 0) {
						//                     
						data_1[i][j] = index + 256;
					} else {
						data_1[i][j] = index;
					}

				}
			}
			state = true;
			//      JFrame 
			 G = this.getGraphics();
			BMPColor bmpCol = new BMPColor();
			for (int i = 0; i < data_1.length; i++) {
				for (int j = 0; j < data_1[i].length; j++) {
					//                 
					bmpCol = colorSelector_1[data_1[i][j]];
					G.setColor(new Color(bmpCol.getR(), bmpCol.getG(), bmpCol
							.getB()));
					G.drawLine(j, i, j, i);

				}
			}
			
			/**
			 *         
			 */
			InputStream in_2 = new FileInputStream("F:\\b2.bmp");
			DataInputStream dis_2 = new DataInputStream(in_2);
			
			dis_2.skipBytes(54);
			for (int i = 0; i < 256; i++) {
				/**
				 *       
				 */
				colorSelector_2[i] = new BMPColor();
				
				
				int b = dis_2.readByte();
				int g = dis_2.readByte();
				int r = dis_2.readByte();
				dis_2.readByte();

				if (g < 0) {
					g += 256;
					colorSelector_2[i].g = g;
				} else {
					colorSelector_2[i].g = g;
				}
				
				if (r < 0) {
					r += 256;
					colorSelector_2[i].r = r;
				} else {
					colorSelector_2[i].r = r;
				}

				if (b < 0) {
					b += 256;
					colorSelector_2[i].b = b;
					
				} else {
					colorSelector_2[i].b = b;
					
				}

				

			}
			//         
			for (int i = data_2.length - 1; i > 0; i--) {// 468  

				for (int j = 0; j < data_2[i].length; j++) {// 1048 
					byte index = dis_2.readByte();
					if (index < 0) {
						data_2[i][j] = index + 256;
					} else {
						data_2[i][j] = index;
					}

				}
			}
			
			System.out.println("          ........");
			

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		this.addMouseMotionListener(new MouseAdapter(){
			
			 public void mouseMoved(MouseEvent e) {
				 System.out.println("@@@@@@@@@@@@@@");
				 if(state){
					
					 int x=e.getX();
					 System.out.println("Enter:   X="+myText_X);
					 //  x  X     
					 //  
					 BMPColor bmpColor;
					 if(x<myText_X){
					 for(int i=0;i<data_2.length;i++){
						 for(int j=x;j<myText_X;j++){
							 bmpColor=colorSelector_2[data_2[i][j]];
							 G.setColor(new Color(bmpColor.r, bmpColor.g, bmpColor.b));
							 G.drawLine(j, i, j, i);
						 }
					 }
					 }else{
						 //  
						 for(int i=0;i<data_1.length;i++){
							 for(int j=myText_X;j<x;j++){
								 bmpColor=colorSelector_1[data_1[i][j]];
								 G.setColor(new Color(bmpColor.r, bmpColor.g, bmpColor.b));
								 G.drawLine(j, i, j, i);
							 }
						 } 
					 }
					
					 
					 myText_X=e.getX();
				 }
				 
				 
			 }
		});
	}

	class BMPColor {
		int b = 0;
		int g = 0;
		int r = 0;
		int a = 0;

		public int getB() {
			return b;
		}

		public int getG() {
			return g;
		}

		public int getR() {
			return r;
		}

		public int getA() {
			return a;
		}

		public void set_B(int b) {
			if (b < 0) {
				b += 256;
			}
			this.b = b;
		}

		public void set_G(int g) {
			if (g < 0) {
				g += 256;
			}
			this.g = g;
		}

		public void set_R(int r) {
			if (r < 0) {
				r += 256;
			}
			this.r = r;
		}

		public void set_A(int a) {
			if (a < 0) {
				a += 256;
			}
			this.a = a;
		}

		
		
		
	}

	public void paint(Graphics g) {
		if (state) {
			BMPColor bmpCol = new BMPColor();
			for (int i = 0; i < data_1.length; i++) {
				for (int j = 0; j < data_1[i].length; j++) {
					
						//                 
						bmpCol = colorSelector_1[data_1[i][j]];
						g.setColor(new Color(bmpCol.getR(), bmpCol.getG(),
								bmpCol.getB()));
						g.drawLine(j, i, j, i);
					
				}
			}
		}
	}
}