Java Code Examples for java.awt.image.BufferedImage#flush()

The following examples show how to use java.awt.image.BufferedImage#flush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransformedTextPainter.java    From Pixelitor with GNU General Public License v3.0 6 votes vote down vote up
public Shape getTextShape(Canvas canvas) {
    // create this image just to get a Graphics2D somehow...
    BufferedImage tmp = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = tmp.createGraphics();
    var imgOrigTransform = g2.getTransform();

    int canvasWidth = canvas.getWidth();
    int canvasHeight = canvas.getHeight();
    setupGraphics(g2, canvasWidth, canvasHeight, getText());
    var at = g2.getTransform();
    g2.setTransform(imgOrigTransform); // provideShape must be called with untransformed Graphics
    Shape shape = provideShape(g2, null, canvasWidth, canvasHeight);

    g2.dispose();
    tmp.flush();

    return at.createTransformedShape(shape);
}
 
Example 2
Source File: Utilities.java    From Darcula with Apache License 2.0 6 votes vote down vote up
public static BufferedImage createGradientMask(int width, int height, int orientation) {
    // algorithm derived from Romain Guy's blog
    BufferedImage gradient = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = gradient.createGraphics();
    GradientPaint paint = new GradientPaint(0.0f, 0.0f,
            new Color(1.0f, 1.0f, 1.0f, 1.0f),
            orientation == SwingConstants.HORIZONTAL? width : 0.0f, 
            orientation == SwingConstants.VERTICAL? height : 0.0f,
            new Color(1.0f, 1.0f, 1.0f, 0.0f));
    g.setPaint(paint);
    g.fill(new Rectangle2D.Double(0, 0, width, height));

    g.dispose();
    gradient.flush();

    return gradient;
}
 
Example 3
Source File: ImageUtils.java    From springboot-admin with Apache License 2.0 6 votes vote down vote up
/**
 * 等比缩放,居中剪切
 * 
 * @param src
 * @param dest
 * @param w
 * @param h
 * @throws IOException
 */
public static void scale(String src, String dest, int w, int h) throws IOException {
	String srcSuffix = src.substring(src.lastIndexOf(".") + 1);
	Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName(srcSuffix);
	ImageReader reader = (ImageReader) iterator.next();

	InputStream in = new FileInputStream(src);
	ImageInputStream iis = ImageIO.createImageInputStream(in);
	reader.setInput(iis);

	BufferedImage srcBuffered = readBuffereImage(reader, w, h);
	BufferedImage targetBuffered = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

	Graphics graphics = targetBuffered.getGraphics();
	graphics.drawImage(srcBuffered.getScaledInstance(w, h, Image.SCALE_DEFAULT), 0, 0, null); // 绘制缩小后的图

	graphics.dispose();
	srcBuffered.flush();
	in.close();
	iis.close();

	ImageIO.write(targetBuffered, srcSuffix, new File(dest));
	targetBuffered.flush();
}
 
Example 4
Source File: Screen.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
/**
 * 设定背景图像
 * 
 * @param screen
 */
public void setBackground(BufferedImage screen) {
	if (screen != null) {
		if (screen.getWidth() != getWidth() || screen.getHeight() != getHeight()) {
			screen = GraphicsUtils.getResize(screen, getWidth(), getHeight());
		}
		BufferedImage tmp = currentScreen;
		this.currentScreen = screen;
		if (tmp != null) {
			tmp.flush();
			tmp = null;
		}
		this.setRepaintMode(SCREEN_BITMAP_REPAINT);
	} else {
		this.setRepaintMode(SCREEN_CANVAS_REPAINT);
	}
}
 
Example 5
Source File: Utilities.java    From littleluck with Apache License 2.0 6 votes vote down vote up
public static BufferedImage createGradientMask(int width, int height, int orientation) {
    // algorithm derived from Romain Guy's blog
    BufferedImage gradient = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = gradient.createGraphics();
    GradientPaint paint = new GradientPaint(0.0f, 0.0f,
            new Color(1.0f, 1.0f, 1.0f, 1.0f),
            orientation == SwingConstants.HORIZONTAL? width : 0.0f, 
            orientation == SwingConstants.VERTICAL? height : 0.0f,
            new Color(1.0f, 1.0f, 1.0f, 0.0f));
    g.setPaint(paint);
    g.fill(new Rectangle2D.Double(0, 0, width, height));

    g.dispose();
    gradient.flush();

    return gradient;
}
 
