注:这代码是我从http://expert.csdn.net/Expert/topic/2529/2529972.xml?temp=.1256678 转来的,我想它会给你帮助的。测试环境为jdk1.4import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;public class test 
{    
  public static void main(String args[]) 
  {
    try
    {
      //读取第一张图片
      File fileOne = new File("D:\\house.png");
      BufferedImage ImageOriginal = ImageIO.read(fileOne);
      int width = ImageOriginal.getWidth();//图片宽度
      int height = ImageOriginal.getHeight();//图片高度
  //计算四张图片的大小
  int widthOne = width/2;
  int widthTwo = width - widthOne;
  int heightOne = height/2;
  int heightTwo = height - heightOne;
      //从图片中读取RGB
      int[] ImageArray = new int[widthOne*heightOne];
      ImageArray = ImageOriginal.getRGB(0,0,widthOne,heightOne,ImageArray,0,widthOne);      
      //生成第一张新图片
      BufferedImage ImageNewOne = new BufferedImage(widthOne,heightOne,BufferedImage.TYPE_INT_RGB);
      ImageNewOne.setRGB(0,0,widthOne,heightOne,ImageArray,0,widthOne);
      File outFileOne = new File("d:\\house_1.png");
      ImageIO.write(ImageNewOne, "png", outFileOne);//写图片   //其他三张图片做相同的处理
      ImageArray = new int[widthTwo*heightOne];
      ImageArray = ImageOriginal.getRGB(widthOne,0,widthTwo,heightOne,ImageArray,0,widthTwo);
  BufferedImage ImageNewTwo = new BufferedImage(widthTwo,heightOne,BufferedImage.TYPE_INT_RGB);
      ImageNewTwo.setRGB(0,0,widthTwo,heightOne,ImageArray,0,widthTwo);
      File outFileTwo = new File("d:\\house_2.png");
      ImageIO.write(ImageNewTwo, "png", outFileTwo);   ImageArray = new int[widthTwo*heightOne];
      ImageArray = ImageOriginal.getRGB(0,heightOne,widthOne,heightTwo,ImageArray,0,widthOne);
  BufferedImage ImageNewThree = new BufferedImage(widthOne,heightTwo,BufferedImage.TYPE_INT_RGB);
      ImageNewThree.setRGB(0,0,widthOne,heightTwo,ImageArray,0,widthOne);
      File outFileThree = new File("d:\\house_3.png");
      ImageIO.write(ImageNewThree, "png", outFileThree);   ImageArray = new int[widthTwo*heightTwo];
      ImageArray = ImageOriginal.getRGB(widthOne,heightOne,widthTwo,heightTwo,ImageArray,0,widthTwo);
  BufferedImage ImageNewFour = new BufferedImage(widthTwo,heightTwo,BufferedImage.TYPE_INT_RGB);
      ImageNewFour.setRGB(0,0,widthTwo,heightTwo,ImageArray,0,widthTwo);
      File outFileFour = new File("d:\\house_4.png");
      ImageIO.write(ImageNewFour, "png", outFileFour);//写图片
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
}

解决方案 »

  1.   

    不知道这段代码能不能符合你的要求。此代码将D:\house.png文件的每个象素读出,然后存入到d:\house.txt文本中。测试环境jdk1.4import java.io.File;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import java.io.FileOutputStream;
    import java.io.FileInputStream;public class test 
    {    
      public static void main(String args[]) 
      {
        try
        {
          //读取第一张图片
          File fileOne = new File("D:\\house.png");
          BufferedImage ImageOriginal = ImageIO.read(fileOne);
          int width = ImageOriginal.getWidth();//图片宽度
          int height = ImageOriginal.getHeight();//图片高度
          //从图片中读取RGB
          int[] ImageArray = new int[width*height];
          ImageArray = ImageOriginal.getRGB(0,0,width,height,ImageArray,0,width);
          //写文件
          FileOutputStream fos = new FileOutputStream(new File("D:\\house.txt"));
          System.out.println(ImageArray[0]);
          fos.write(String.valueOf(ImageArray[0]).getBytes());
          for(int i=0;i<ImageArray.length;i++)
          {
            fos.write(ImageArray[i]);
          }
          fos.close();
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
      }
    }
      

  2.   

    1    /**
    2     * PngEncoder takes a Java Image object and creates a byte string which can be saved as a PNG file.
    3     * The Image is presumed to use the DirectColorModel.
    4     *
    5     * Thanks to Jay Denny at KeyPoint Software
    6     *    http://www.keypoint.com/
    7     * who let me develop this code on company time.
    8     *
    9     * You may contact me with (probably very-much-needed) improvements,
    10    * comments, and bug fixes at:
    11    *
    12    *   [email protected]
    13    *
    14    * This library is free software; you can redistribute it and/or
    15    * modify it under the terms of the GNU Lesser General Public
    16    * License as published by the Free Software Foundation; either
    17    * version 2.1 of the License, or (at your option) any later version.
    18    * 
    19    * This library is distributed in the hope that it will be useful,
    20    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    21    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    22    * Lesser General Public License for more details.
    23    * 
    24    * You should have received a copy of the GNU Lesser General Public
    25    * License along with this library; if not, write to the Free Software
    26    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    27    * A copy of the GNU LGPL may be found at
    28    * http://www.gnu.org/copyleft/lesser.html,
    29    *
    30    * @author J. David Eisenberg
    31    * @version 1.4, 31 March 2000
    32    *
    33    * Hacked by Ralph Hartley 8/00
    34    * Instead of acuumultaing the output in an array, write it directly to
    35    * a stream. A byte array big enough for a large image can be very big.
    36    *
    37    * In fact it has been hacked almost beyond recognition.
    38    */
    39   
    40   import java.io.*;
    41   import java.awt.*;
    42   import java.awt.image.*;
    43   import java.util.*;
    44   import java.util.zip.*;
    45   import java.io.*;
    46   import javax.swing.*;
    47   
    48   public class PngEncoder extends Object
    49   {
    50     /** Constant specifying that alpha channel should be encoded. */
    51     public static final boolean ENCODE_ALPHA=true;
    52     /** Constant specifying that alpha channel should not be encoded. */
    53     public static final boolean NO_ALPHA=false;
    54     /** Constants for filters */
    55     public static final int FILTER_NONE = 0;
    56     public static final int FILTER_SUB = 1;
    57     public static final int FILTER_UP = 2;
    58     public static final int FILTER_LAST = 2;
    59   
    60     public static int compress = 1;
    61     public static int filter = FILTER_SUB | FILTER_UP;
    62     public static int chunksize;
    63   
    64     protected OutputStream out;
    65     protected int width, height;
    66     protected CRC32 crc = new CRC32();
    67     protected long crcValue;
    68   
    69     public boolean encodeAlpha = true;
    70   
    71     public JLabel status = null;
    72     public String message = "";
    73   
    74   //  public static boolean kill = false;
    75   
    76     /**
    77      * Class constructor specifying Size of image, and stream to write to.
    78      * @param int width The total width of the image.
    79      * @param int height The total height of the image.
    80      * @param OutPutStream out The stream to write the image to.
    81      * @see java.awt.Image
    82      */
    83     public PngEncoder( int width,int height,OutputStream out) {
    84       this.width = width;
    85       this.height = height;
    86       this.out = out;
    87     }
    88   
    89     /**
    90      * Write an array of bytes output stream
    91      * Note: This routine has the side effect of updating
    92      * the crc.
    93      *
    94      * @param data The data to be written .
    95      */
    96     protected void writeBytes( byte[] data) throws IOException {
    97       out.write(data);
    98       crc.update(data);
    99     }
    100  
      

  3.   

    101    /**
    102     * Write an array of bytes into the pngBytes array, specifying number of bytes to write.
    103     * Note: This routine has the side effect of updating
    104     * the crc.
    105     *
    106     * @param data The data to be written.
    107     * @param nBytes The number of bytes to be written.
    108     */
    109    protected void writeBytes( byte[] data, int nBytes ) throws IOException  {
    110      out.write(data,0,nBytes);
    111      crc.update(data,0,nBytes);
    112    }
    113  
    114    /**
    115     * Write a two-byte integer into the output.
    116     *
    117     * @param n The integer to be written.
    118     */
    119    protected void writeInt2( int n ) throws IOException {
    120      byte[] temp = { (byte)((n >> 8) & 0xff),
    121               (byte) (n & 0xff) };
    122      writeBytes( temp );
    123    }
    124  
    125    /**
    126     * Write a four-byte integer into the output.
    127     *
    128     * @param n The integer to be written.
    129     */
    130    protected void writeInt4( int n ) throws IOException {
    131      byte[] temp = { (byte)((n >> 24) & 0xff),
    132               (byte) ((n >> 16) & 0xff ),
    133               (byte) ((n >> 8) & 0xff ),
    134               (byte) ( n & 0xff ) };
    135      out.write( temp );
    136      crc.update(temp);
    137    }
    138  
    139    /**
    140     * Write a four-byte integer into the output.
    141     * Do not update crc.
    142     *
    143     * @param n The integer to be written.
    144     */
    145    protected void writeInt4nocrc( int n ) throws IOException {
    146      byte[] temp = { (byte)((n >> 24) & 0xff),
    147               (byte) ((n >> 16) & 0xff ),
    148               (byte) ((n >> 8) & 0xff ),
    149               (byte) ( n & 0xff ) };
    150      out.write( temp );
    151    }
    152  
    153    /**
    154     * Write a single byte into the output.
    155     *
    156     * @param n The integer to be written.
    157     */
    158    protected void writeByte( int b ) throws IOException {
    159      out.write(b);
    160      crc.update(b);
    161    }
    162  
    163    /**
    164     * Write a string into the output.
    165     * This uses the getBytes method, so the encoding used will
    166     * be its default.
    167     *
    168     * @param n The integer to be written.
    169     * @see java.lang.String#getBytes()
    170     */
    171    protected void writeString( String s ) throws IOException {
    172      writeBytes( s.getBytes() );
    173    }
    174  
    175    /**
    176     * Write a PNG "IHDR" chunk to the output.
    177     */
    178    protected void writeHeader() throws IOException {
    179      crc.reset();
    180      writeInt4nocrc( 13 );
    181      writeString( "IHDR");
    182  //    width = image.getWidth( null );
    183  //    height = image.getHeight( null );
    184      writeInt4( width );
    185      writeInt4( height );
    186      writeByte( 8 ); // bit depth
    187      writeByte( (encodeAlpha) ? 6 : 2 ); // direct model
    188      writeByte( 0 ); // compression method
    189      writeByte( 0 ); // filter method
    190      writeByte( 0 ); // no interlace
    191      crcValue = crc.getValue();
    192      writeInt4nocrc( (int) crcValue );
    193    }
    194  
    195    int bytesPerPixel = 4;
    196    byte[] lastline = null;
    197    byte[] raw = null;
    198    byte[] line = null;
    199    int[] pixels = null;
    200    byte[] deflatebuff;