Пример #1
0
  /**
   * Save onscreen image to file - suffix must be png, jpg, or gif.
   *
   * @param filename the name of the file with one of the required suffixes
   */
  public static void save(String filename) {
    File file = new File(filename);
    String suffix = filename.substring(filename.lastIndexOf('.') + 1);

    // png files
    if (suffix.toLowerCase().equals("png")) {
      try {
        ImageIO.write(onscreenImage, suffix, file);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    // need to change from ARGB to RGB for jpeg
    // reference: http://archives.java.sun.com/cgi-bin/wa?A2=ind0404&L=java2d-interest&D=0&P=2727
    else if (suffix.toLowerCase().equals("jpg")) {
      WritableRaster raster = onscreenImage.getRaster();
      WritableRaster newRaster;
      newRaster = raster.createWritableChild(0, 0, width, height, 0, 0, new int[] {0, 1, 2});
      DirectColorModel cm = (DirectColorModel) onscreenImage.getColorModel();
      DirectColorModel newCM =
          new DirectColorModel(
              cm.getPixelSize(), cm.getRedMask(), cm.getGreenMask(), cm.getBlueMask());
      BufferedImage rgbBuffer = new BufferedImage(newCM, newRaster, false, null);
      try {
        ImageIO.write(rgbBuffer, suffix, file);
      } catch (IOException e) {
        e.printStackTrace();
      }
    } else {
      System.out.println("Invalid image file type: " + suffix);
    }
  }
 public DisplayImage16(int w, int h) {
   this();
   iWidth = w;
   iHeight = h;
   iSize = iWidth * iHeight;
   imageBufferArray = new short[iSize];
   for (int i = 0; i < iSize; i++) {
     imageBufferArray[i] = 0;
   }
   bImage = new BufferedImage(iWidth, iHeight, BufferedImage.TYPE_USHORT_GRAY);
   bRaster = bImage.getRaster();
   bRaster.setDataElements(0, 0, iWidth, iHeight, imageBufferArray);
   //    buffGraphics = (Graphics2D) bImage.createGraphics();
   //    buffGraphics.setFont(font);
   this.setPreferredSize(new Dimension(iWidth, iHeight));
   setVisible(true);
   // this.setBorder(BorderFactory.createLoweredBevelBorder());
 }
 public void updateImage(short[] imageData) {
   bRaster.setDataElements(0, 0, iWidth, iHeight, imageData);
   repaint();
 }
Пример #4
0
 public static BufferedImage getImageFromArray(int[] pixels, int width, int height) {
   BufferedImage imageBI = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
   WritableRaster raster = (WritableRaster) imageBI.getData();
   raster.setPixels(0, 0, width, height, pixels);
   return imageBI;
 }