Example 6
Source File: ProvidedSkin.java    From Geyser with MIT License 6 votes vote down vote up
public ProvidedSkin(String internalUrl) {
    try {
        BufferedImage image = ImageIO.read(ProvidedSkin.class.getClassLoader().getResource(internalUrl));

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(image.getWidth() * 4 + image.getHeight() * 4);
        try {
            for (int y = 0; y < image.getHeight(); y++) {
                for (int x = 0; x < image.getWidth(); x++) {
                    int rgba = image.getRGB(x, y);
                    outputStream.write((rgba >> 16) & 0xFF); // Red
                    outputStream.write((rgba >> 8) & 0xFF); // Green
                    outputStream.write(rgba & 0xFF); // Blue
                    outputStream.write((rgba >> 24) & 0xFF); // Alpha
                }
            }
            image.flush();
            skin = outputStream.toByteArray();
        } finally {
            try {
                outputStream.close();
            } catch (IOException ignored) {}
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: SVGDocumentGenerator.java    From latexdraw with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a thumbnail from the given selection in the given file.
 * @param templateFile The file of the future thumbnail.
 * @param selection The set of shapes composing the template.
 */
private void createTemplateThumbnail(final File templateFile, final javafx.scene.Group selection) {
	final Bounds bounds = selection.getBoundsInParent();
	final double scale = 70d / Math.max(bounds.getWidth(), bounds.getHeight());
	final WritableImage img = new WritableImage((int) (bounds.getWidth() * scale), (int) (bounds.getHeight() * scale));
	final SnapshotParameters snapshotParameters = new SnapshotParameters();

	snapshotParameters.setFill(Color.WHITE);
	snapshotParameters.setTransform(new Scale(scale, scale));
	selection.snapshot(snapshotParameters, img);

	final BufferedImage bufferedImage = SwingFXUtils.fromFXImage(img, null);

	try {
		ImageIO.write(bufferedImage, "png", templateFile);  //NON-NLS
	}catch(final IOException ex) {
		BadaboomCollector.INSTANCE.add(ex);
	}
	bufferedImage.flush();
}
 
Example 8
Source File: ZoomInVideo.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
public static BufferedImage getViewportImage(TiledImagePainter tip, double scale, Point2D center, int vpWidth, int vpHeight) throws Exception {

        double vpX = -(vpWidth/2d);
        double vpY = -(vpHeight/2d);
        BufferedImage bi = new BufferedImage(vpWidth,vpHeight,BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = (Graphics2D) bi.getGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);

        double sc = scale / 100d;
        double vpOffsX = center.getX()*sc +vpX;
        double vpOffsY = center.getY()*sc +vpY;
        g2d.translate(-vpOffsX,-vpOffsY);
        g2d.scale(sc, sc);

        tip.drawImage(g2d,vpOffsX,vpOffsY,vpWidth,vpHeight,scale,0);
        bi.flush();
        return bi;
    }
 
Example 9
Source File: GraphicsUtils.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
/**
 * 加载双图对比过滤图像
 * 
 * @param img
 * @param width
 * @param height
 * @return
 */
final static public BufferedImage loadDoubleFilterImage(final Image img, final int width, final int height) {
	BufferedImage img1 = GraphicsUtils.drawClipImage(img, width, height, 0, 0);
	BufferedImage img2 = GraphicsUtils.drawClipImage(img, width, height, width, 0);
	WritableRaster writableRaster1 = img1.getRaster();
	DataBuffer dataBuffer1 = writableRaster1.getDataBuffer();
	int[] basePixels1 = getDataInt(dataBuffer1);
	WritableRaster writableRaster2 = img2.getRaster();
	DataBuffer dataBuffer2 = writableRaster2.getDataBuffer();
	int[] basePixels2 = getDataInt(dataBuffer2);
	int length = basePixels2.length;
	for (int i = 0; i < length; i++) {
		if (basePixels2[i] >= LColor.getRGB(200, 200, 200)) {
			basePixels2[i] = 0xffffff;
		} else {
			basePixels2[i] = basePixels1[i];
		}
	}
	img1.flush();
	img1 = null;
	return img2;
}
 
Example 10
Source File: ImageSaver.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
/** Save an RGB jpeg including the alpha channel if it has one; can be read only by ImageSaver.openJpegAlpha method; in other software the alpha channel is confused by some other color channel. */
static public final boolean saveAsJpegAlpha(final Image awt, final String path, final float quality) {
	final BufferedImage bi = asBufferedImage(awt);
	boolean b = saveAsJpegAlpha(bi, path, quality);
	if (bi != awt) bi.flush();
	return b;
}
 
Example 11
Source File: MainFrame.java    From ios-image-util with MIT License 5 votes vote down vote up
/**
 * Write launch image to the file.
 *
 * @param srcImageFile	source image
 * @param asset			image information
 * @param targetFile	file to output
 * @throws Exception	exception
 */
private void writeSplashImage(final ImageFile srcImageFile, final IOSSplashAssetCatalogs asset, final File targetFile) throws Exception {
	SwingWorker<Boolean, Integer> worker = new SwingWorker<Boolean, Integer>() {
		@Override protected Boolean doInBackground() throws Exception {
			try {
				boolean writable = (targetFile.exists() && targetFile.canWrite());
				if (!writable && !targetFile.exists()) {
					try { writable = targetFile.createNewFile(); } catch (Exception ex) { writable = false; }
				}
				if (!writable) {
					throw new IOException(String.format("%s [%s]", getResource("error.not.writable", "not writable"), targetFile.getAbsolutePath()));
				}
				int width = (int)asset.getIOSImageInfo().getSize().getWidth();
				int height = (int)asset.getIOSImageInfo().getSize().getHeight();
				BufferedImage buf = generateLaunchImage(srcImageFile.getImage(), width, height, asset);
				ImageIO.write(buf, "png", targetFile);
				buf.flush();
				buf = null;
				verbose(targetFile);
				return true;
			} catch (Throwable t) {
				handleThrowable(t);
				return false;
			} finally {
				addProgress(1);
			}
		}
	};
	pool.submit(worker);
}
 
Example 12
Source File: Skin.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void parseBufferedImage(BufferedImage image) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            Color color = new Color(image.getRGB(x, y), true);
            outputStream.write(color.getRed());
            outputStream.write(color.getGreen());
            outputStream.write(color.getBlue());
            outputStream.write(color.getAlpha());
        }
    }
    image.flush();
    this.setSkinData(outputStream.toByteArray());
}
 
