j2me 在手机上,截屏你的程序

收藏起来:http://www.iteye.com/topic/702252

 

KJAVA在这里人气不算很旺,不过这磨灭不了我们这群热爱着J2ME开发的人们 的激情。

 

关于这个截屏程序,是需要附加到你的项目中的,因为j2me的手机没听说双线程的。

 

此截屏程序的原理:

把缓冲画笔所在的画布img_buffer转成PNG格式保存到指定路径下,image转png需要一些插值加文件头的操作。

 

这段代码网上有,我也是copy过来进行创新的。

 

你需要做的是:

1.在你的canvas里定义截屏类的对象,初始值null。

2.在你的canvas里定义一个boolean变量,初始值true。

3.在你的canvas里的初始化或者构造方法里new 一个截屏类的对象。

4.在你的canvas里run方法里如同下面代码一样改造,repaint()方法就是重绘方法,c就是截屏类的实例,c.save()方法是保 存图片的方法。

Java代码
  1. public   void  run() {  
  2.         while  ( true ) {  
  3.             startTime = System.currentTimeMillis();  
  4. //          if(b_isPaint)   
  5. //          {   
  6.                 repaint();  //重绘   
  7. //          }   
  8. //          else   
  9. //          {   
  10. //              mainForm.paint(gg);   
  11. //              c.save(img_buffer); //c是截屏类实例,save方法是保存 图片方法,img_buffer是当前缓冲。   
  12. //              b_isPaint = true;   
  13. //          }   
  14.             timeTaken = System.currentTimeMillis() - startTime;  
  15.             if  (timeTaken <  1000  / DefaultProperties.FPS) {  
  16.                 try  {  
  17.                     Thread.sleep(1000  / DefaultProperties.FPS - timeTaken);  
  18.                 } catch  (Exception e) {  
  19.                     e.printStackTrace();  
  20.                 }  
  21.             }  
  22.         }  
  23.     }  