Example 13
Source File: WComponentPeer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void print(Graphics g) {

    Component comp = (Component)target;

    // To conserve memory usage, we will band the image.

    int totalW = comp.getWidth();
    int totalH = comp.getHeight();

    int hInc = (int)(totalH / BANDING_DIVISOR);
    if (hInc == 0) {
        hInc = totalH;
    }

    for (int startY = 0; startY < totalH; startY += hInc) {
        int endY = startY + hInc - 1;
        if (endY >= totalH) {
            endY = totalH - 1;
        }
        int h = endY - startY + 1;

        Color bgColor = comp.getBackground();
        int[] pix = createPrintedPixels(0, startY, totalW, h,
                                        bgColor == null ? 255 : bgColor.getAlpha());
        if (pix != null) {
            BufferedImage bim = new BufferedImage(totalW, h,
                                          BufferedImage.TYPE_INT_ARGB);
            bim.setRGB(0, 0, totalW, h, pix, 0, totalW);
            g.drawImage(bim, 0, startY, null);
            bim.flush();
        }
    }

    comp.print(g);
}
 
Example 14
Source File: WComponentPeer.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void print(Graphics g) {

    Component comp = (Component)target;

    // To conserve memory usage, we will band the image.

    int totalW = comp.getWidth();
    int totalH = comp.getHeight();

    int hInc = (int)(totalH / BANDING_DIVISOR);
    if (hInc == 0) {
        hInc = totalH;
    }

    for (int startY = 0; startY < totalH; startY += hInc) {
        int endY = startY + hInc - 1;
        if (endY >= totalH) {
            endY = totalH - 1;
        }
        int h = endY - startY + 1;

        Color bgColor = comp.getBackground();
        int[] pix = createPrintedPixels(0, startY, totalW, h,
                                        bgColor == null ? 255 : bgColor.getAlpha());
        if (pix != null) {
            BufferedImage bim = new BufferedImage(totalW, h,
                                          BufferedImage.TYPE_INT_ARGB);
            bim.setRGB(0, 0, totalW, h, pix, 0, totalW);
            g.drawImage(bim, 0, startY, null);
            bim.flush();
        }
    }

    comp.print(g);
}
 
Example 15
Source File: ImageUtils.java    From springboot-admin with Apache License 2.0 5 votes vote down vote up
/**
 * 图片大小压缩
 *
 * @param src
 * @throws IOException
 */
public static void scale(String src) throws IOException {
	File file = new File(src);
	if (!file.exists()) {
		return;
	}
	BufferedImage bufferedImage = ImageIO.read(file);
	int w = bufferedImage.getWidth();
	int h = bufferedImage.getHeight();
	String srcSuffix = src.substring(src.lastIndexOf(".") + 1);
	Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName(srcSuffix);
	ImageReader reader = (ImageReader) iterator.next();

	InputStream in = new FileInputStream(src);
	ImageInputStream iis = ImageIO.createImageInputStream(in);
	reader.setInput(iis);

	BufferedImage srcBuffered = readBuffereImage(reader, w, h);
	BufferedImage targetBuffered = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

	Graphics graphics = targetBuffered.getGraphics();
	graphics.drawImage(srcBuffered.getScaledInstance(w, h, Image.SCALE_DEFAULT), 0, 0, null); // 绘制缩小后的图

	graphics.dispose();
	srcBuffered.flush();
	in.close();
	iis.close();

	ImageIO.write(targetBuffered, srcSuffix, file);
	targetBuffered.flush();
}
 
Example 16
Source File: WComponentPeer.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void print(Graphics g) {

    Component comp = (Component)target;

    // To conserve memory usage, we will band the image.

    int totalW = comp.getWidth();
    int totalH = comp.getHeight();

    int hInc = (int)(totalH / BANDING_DIVISOR);
    if (hInc == 0) {
        hInc = totalH;
    }

    for (int startY = 0; startY < totalH; startY += hInc) {
        int endY = startY + hInc - 1;
        if (endY >= totalH) {
            endY = totalH - 1;
        }
        int h = endY - startY + 1;

        Color bgColor = comp.getBackground();
        int[] pix = createPrintedPixels(0, startY, totalW, h,
                                        bgColor == null ? 255 : bgColor.getAlpha());
        if (pix != null) {
            BufferedImage bim = new BufferedImage(totalW, h,
                                          BufferedImage.TYPE_INT_ARGB);
            bim.setRGB(0, 0, totalW, h, pix, 0, totalW);
            g.drawImage(bim, 0, startY, null);
            bim.flush();
        }
    }

    comp.print(g);
}
 
Example 17
Source File: ImageService.java    From htwplus with MIT License 4 votes vote down vote up
static private void saveToJPG(BufferedImage image, File file) throws IOException {
    BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
    newImage.createGraphics().drawImage(image, 0, 0, Color.WHITE, null);
    ImageIO.write(newImage, "jpg", file);
    newImage.flush();
}
 
Example 18
Source File: DropShadowBorder.java    From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Map<Position,BufferedImage> getImages(Graphics2D g2) {
    //first, check to see if an image for this size has already been rendered
    //if so, use the cache. Else, draw and save
    Map<Position,BufferedImage> images = CACHE.get(shadowSize);
    if (images == null) {
        images = new HashMap<Position,BufferedImage>();

        /*
         * Do draw a drop shadow, I have to:
         *  1) Create a rounded rectangle
         *  2) Create a BufferedImage to draw the rounded rect in
         *  3) Translate the graphics for the image, so that the rectangle
         *     is centered in the drawn space. The border around the rectangle
         *     needs to be shadowWidth wide, so that there is space for the
         *     shadow to be drawn.
         *  4) Draw the rounded rect as black, with an opacity of 50%
         *  5) Create the BLUR_KERNEL
         *  6) Blur the image
         *  7) copy off the corners, sides, etc into images to be used for
         *     drawing the Border
         */
        int rectWidth = cornerSize + 1;
        RoundRectangle2D rect = new RoundRectangle2D.Double(0, 0, rectWidth, rectWidth, cornerSize, cornerSize);
        int imageWidth = rectWidth + shadowSize * 2;
        BufferedImage image = GraphicsUtilities.createCompatibleTranslucentImage(imageWidth, imageWidth);
        Graphics2D buffer = (Graphics2D)image.getGraphics();
        buffer.setColor(new Color(0.0f, 0.0f, 0.0f, shadowOpacity));
        buffer.translate(shadowSize, shadowSize);
        buffer.fill(rect);
        buffer.dispose();
        
        float blurry = 1.0f / (float)(shadowSize * shadowSize);
        float[] blurKernel = new float[shadowSize * shadowSize];
        for (int i=0; i<blurKernel.length; i++) {
            blurKernel[i] = blurry;
        }
        ConvolveOp blur = new ConvolveOp(new Kernel(shadowSize, shadowSize, blurKernel));
        BufferedImage targetImage = GraphicsUtilities.createCompatibleTranslucentImage(imageWidth, imageWidth);
        ((Graphics2D)targetImage.getGraphics()).drawImage(image, blur, -(shadowSize/2), -(shadowSize/2));

        int x = 1;
        int y = 1;
        int w = shadowSize;
        int h = shadowSize;
        images.put(Position.TOP_LEFT, getSubImage(targetImage, x, y, w, h));
        x = 1;
        y = h;
        w = shadowSize;
        h = 1;
        images.put(Position.LEFT, getSubImage(targetImage, x, y, w, h));
        x = 1;
        y = rectWidth;
        w = shadowSize;
        h = shadowSize;
        images.put(Position.BOTTOM_LEFT, getSubImage(targetImage, x, y, w, h));
        x = cornerSize + 1;
        y = rectWidth;
        w = 1;
        h = shadowSize;
        images.put(Position.BOTTOM, getSubImage(targetImage, x, y, w, h));
        x = rectWidth;
        y = x;
        w = shadowSize;
        h = shadowSize;
        images.put(Position.BOTTOM_RIGHT, getSubImage(targetImage, x, y, w, h));
        x = rectWidth;
        y = cornerSize + 1;
        w = shadowSize;
        h = 1;
        images.put(Position.RIGHT, getSubImage(targetImage, x, y, w, h));
        x = rectWidth;
        y = 1;
        w = shadowSize;
        h = shadowSize;
        images.put(Position.TOP_RIGHT, getSubImage(targetImage, x, y, w, h));
        x = shadowSize;
        y = 1;
        w = 1;
        h = shadowSize;
        images.put(Position.TOP, getSubImage(targetImage, x, y, w, h));

        image.flush();
        CACHE.put(shadowSize, images);
    }
    return images;
}
 