public void run() {
		while (true) {
			startTime = System.currentTimeMillis();
//			if(b_isPaint)
//			{
				repaint();	//重绘
//			}
//			else
//			{
//				mainForm.paint(gg);
//				c.save(img_buffer);	//c是截屏类实例,save方法是保存图片方法,img_buffer是当前缓冲。
//				b_isPaint = true;
//			}
			timeTaken = System.currentTimeMillis() - startTime;
			if (timeTaken < 1000 / DefaultProperties.FPS) {
				try {
					Thread.sleep(1000 / DefaultProperties.FPS - timeTaken);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

 

5.你想要以何种触发事件触发这个截屏方法,如果想要触摸屏点击触发,参考以下方法。其他触发方法照之改之。

Java代码
  1.      protected   void  pointerPressed( int  x,  int  y) {  
  2. //      if(x < 30 && x > 0 && y < 30 && y > 0)   
  3. //      {   
  4. //          b_isPaint = false;   
  5. //          color = 0;   
  6. //      }   
  7. //      if(b_isPaint)   
  8.             mainForm.pointerPressed(x, y);//程序主触屏事件   
  9.     }  
	protected void pointerPressed(int x, int y) {
//		if(x < 30 && x > 0 && y < 30 && y > 0)
//		{
//			b_isPaint = false;
//			color = 0;
//		}
//		if(b_isPaint)
			mainForm.pointerPressed(x, y);//程序主触屏事件
	}

 

6.在你的程序里添加截屏类(CGAMG.java)  第45行写你要保存的路径。

Java代码
  1. import  javax.microedition.lcdui.*;  
  2. import  java.io.*;  
  3. import  javax.microedition.io.file.FileConnection;  
  4. import  javax.microedition.io.Connector;  
  5.   
  6. public   class  CGame  extends  Canvas {  
  7.     public   void  paint(Graphics g)  
  8.     {  
  9.     }  
  10.     //Image2Bytes by AnderLu   
  11.     //生成的byte[]数组可直接用于外部存储为.png格式的图片文件看图软件可直接打开   
  12.     public   static   int  IDATPOS;  
  13.     public   static   int  haha =  0 ;  
  14.     public   static   byte [] HEADChunk = {  
  15.                                      (byte0x89 , ( byte0x50 ,  
  16.                                      (byte0x4E , ( byte0x47 ,  
  17.                                      (byte0x0D , ( byte0x0A ,  
  18.                                      (byte0x1A , ( byte0x0A ,  
  19.     };  
  20.     public   static   byte [] tRNSChunk = {  
  21.                                      (byte0x00 , ( byte0x00 ,  
  22.                                      (byte0x00 , ( byte0x01 ,  
  23.                                      (byte0x74 , ( byte0x52 ,  
  24.                                      (byte0x4E , ( byte0x53 ,  
  25.                                      (byte0x00 ,  
  26.                                      (byte0x40 , ( byte0xE6 ,  
  27.                                      (byte0xD8 , ( byte0x66 ,  
  28.     };  
  29.     public   static   byte [] IENDChunk = {  
  30.                                      //PNGIEND   
  31.                                      (byte0x00 , ( byte0x00 ,  
  32.                                      (byte0x00 , ( byte0x00 ,  
  33.                                      (byte0x49 , ( byte0x45 ,  
  34.                                      (byte0x4E , ( byte0x44 ,  
  35.                                      (byte0xAE , ( byte0x42 ,  
  36.                                      (byte0x60 , ( byte0x82   
  37.     };  
  38.     Image img;  
  39.     public   void  save(Image im)  
  40.     {  
  41.         byte  data[] = Image2Bytes(im);  
  42.         this .img = Image.createImage(data,  0 , data.length);  
  43.   
  44.           
  45.         saveFile("file:///E:/jieping/"  + haha + ".png" , data);   //路径,haha变量递增 保存名为 1 .png, 2 .png  
  46.         haha++;  
  47.     }  
  48.   
  49.   
  50.     /**保存文件  
  51.      * @path:路径  
  52.      * @fileData:文件数据  
  53.      * @return: 0:出现异常,1:保存成功  
  54.      */   
  55.     public   int  saveFile(String path,  byte [] fileData) {  
  56.         FileConnection fc = null ;  
  57.         try  {  
  58.             fc = (FileConnection) Connector.open(path, Connector.READ_WRITE);  
  59.             if  (!fc.exists()) {  
  60.                 fc.create();  
  61.             }  
  62.             OutputStream os = fc.openOutputStream();  
  63.             os.write(fileData);  
  64.             os.flush();  
  65.             os.close();  
  66.             fc.close();  
  67.             return   1 ;  
  68.   
  69.         } catch  (IOException ex) {  
  70.             ex.printStackTrace();  
  71.             return   0 ;  
  72.         }  
  73.     }  
  74.   
  75.     public   byte [] Image2Bytes(Image img) {  
  76.         try  {  
  77.             int  w = img.getWidth();  
  78.             int  h = img.getHeight();  
  79.             int  offset =  0 ;  
  80.             byte  buffer[] =  new   byte [(w *  4  +  1 ) * h + offset];  
  81.             getImageBufferForImageARGB8888(img, buffer, w, h, offset);  
  82.             System.gc();  
  83.             ByteArrayOutputStream baos = new  ByteArrayOutputStream();  
  84.             DataOutputStream dout = new  DataOutputStream(baos);  
  85.             WritePng(dout, w, h, buffer, nullfalse , offset);  
  86.             byte [] data = baos.toByteArray();  
  87.             writeCRC(data, 8 );  //更新 IHDR CRC   
  88.             writeCRC(data, 33 );  //更新 PLTE CRC   
  89.             writeCRC(data, IDATPOS); //更新IDAT CRC   
  90.             buffer = null ;  
  91.             System.gc();  
  92.             return  data;  
  93.         } catch  (IOException ex) {  
  94.             ex.printStackTrace();  
  95.             return   null ;  
  96.         }  
  97.     }  
  98.   
  99.     public   static   void  writeCRC( byte [] data,  int  chunkpos) {  
  100.         int  chunklen = ((data[chunkpos] &  0xFF ) <<  24 )  
  101.                        | ((data[chunkpos + 1 ] &  0xFF ) <<  16 )  
  102.                        | ((data[chunkpos + 2 ] &  0xFF ) <<  8 )  
  103.                        | (data[chunkpos + 3 ] &  0xFF );  
  104.   
  105.         int  sum = CRCChecksum(data, chunkpos +  44  + chunklen) ^  0xffffffff ;  
  106.         int  val = sum;  
  107.         int  pos = chunkpos +  8  + chunklen;  
  108.         data[pos] = (byte ) ((val &  0xFF000000 ) >>  24 );  
  109.         data[pos + 1 ] = ( byte ) ((val &  0xFF0000 ) >>  16 );  
  110.         data[pos + 2 ] = ( byte ) ((val &  0xFF00 ) >>  8 );  
  111.         data[pos + 3 ] = ( byte ) (val &  0xFF );  
  112.     }  
  113.   
  114.     public   static   int [] crc_table;  //CRC 表   
  115.     public   static   int  CRCChecksum( byte [] buf,  int  off,  int  len) {  
  116.         int  c =  0xffffffff ;  
  117.         int  n;  
  118.         if  (crc_table ==  null ) {  
  119.             int  mkc;  
  120.             int  mkn, mkk;  
  121.             crc_table = new   int [ 256 ];  
  122.             for  (mkn =  0 ; mkn <  256 ; mkn++) {  
  123.                 mkc = mkn;  
  124.                 for  (mkk =  0 ; mkk <  8 ; mkk++) {  
  125.                     if  ((mkc &  1 ) ==  1 ) {  
  126.                         mkc = 0xedb88320  ^ (mkc >>>  1 );  
  127.                     } else  {  
  128.                         mkc = mkc >>> 1 ;  
  129.                     }  
  130.                 }  
  131.                 crc_table[mkn] = mkc;  
  132.             }  
  133.         }  
  134.         for  (n = off; n < len + off; n++) {  
  135.             c = crc_table[(c ^ buf[n]) & 0xff ] ^ (c >>>  8 );  
  136.         }  
  137.         return  c;  
  138.     }  
  139.   
  140.     public   static   long  adler32( long  adler,  byte [] buf,  int  index,  int  len) {  
  141.         int  BASE =  65521 ;  
  142.         int  NMAX =  5552 ;  
  143.         //TODO remove this function at all   
  144.         if  (buf ==  null ) {  
  145.             return  1L;  
  146.         }  
  147.   
  148.         long  s1 = adler &  0xffff ;  
  149.         long  s2 = (adler >>  16 ) &  0xffff ;  
  150.         int  k;  
  151.   
  152.         while  (len >  0 ) {  
  153.             k = len < NMAX ? len : NMAX;  
  154.             len -= k;  
  155.             while  (k >=  16 ) {  
  156.                 s1 += buf[index++] & 0xff ;  
  157.                 s2 += s1;  
  158.                 s1 += buf[index++] & 0xff ;  
  159.                 s2 += s1;  
  160.                 s1 += buf[index++] & 0xff ;  
  161.                 s2 += s1;  
  162.                 s1 += buf[index++] & 0xff ;  
  163.                 s2 += s1;  
  164.                 s1 += buf[index++] & 0xff ;  
  165.                 s2 += s1;  
  166.                 s1 += buf[index++] & 0xff ;  
  167.                 s2 += s1;  
  168.                 s1 += buf[index++] & 0xff ;  
  169.                 s2 += s1;  
  170.                 s1 += buf[index++] & 0xff ;  
  171.                 s2 += s1;  
  172.                 s1 += buf[index++] & 0xff ;  
  173.                 s2 += s1;  
  174.                 s1 += buf[index++] & 0xff ;  
  175.                 s2 += s1;  
  176.                 s1 += buf[index++] & 0xff ;  
  177.                 s2 += s1;  
  178.                 s1 += buf[index++] & 0xff ;  
  179.                 s2 += s1;  
  180.                 s1 += buf[index++] & 0xff ;  
  181.                 s2 += s1;  
  182.                 s1 += buf[index++] & 0xff ;  
  183.                 s2 += s1;  
  184.                 s1 += buf[index++] & 0xff ;  
  185.                 s2 += s1;  
  186.                 s1 += buf[index++] & 0xff ;  
  187.                 s2 += s1;  
  188.                 k -= 16 ;  
  189.             }  
  190.             if  (k !=  0 ) {  
  191.                 do  {  
  192.                     s1 += buf[index++] & 0xff ;  
  193.                     s2 += s1;  
  194.                 } while  (--k !=  0 );  
  195.             }  
  196.             s1 %= BASE;  
  197.             s2 %= BASE;  
  198.         }  
  199.         return  (s2 <<  16 ) | s1;  
  200.     }  
  201.   
  202.     public   static   void  WritePng(DataOutputStream output,  int  width,  int  height,  
  203.                                 byte [] buffer,  byte [] colors,  
  204.                                 boolean  Transparent,  int  offset)  throws   
  205.             IOException {  
  206.         int  adler = ( int ) adler32(1l, buffer, offset, buffer.length - offset);  
  207.         byte [] lenNlen = {  //压缩块的LEN和NLEN信息   
  208.                          (byte0 ,  
  209.                          (byte0xfa , ( byte0x7e ,  
  210.                          (byte0x05 , ( byte0x81   
  211.         };  
  212.         IDATPOS = 0 ;  
  213.         output.write(HEADChunk);  
  214.         IDATPOS += HEADChunk.length;  
  215.         //写IHDR   
  216.         output.writeInt(13 );  //len   
  217.         output.writeInt(1229472850 );  //IHDR type code   
  218.         output.writeInt(width); //写宽度   
  219.         output.writeInt(height); //写高度   
  220.         output.writeByte(8 );  //1Bitdepth   
  221.         if  (colors ==  null ) {  
  222.             output.writeByte(6 );  //2ColorType   
  223.         } else  {  
  224.             output.writeByte(3 );  //2ColorType   
  225.         }  
  226.         output.writeByte(0 );  //3CompressionMethod   
  227.         output.writeByte(0 );  //4Filter method   
  228.         output.writeByte(0 );  //5Interlace method   
  229.         output.writeInt(0 );  //写crc   
  230.         IDATPOS += 25 ;  
  231.         //写PLTE   
  232.         if  (colors !=  null ) {  
  233.             output.writeInt(colors.length); //len   
  234.             output.writeInt(1347179589 );  //type code   
  235.             output.write(colors); //data   
  236.             output.writeInt(0 );  //crc   
  237.             IDATPOS += colors.length + 12 ;  
  238.         }  
  239.         //写TRNS   
  240.         if  (Transparent) {  
  241.             output.write(tRNSChunk);  
  242.             IDATPOS += tRNSChunk.length;  
  243.         }  
  244.         //写IDAT   
  245.         byte [] dpixels = buffer;  
  246.         int  bufferlen = dpixels.length - offset;  
  247.         int  blocklen =  32506 ;  
  248.         int  blocknum =  1 ;  
  249.         if  ((dpixels.length % blocklen) ==  0 ) {  
  250.             blocknum = bufferlen / blocklen;  
  251.         } else  {  
  252.             blocknum = (bufferlen / blocklen) + 1 ;  
  253.         }  
  254.         int  IDATChunkLen = (bufferlen +  6  + blocknum *  5 );  
  255.         output.writeInt(IDATChunkLen); //len   
  256.         output.writeInt(1229209940 );  //idat type code   
  257.         output.writeShort((short0x78da );  //78da   
  258.         for  ( int  i =  0 ; i < blocknum; i++) {  
  259.             int  off = i * blocklen;  
  260.             int  len = bufferlen - off;  
  261.             if  (len >= blocklen) {  
  262.                 len = blocklen;  
  263.                 lenNlen[0 ] = ( byte0 ;  
  264.             } else  {  
  265.                 lenNlen[0 ] = ( byte1 ;  
  266.             }  
  267.             int  msb = (len &  0xff );  
  268.             int  lsb = (len >>>  8 );  
  269.             lenNlen[1 ] = ( byte ) msb;  
  270.             lenNlen[2 ] = ( byte ) lsb;  
  271.             lenNlen[3 ] = ( byte ) (msb ^  0xff );  
  272.             lenNlen[4 ] = ( byte ) (lsb ^  0xff );  
  273.             output.write(lenNlen);  
  274.             output.write(dpixels, off + offset, len);  
  275.         }  
  276.         output.writeInt(adler); //IDAT adler   
  277.         output.writeInt(0 );  //IDAT crc   
  278.         output.write(IENDChunk);  
  279.     }  
  280.   
  281.     public   static   void  getImageBufferForImageARGB8888(Image img,  byte [] rawByte,  
  282.             int  w,  int  h,  int  off) {  
  283.         int  n = off;  
  284.         int [] raw =  new   int [w];  
  285.         for  ( int  j =  0 ; j < h; j++) {  
  286.             img.getRGB(raw, 0 , w,  0 , j, w,  1 );  
  287.             for  ( int  i =  0 ; i < raw.length; i++) {  
  288.                 int  ARGB = raw[i];  
  289.                 int  a = (ARGB &  0xff000000 ) >>  24 ;  
  290.                 int  r = (ARGB &  0xff0000 ) >>  16 ;  
  291.                 int  g = (ARGB &  0xff00 ) >>  8 ;  
  292.                 int  b = ARGB &  0xff ;  
  293.                 if  (i % w ==  0 ) {  
  294.                     n += 1 ;  
  295.                 }  
  296.                 rawByte[n] = (byte ) r;  
  297.                 rawByte[n + 1 ] = ( byte ) g;  
  298.                 rawByte[n + 2 ] = ( byte ) b;  
  299.                 rawByte[n + 3 ] = ( byte ) a;  
  300.                 n += 4 ;  
  301.             }  
  302.         }  
  303.         raw = null ;  
  304.         System.gc();  
  305.     }  
  306. }  
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.Connector;

public class CGame extends Canvas {
	public void paint(Graphics g)
	{
	}
    //Image2Bytes by AnderLu
    //生成的byte[]数组可直接用于外部存储为.png格式的图片文件看图软件可直接打开
    public static int IDATPOS;
    public static int haha = 0;
    public static byte[] HEADChunk = {
                                     (byte) 0x89, (byte) 0x50,
                                     (byte) 0x4E, (byte) 0x47,
                                     (byte) 0x0D, (byte) 0x0A,
                                     (byte) 0x1A, (byte) 0x0A,
    };
    public static byte[] tRNSChunk = {
                                     (byte) 0x00, (byte) 0x00,
                                     (byte) 0x00, (byte) 0x01,
                                     (byte) 0x74, (byte) 0x52,
                                     (byte) 0x4E, (byte) 0x53,
                                     (byte) 0x00,
                                     (byte) 0x40, (byte) 0xE6,
                                     (byte) 0xD8, (byte) 0x66,
    };
    public static byte[] IENDChunk = {
                                     //PNGIEND
                                     (byte) 0x00, (byte) 0x00,
                                     (byte) 0x00, (byte) 0x00,
                                     (byte) 0x49, (byte) 0x45,
                                     (byte) 0x4E, (byte) 0x44,
                                     (byte) 0xAE, (byte) 0x42,
                                     (byte) 0x60, (byte) 0x82
    };
    Image img;
    public void save(Image im)
    {
        byte data[] = Image2Bytes(im);
        this.img = Image.createImage(data, 0, data.length);

        
        saveFile("file:///E:/jieping/" + haha +".png", data);	//路径,haha变量递增 保存名为1.png,2.png
        haha++;
    }


    /**保存文件
     * @path:路径
     * @fileData:文件数据
     * @return: 0:出现异常,1:保存成功
     */
    public int saveFile(String path, byte[] fileData) {
        FileConnection fc = null;
        try {
            fc = (FileConnection) Connector.open(path, Connector.READ_WRITE);
            if (!fc.exists()) {
                fc.create();
            }
            OutputStream os = fc.openOutputStream();
            os.write(fileData);
            os.flush();
            os.close();
            fc.close();
            return 1;

        } catch (IOException ex) {
            ex.printStackTrace();
            return 0;
        }
    }

    public byte[] Image2Bytes(Image img) {
        try {
            int w = img.getWidth();
            int h = img.getHeight();
            int offset = 0;
            byte buffer[] = new byte[(w * 4 + 1) * h + offset];
            getImageBufferForImageARGB8888(img, buffer, w, h, offset);
            System.gc();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dout = new DataOutputStream(baos);
            WritePng(dout, w, h, buffer, null, false, offset);
            byte[] data = baos.toByteArray();
            writeCRC(data, 8); //更新IHDR CRC
            writeCRC(data, 33); //更新PLTE CRC
            writeCRC(data, IDATPOS); //更新IDAT CRC
            buffer = null;
            System.gc();
            return data;
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public static void writeCRC(byte[] data, int chunkpos) {
        int chunklen = ((data[chunkpos] & 0xFF) << 24)
                       | ((data[chunkpos + 1] & 0xFF) << 16)
                       | ((data[chunkpos + 2] & 0xFF) << 8)
                       | (data[chunkpos + 3] & 0xFF);

        int sum = CRCChecksum(data, chunkpos + 4, 4 + chunklen) ^ 0xffffffff;
        int val = sum;
        int pos = chunkpos + 8 + chunklen;
        data[pos] = (byte) ((val & 0xFF000000) >> 24);
        data[pos + 1] = (byte) ((val & 0xFF0000) >> 16);
        data[pos + 2] = (byte) ((val & 0xFF00) >> 8);
        data[pos + 3] = (byte) (val & 0xFF);
    }

    public static int[] crc_table; //CRC 表
    public static int CRCChecksum(byte[] buf, int off, int len) {
        int c = 0xffffffff;
        int n;
        if (crc_table == null) {
            int mkc;
            int mkn, mkk;
            crc_table = new int[256];
            for (mkn = 0; mkn < 256; mkn++) {
                mkc = mkn;
                for (mkk = 0; mkk < 8; mkk++) {
                    if ((mkc & 1) == 1) {
                        mkc = 0xedb88320 ^ (mkc >>> 1);
                    } else {
                        mkc = mkc >>> 1;
                    }
                }
                crc_table[mkn] = mkc;
            }
        }
        for (n = off; n < len + off; n++) {
            c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >>> 8);
        }
        return c;
    }

    public static long adler32(long adler, byte[] buf, int index, int len) {
        int BASE = 65521;
        int NMAX = 5552;
        //TODO remove this function at all
        if (buf == null) {
            return 1L;
        }

        long s1 = adler & 0xffff;
        long s2 = (adler >> 16) & 0xffff;
        int k;

        while (len > 0) {
            k = len < NMAX ? len : NMAX;
            len -= k;
            while (k >= 16) {
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                s1 += buf[index++] & 0xff;
                s2 += s1;
                k -= 16;
            }
            if (k != 0) {
                do {
                    s1 += buf[index++] & 0xff;
                    s2 += s1;
                } while (--k != 0);
            }
            s1 %= BASE;
            s2 %= BASE;
        }
        return (s2 << 16) | s1;
    }

    public static void WritePng(DataOutputStream output, int width, int height,
                                byte[] buffer, byte[] colors,
                                boolean Transparent, int offset) throws
            IOException {
        int adler = (int) adler32(1l, buffer, offset, buffer.length - offset);
        byte[] lenNlen = { //压缩块的LEN和NLEN信息
                         (byte) 0,
                         (byte) 0xfa, (byte) 0x7e,
                         (byte) 0x05, (byte) 0x81
        };
        IDATPOS = 0;
        output.write(HEADChunk);
        IDATPOS += HEADChunk.length;
        //写IHDR
        output.writeInt(13); //len
        output.writeInt(1229472850); //IHDR type code
        output.writeInt(width); //写宽度
        output.writeInt(height); //写高度
        output.writeByte(8); //1Bitdepth
        if (colors == null) {
            output.writeByte(6); //2ColorType
        } else {
            output.writeByte(3); //2ColorType
        }
        output.writeByte(0); //3CompressionMethod
        output.writeByte(0); //4Filter method
        output.writeByte(0); //5Interlace method
        output.writeInt(0); //写crc
        IDATPOS += 25;
        //写PLTE
        if (colors != null) {
            output.writeInt(colors.length); //len
            output.writeInt(1347179589); //type code
            output.write(colors); //data
            output.writeInt(0); //crc
            IDATPOS += colors.length + 12;
        }
        //写TRNS
        if (Transparent) {
            output.write(tRNSChunk);
            IDATPOS += tRNSChunk.length;
        }
        //写IDAT
        byte[] dpixels = buffer;
        int bufferlen = dpixels.length - offset;
        int blocklen = 32506;
        int blocknum = 1;
        if ((dpixels.length % blocklen) == 0) {
            blocknum = bufferlen / blocklen;
        } else {
            blocknum = (bufferlen / blocklen) + 1;
        }
        int IDATChunkLen = (bufferlen + 6 + blocknum * 5);
        output.writeInt(IDATChunkLen); //len
        output.writeInt(1229209940); //idat type code
        output.writeShort((short) 0x78da); //78da
        for (int i = 0; i < blocknum; i++) {
            int off = i * blocklen;
            int len = bufferlen - off;
            if (len >= blocklen) {
                len = blocklen;
                lenNlen[0] = (byte) 0;
            } else {
                lenNlen[0] = (byte) 1;
            }
            int msb = (len & 0xff);
            int lsb = (len >>> 8);
            lenNlen[1] = (byte) msb;
            lenNlen[2] = (byte) lsb;
            lenNlen[3] = (byte) (msb ^ 0xff);
            lenNlen[4] = (byte) (lsb ^ 0xff);
            output.write(lenNlen);
            output.write(dpixels, off + offset, len);
        }
        output.writeInt(adler); //IDAT adler
        output.writeInt(0); //IDAT crc
        output.write(IENDChunk);
    }

    public static void getImageBufferForImageARGB8888(Image img, byte[] rawByte,
            int w, int h, int off) {
        int n = off;
        int[] raw = new int[w];
        for (int j = 0; j < h; j++) {
            img.getRGB(raw, 0, w, 0, j, w, 1);
            for (int i = 0; i < raw.length; i++) {
                int ARGB = raw[i];
                int a = (ARGB & 0xff000000) >> 24;
                int r = (ARGB & 0xff0000) >> 16;
                int g = (ARGB & 0xff00) >> 8;
                int b = ARGB & 0xff;
                if (i % w == 0) {
                    n += 1;
                }
                rawByte[n] = (byte) r;
                rawByte[n + 1] = (byte) g;
                rawByte[n + 2] = (byte) b;
                rawByte[n + 3] = (byte) a;
                n += 4;
            }
        }
        raw = null;
        System.gc();
    }
}

 

注意:截屏命名是以1、2、3、4数字++命名的,如1.png,2.png等等,该程序设计的触发截屏事件是点击屏幕左上角,这个根据具体需要随 意改了,并且图片保存命名规则也需要完善下。

 

曾尝试过截屏的时候弄点提示,但这是万万不可的,在屏幕上出现任何提示都将会被截屏记录下来,所以我在写这个功能的时候,很是认真的考虑了截屏的完 整性及安全性,只要触发了,程序会自动屏蔽所有操作等待截屏操作结束,结束后恢复,也就是说机器慢的,触发截屏了,你在点或者做其他的程序界面的操作,那 都是不可能的,截屏生成保存文件这个过程结束后才可以。

 

附件:截屏类(其实就是把image对象转成png并保存到指定路径的方法)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值