Example 19
Source File: CodeUtil.java    From o2oa with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 *生成带logo的二维码图片
 * @param logoFile /logo图片文件
 * @param codeFile /二维码图片
 * @param qrUrl /二维码存储的信息:vcard格式
 * @param note /二维码描述信息
 */
public static byte[] drawLogoQRCodeByte(BufferedImage logo, ByteArrayOutputStream codeFile, String qrUrl, String note) {
	byte[] bs = null;
    try {
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
        BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

        // 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
        for (int x = 0; x < WIDTH; x++) {
            for (int y = 0; y < HEIGHT; y++) {
                image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
            }
        }

        int width = image.getWidth();
        int height = image.getHeight();
        if (Objects.nonNull(logo)) {
            // 构建绘图对象
            Graphics2D g = image.createGraphics();
            // 读取Logo图片
            //BufferedImage logo = ImageIO.read(logoFile);
            // 开始绘制logo图片
            g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
            g.dispose();
            logo.flush();
        }

        // 自定义文本描述
        if (StringUtils.isNotEmpty(note)) {
            // 新的图片,把带logo的二维码下面加上文字
            BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D outg = outImage.createGraphics();
            // 画二维码到新的面板
            outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
            // 画文字到新的面板
            outg.setColor(Color.BLACK);
            outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号
            int strWidth = outg.getFontMetrics().stringWidth(note);
            if (strWidth > WIDTH) {
                // //长度过长就截取前面部分
                // 长度过长就换行
                String note1 = note.substring(0, note.length() / 2);
                String note2 = note.substring(note.length() / 2, note.length());
                int strWidth1 = outg.getFontMetrics().stringWidth(note1);
                int strWidth2 = outg.getFontMetrics().stringWidth(note2);
                outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
                BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
                Graphics2D outg2 = outImage2.createGraphics();
                outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
                outg2.setColor(Color.BLACK);
                outg2.setFont(new Font("宋体", Font.BOLD, 30)); // 字体、字型、字号
                outg2.drawString(note2, 200 - strWidth2 / 2,outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
                outg2.dispose();
                outImage2.flush();
                outImage = outImage2;
            } else {
                outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 画文字
            }
            outg.dispose();
            outImage.flush();
            image = outImage;
        }

        image.flush();

        ImageIO.write(image, "png", codeFile); // TODO
        bs = codeFile.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bs;
}
 
Example 20
Source File: AbstractPainter.java    From pgptool with GNU General Public License v3.0 3 votes vote down vote up
/**
 * <p>
 * Call this method to clear the cacheable. This may be called whether there is
 * a cacheable being used or not. If cleared, on the next call to
 * <code>paint</code>, the painting routines will be called.
 * </p>
 *
 * <p>
 * <strong>Subclasses</strong>If overridden in subclasses, you
 * <strong>must</strong> call super.clearCache, or physical resources (such as
 * an Image) may leak.
 * </p>
 */
public void clearCache() {
	BufferedImage cache = cachedImage == null ? null : cachedImage.get();
	if (cache != null) {
		cache.flush();
	}
	cacheCleared = true;
	if (!isCacheable()) {
		cachedImage = null;
	}
}