Java Code Examples for java.awt.image.BufferedImage#TYPE_4BYTE_ABGR_PRE

The following examples show how to use java.awt.image.BufferedImage#TYPE_4BYTE_ABGR_PRE . 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: ColConvTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static String getImageTypeName(int type) {
    switch(type) {
        case BufferedImage.TYPE_INT_ARGB:
            return "TYPE_INT_ARGB";
        case BufferedImage.TYPE_INT_RGB:
            return "TYPE_INT_RGB";
        case BufferedImage.TYPE_INT_BGR:
            return "TYPE_INT_BGR";
        case BufferedImage.TYPE_INT_ARGB_PRE:
            return "TYPE_INT_ARGB_PRE";
        case BufferedImage.TYPE_3BYTE_BGR:
            return "TYPE_3BYTE_BGR";
        case BufferedImage.TYPE_4BYTE_ABGR:
            return "TYPE_4BYTE_ABGR";
        case BufferedImage.TYPE_4BYTE_ABGR_PRE:
            return "TYPE_4BYTE_ABGR_PRE";
        case BufferedImage.TYPE_BYTE_BINARY:
            return "TYPE_BYTE_BINARY";
        case BufferedImage.TYPE_BYTE_GRAY:
            return "TYPE_BYTE_GRAY";
        case BufferedImage.TYPE_BYTE_INDEXED:
            return "TYPE_BYTE_INDEXED";
        case BufferedImage.TYPE_USHORT_555_RGB:
            return "TYPE_USHORT_555_RGB";
        case BufferedImage.TYPE_USHORT_565_RGB:
            return "TYPE_USHORT_565_RGB";
        case BufferedImage.TYPE_USHORT_GRAY:
            return "TYPE_USHORT_GRAY";
    }
    return "UNKNOWN";
}
 
Example 2
Source File: ITXtTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImage createBufferedImage() {
    BufferedImage image = new BufferedImage(128, 128,
                  BufferedImage.TYPE_4BYTE_ABGR_PRE);
    Graphics2D graph = image.createGraphics();
    graph.setPaintMode();
    graph.setColor(Color.orange);
    graph.fillRect(32, 32, 64, 64);
    graph.dispose();
    return image;
}
 
Example 3
Source File: ITXtTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImage createBufferedImage() {
    BufferedImage image = new BufferedImage(128, 128,
                  BufferedImage.TYPE_4BYTE_ABGR_PRE);
    Graphics2D graph = image.createGraphics();
    graph.setPaintMode();
    graph.setColor(Color.orange);
    graph.fillRect(32, 32, 64, 64);
    graph.dispose();
    return image;
}
 
Example 4
Source File: ColConvTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static String getImageTypeName(int type) {
    switch(type) {
        case BufferedImage.TYPE_INT_ARGB:
            return "TYPE_INT_ARGB";
        case BufferedImage.TYPE_INT_RGB:
            return "TYPE_INT_RGB";
        case BufferedImage.TYPE_INT_BGR:
            return "TYPE_INT_BGR";
        case BufferedImage.TYPE_INT_ARGB_PRE:
            return "TYPE_INT_ARGB_PRE";
        case BufferedImage.TYPE_3BYTE_BGR:
            return "TYPE_3BYTE_BGR";
        case BufferedImage.TYPE_4BYTE_ABGR:
            return "TYPE_4BYTE_ABGR";
        case BufferedImage.TYPE_4BYTE_ABGR_PRE:
            return "TYPE_4BYTE_ABGR_PRE";
        case BufferedImage.TYPE_BYTE_BINARY:
            return "TYPE_BYTE_BINARY";
        case BufferedImage.TYPE_BYTE_GRAY:
            return "TYPE_BYTE_GRAY";
        case BufferedImage.TYPE_BYTE_INDEXED:
            return "TYPE_BYTE_INDEXED";
        case BufferedImage.TYPE_USHORT_555_RGB:
            return "TYPE_USHORT_555_RGB";
        case BufferedImage.TYPE_USHORT_565_RGB:
            return "TYPE_USHORT_565_RGB";
        case BufferedImage.TYPE_USHORT_GRAY:
            return "TYPE_USHORT_GRAY";
    }
    return "UNKNOWN";
}
 
Example 5
Source File: ColConvTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static String getImageTypeName(int type) {
    switch(type) {
        case BufferedImage.TYPE_INT_ARGB:
            return "TYPE_INT_ARGB";
        case BufferedImage.TYPE_INT_RGB:
            return "TYPE_INT_RGB";
        case BufferedImage.TYPE_INT_BGR:
            return "TYPE_INT_BGR";
        case BufferedImage.TYPE_INT_ARGB_PRE:
            return "TYPE_INT_ARGB_PRE";
        case BufferedImage.TYPE_3BYTE_BGR:
            return "TYPE_3BYTE_BGR";
        case BufferedImage.TYPE_4BYTE_ABGR:
            return "TYPE_4BYTE_ABGR";
        case BufferedImage.TYPE_4BYTE_ABGR_PRE:
            return "TYPE_4BYTE_ABGR_PRE";
        case BufferedImage.TYPE_BYTE_BINARY:
            return "TYPE_BYTE_BINARY";
        case BufferedImage.TYPE_BYTE_GRAY:
            return "TYPE_BYTE_GRAY";
        case BufferedImage.TYPE_BYTE_INDEXED:
            return "TYPE_BYTE_INDEXED";
        case BufferedImage.TYPE_USHORT_555_RGB:
            return "TYPE_USHORT_555_RGB";
        case BufferedImage.TYPE_USHORT_565_RGB:
            return "TYPE_USHORT_565_RGB";
        case BufferedImage.TYPE_USHORT_GRAY:
            return "TYPE_USHORT_GRAY";
    }
    return "UNKNOWN";
}
 
Example 6
Source File: ITXtTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImage createBufferedImage() {
    BufferedImage image = new BufferedImage(128, 128,
                  BufferedImage.TYPE_4BYTE_ABGR_PRE);
    Graphics2D graph = image.createGraphics();
    graph.setPaintMode();
    graph.setColor(Color.orange);
    graph.fillRect(32, 32, 64, 64);
    graph.dispose();
    return image;
}
 
Example 7
Source File: ITXtTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImage createBufferedImage() {
    BufferedImage image = new BufferedImage(128, 128,
                  BufferedImage.TYPE_4BYTE_ABGR_PRE);
    Graphics2D graph = image.createGraphics();
    graph.setPaintMode();
    graph.setColor(Color.orange);
    graph.fillRect(32, 32, 64, 64);
    graph.dispose();
    return image;
}
 
Example 8
Source File: ITXtTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImage createBufferedImage() {
    BufferedImage image = new BufferedImage(128, 128,
                  BufferedImage.TYPE_4BYTE_ABGR_PRE);
    Graphics2D graph = image.createGraphics();
    graph.setPaintMode();
    graph.setColor(Color.orange);
    graph.fillRect(32, 32, 64, 64);
    graph.dispose();
    return image;
}
 
Example 9
Source File: GenericImageSinglePassIterator.java    From pumpernickel with MIT License 5 votes vote down vote up
public int getPixelSize() {
	switch (type) {
	case BufferedImage.TYPE_4BYTE_ABGR:
	case BufferedImage.TYPE_4BYTE_ABGR_PRE:
	case PixelIterator.TYPE_4BYTE_ARGB:
	case PixelIterator.TYPE_4BYTE_ARGB_PRE:
		return 4;
	case PixelIterator.TYPE_3BYTE_RGB:
	case BufferedImage.TYPE_3BYTE_BGR:
		return 3;
	case BufferedImage.TYPE_BYTE_GRAY:
		return 1;
	}
	throw new RuntimeException("unexpected iterator type: " + type);
}
 
Example 10
Source File: ImageValue.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static String imageType(int type) {
    switch (type) {
        case BufferedImage.TYPE_3BYTE_BGR:
            return "3BYTE_BGR";
        case BufferedImage.TYPE_4BYTE_ABGR:
            return "4BYTE_ABGR";
        case BufferedImage.TYPE_4BYTE_ABGR_PRE:
            return "4BYTE_ABGR_PRE";
        case BufferedImage.TYPE_BYTE_BINARY:
            return "BYTE_BINARY";
        case BufferedImage.TYPE_BYTE_GRAY:
            return "BYTE_GRAY";
        case BufferedImage.TYPE_BYTE_INDEXED:
            return "BYTE_INDEXED";
        case BufferedImage.TYPE_CUSTOM:
            return "CUSTOM";
        case BufferedImage.TYPE_INT_ARGB:
            return "INT_ARGB";
        case BufferedImage.TYPE_INT_ARGB_PRE:
            return "INT_ARGB_PRE";
        case BufferedImage.TYPE_INT_BGR:
            return "INT_BGR";
        case BufferedImage.TYPE_INT_RGB:
            return "INT_RGB";
        case BufferedImage.TYPE_USHORT_555_RGB:
            return "USHORT_555_RGB";
        case BufferedImage.TYPE_USHORT_565_RGB:
            return "USHORT_565_RGB";
    }
    return type + "";
}
 
Example 11
Source File: ColConvTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static String getImageTypeName(int type) {
    switch(type) {
        case BufferedImage.TYPE_INT_ARGB:
            return "TYPE_INT_ARGB";
        case BufferedImage.TYPE_INT_RGB:
            return "TYPE_INT_RGB";
        case BufferedImage.TYPE_INT_BGR:
            return "TYPE_INT_BGR";
        case BufferedImage.TYPE_INT_ARGB_PRE:
            return "TYPE_INT_ARGB_PRE";
        case BufferedImage.TYPE_3BYTE_BGR:
            return "TYPE_3BYTE_BGR";
        case BufferedImage.TYPE_4BYTE_ABGR:
            return "TYPE_4BYTE_ABGR";
        case BufferedImage.TYPE_4BYTE_ABGR_PRE:
            return "TYPE_4BYTE_ABGR_PRE";
        case BufferedImage.TYPE_BYTE_BINARY:
            return "TYPE_BYTE_BINARY";
        case BufferedImage.TYPE_BYTE_GRAY:
            return "TYPE_BYTE_GRAY";
        case BufferedImage.TYPE_BYTE_INDEXED:
            return "TYPE_BYTE_INDEXED";
        case BufferedImage.TYPE_USHORT_555_RGB:
            return "TYPE_USHORT_555_RGB";
        case BufferedImage.TYPE_USHORT_565_RGB:
            return "TYPE_USHORT_565_RGB";
        case BufferedImage.TYPE_USHORT_GRAY:
            return "TYPE_USHORT_GRAY";
    }
    return "UNKNOWN";
}
 
Example 12
Source File: GenericImageSinglePassIterator.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Returns a <code>GenericImageSinglePassIterator</code> that is either a
 * <code>IntPixelIterator</code> or a <code>BytePixelIterator</code>.
 * 
 * @param image
 *            the image to iterate over.
 * @param iteratorType
 *            one of these 8 BufferedImage types: TYPE_INT_ARGB,
 *            TYPE_INT_ARGB_PRE, TYPE_INT_RGB, TYPE_INT_BGR, TYPE_3BYTE_BGR,
 *            TYPE_BYTE_GRAY, TYPE_4BYTE_ABGR, TYPE_4BYTE_ABGR_PRE.
 * @return a <code>GenericImageSinglePassIterator</code> for the image
 *         provided.
 */
public static GenericImageSinglePassIterator get(Image image,
		int iteratorType) {
	if (!(iteratorType == BufferedImage.TYPE_INT_ARGB
			|| iteratorType == BufferedImage.TYPE_INT_ARGB_PRE
			|| iteratorType == BufferedImage.TYPE_INT_RGB
			|| iteratorType == BufferedImage.TYPE_INT_BGR
			|| iteratorType == BufferedImage.TYPE_3BYTE_BGR
			|| iteratorType == BufferedImage.TYPE_BYTE_GRAY
			|| iteratorType == BufferedImage.TYPE_4BYTE_ABGR || iteratorType == BufferedImage.TYPE_4BYTE_ABGR_PRE)) {
		throw new IllegalArgumentException("illegal iterator type: "
				+ iteratorType);
	}
	final ImageProducer producer = image.getSource();
	final Consumer consumer = new Consumer(producer, iteratorType);
	// ImageProducer.startProduction often starts its own thread, but it's
	// not
	// required to. Sometimes in my testing a BufferedImage would make
	// this a blocking call. So to be safe this call should be in its
	// own thread:
	Thread productionThread = new Thread(
			"GenericImageSinglePassIterator: Production Thread") {
		@Override
		public void run() {
			producer.startProduction(consumer);
		}
	};
	productionThread.start();
	return consumer.getPixelIterator();
}
 
Example 13
Source File: ScalingIterator.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Checks the incoming image type against the 4 byte types this class
 * supports.
 */
protected static boolean isSupportedByteType(int type) {
	return (type == BufferedImage.TYPE_3BYTE_BGR
			|| type == BufferedImage.TYPE_4BYTE_ABGR
			|| type == BufferedImage.TYPE_4BYTE_ABGR_PRE
			|| type == PixelIterator.TYPE_3BYTE_RGB
			|| type == PixelIterator.TYPE_4BYTE_ARGB
			|| type == PixelIterator.TYPE_4BYTE_ARGB_PRE || type == BufferedImage.TYPE_BYTE_GRAY);
}
 
Example 14
Source File: ITXtTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImage createBufferedImage() {
    BufferedImage image = new BufferedImage(128, 128,
                  BufferedImage.TYPE_4BYTE_ABGR_PRE);
    Graphics2D graph = image.createGraphics();
    graph.setPaintMode();
    graph.setColor(Color.orange);
    graph.fillRect(32, 32, 64, 64);
    graph.dispose();
    return image;
}
 
Example 15
Source File: QCodeService.java    From litemall with MIT License 4 votes vote down vote up
/**
 * 将商品图片,商品名字画到模版图中
 *
 * @param qrCodeImg  二维码图片
 * @param goodPicUrl 商品图片地址
 * @param goodName   商品名称
 * @return
 * @throws IOException
 */
private byte[] drawPicture(InputStream qrCodeImg, String goodPicUrl, String goodName) throws IOException {
    //底图
    ClassPathResource redResource = new ClassPathResource("back.png");
    BufferedImage red = ImageIO.read(redResource.getInputStream());


    //商品图片
    URL goodPic = new URL(goodPicUrl);
    BufferedImage goodImage = ImageIO.read(goodPic);

    //小程序二维码
    BufferedImage qrCodeImage = ImageIO.read(qrCodeImg);

    // --- 画图 ---

    //底层空白 bufferedImage
    BufferedImage baseImage = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_4BYTE_ABGR_PRE);

    //画上图片
    drawImgInImg(baseImage, red, 0, 0, red.getWidth(), red.getHeight());

    //画上商品图片
    drawImgInImg(baseImage, goodImage, 71, 69, 660, 660);

    //画上小程序二维码
    drawImgInImg(baseImage, qrCodeImage, 448, 767, 300, 300);

    //写上商品名称
    drawTextInImg(baseImage, goodName, 65, 867);

    //写上商城名称
    //        drawTextInImgCenter(baseImage, shopName, 98);


    //转jpg
    BufferedImage result = new BufferedImage(baseImage.getWidth(), baseImage
            .getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    result.getGraphics().drawImage(baseImage, 0, 0, null);
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    ImageIO.write(result, "jpg", bs);

    //最终byte数组
    return bs.toByteArray();
}
 
Example 16
Source File: ByteBGRConverter.java    From pumpernickel with MIT License 4 votes vote down vote up
public void next(byte[] dest) {
	if (byteIterator != null) {
		byteIterator.next(dest);
		byte swap;
		int rowLength = width * byteIterator.getPixelSize();

		switch (originalType) {
		case PixelIterator.TYPE_3BYTE_RGB:
			for (int a = 0; a < rowLength; a += 3) {
				swap = dest[a];
				dest[a] = dest[a + 2];
				dest[a + 2] = swap;
			}
			break;
		case BufferedImage.TYPE_3BYTE_BGR:
			break;
		case PixelIterator.TYPE_4BYTE_ARGB:
		case PixelIterator.TYPE_4BYTE_ARGB_PRE:
			for (int a = 0; a < rowLength; a += 3) {
				dest[a] = dest[a + 3];
				swap = dest[a + 1];
				dest[a + 1] = dest[a + 2];
				dest[a + 2] = swap;
			}
			break;
		case BufferedImage.TYPE_4BYTE_ABGR:
		case BufferedImage.TYPE_4BYTE_ABGR_PRE:
			for (int a = 0; a < rowLength; a += 4) {
				dest[a] = dest[a + 1];
				dest[a + 1] = dest[a + 2];
				dest[a + 2] = dest[a + 3];
			}
			break;
		case BufferedImage.TYPE_BYTE_GRAY:
			for (int a = width - 1; a >= 0; a--) {
				dest[3 * a] = dest[a];
				dest[3 * a + 1] = dest[a];
				dest[3 * a + 2] = dest[a];
			}
			break;
		case BufferedImage.TYPE_BYTE_INDEXED:
			if (rTable == null) {
				rTable = new byte[indexModel.getMapSize()];
				gTable = new byte[indexModel.getMapSize()];
				bTable = new byte[indexModel.getMapSize()];
				indexModel.getReds(rTable);
				indexModel.getGreens(gTable);
				indexModel.getBlues(bTable);
			}
			for (int a = width - 1; a >= 0; a--) {
				dest[3 * a + 2] = rTable[dest[a]];
				dest[3 * a + 1] = gTable[dest[a]];
				dest[3 * a] = bTable[dest[a]];
			}
			break;
		default:
			throw new RuntimeException("Unrecognized type ("
					+ BufferedImageIterator.getTypeName(originalType) + ")");
		}
	} else {
		if (intScratch == null) {
			intScratch = new int[intIterator.getMinimumArrayLength()];
		}
		intIterator.next(intScratch);
		switch (originalType) {
		case BufferedImage.TYPE_INT_ARGB:
		case BufferedImage.TYPE_INT_ARGB_PRE:
			// TODO: handle pre-multiplied alpha correctly
			// (see ByteBGRAConverter for example)
		case BufferedImage.TYPE_INT_RGB:
			for (int a = 0; a < width; a++) {
				dest[3 * a + 2] = (byte) ((intScratch[a] >> 16) & 0xff);
				dest[3 * a + 1] = (byte) ((intScratch[a] >> 8) & 0xff);
				dest[3 * a] = (byte) ((intScratch[a]) & 0xff);
			}
			break;
		case BufferedImage.TYPE_INT_BGR:
			for (int a = 0; a < width; a++) {
				dest[3 * a + 2] = (byte) ((intScratch[a]) & 0xff);
				dest[3 * a + 1] = (byte) ((intScratch[a] >> 8) & 0xff);
				dest[3 * a] = (byte) ((intScratch[a] >> 16) & 0xff);
			}
			break;
		default:
			throw new RuntimeException("Unrecognized type ("
					+ BufferedImageIterator.getTypeName(originalType) + ")");
		}
	}
}
 
Example 17
Source File: QCodeService.java    From BigDataPlatform with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 将商品图片,商品名字画到模版图中
 *
 * @param qrCodeImg  二维码图片
 * @param goodPicUrl 商品图片地址
 * @param goodName   商品名称
 * @return
 * @throws IOException
 */
private byte[] drawPicture(InputStream qrCodeImg, String goodPicUrl, String goodName) throws IOException {
    //底图
    ClassPathResource redResource = new ClassPathResource("back.png");
    BufferedImage red = ImageIO.read(redResource.getInputStream());


    //商品图片
    URL goodPic = new URL(goodPicUrl);
    BufferedImage goodImage = ImageIO.read(goodPic);

    //小程序二维码
    BufferedImage qrCodeImage = ImageIO.read(qrCodeImg);

    // --- 画图 ---

    //底层空白 bufferedImage
    BufferedImage baseImage = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_4BYTE_ABGR_PRE);

    //画上图片
    drawImgInImg(baseImage, red, 0, 0, red.getWidth(), red.getHeight());

    //画上商品图片
    drawImgInImg(baseImage, goodImage, 71, 69, 660, 660);

    //画上小程序二维码
    drawImgInImg(baseImage, qrCodeImage, 448, 767, 300, 300);

    //写上商品名称
    drawTextInImg(baseImage, goodName, 65, 867);

    //写上商城名称
    //        drawTextInImgCenter(baseImage, shopName, 98);


    //转jpg
    BufferedImage result = new BufferedImage(baseImage.getWidth(), baseImage
            .getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    result.getGraphics().drawImage(baseImage, 0, 0, null);
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    ImageIO.write(result, "jpg", bs);

    //最终byte数组
    return bs.toByteArray();
}
 
Example 18
Source File: QCodeService.java    From mall with MIT License 4 votes vote down vote up
/**
     * 将商品图片,商品名字画到模版图中
     *
     * @param qrCodeImg  二维码图片
     * @param goodPicUrl 商品图片地址
     * @param goodName   商品名称
     * @return
     * @throws IOException
     */
    private byte[] drawPicture(InputStream qrCodeImg, String goodPicUrl, String goodName) throws IOException {
        //底图
        ClassPathResource redResource = new ClassPathResource("back.png");
        BufferedImage red = ImageIO.read(redResource.getInputStream());


        //商品图片
        URL goodPic = new URL(goodPicUrl);
        BufferedImage goodImage = ImageIO.read(goodPic);

        //小程序二维码
        BufferedImage qrCodeImage = ImageIO.read(qrCodeImg);

        // --- 画图 ---

        //底层空白 bufferedImage
        BufferedImage baseImage = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_4BYTE_ABGR_PRE);

        //画上图片
        drawImgInImg(baseImage, red, 0, 0, red.getWidth(), red.getHeight());

        //画上商品图片
        drawImgInImg(baseImage, goodImage, 71, 69, 660, 660);

        //画上小程序二维码
        drawImgInImg(baseImage, qrCodeImage, 448, 767, 300, 300);

        //写上商品名称
        drawTextInImg(baseImage, goodName, 65, 867);

        //写上商城名称
//        drawTextInImgCenter(baseImage, shopName, 98);


        //转jpg
        BufferedImage result = new BufferedImage(baseImage.getWidth(), baseImage
                .getHeight(), BufferedImage.TYPE_3BYTE_BGR);
        result.getGraphics().drawImage(baseImage, 0, 0, null);
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ImageIO.write(result, "jpg", bs);

        //最终byte数组
        return bs.toByteArray();
    }
 
Example 19
Source File: Image.java    From JavaAV with GNU General Public License v2.0 4 votes vote down vote up
public static PixelFormat getPixelFormat(BufferedImage image) {
	switch (image.getType()) {
		case BufferedImage.TYPE_3BYTE_BGR:
			return PixelFormat.BGR24;

		case BufferedImage.TYPE_4BYTE_ABGR:
		case BufferedImage.TYPE_4BYTE_ABGR_PRE:
			return PixelFormat.ABGR;

		case BufferedImage.TYPE_BYTE_BINARY:
			return PixelFormat.RGB444;

		case BufferedImage.TYPE_BYTE_INDEXED:
			return PixelFormat.RGB555;

		case BufferedImage.TYPE_BYTE_GRAY:
			return PixelFormat.GRAY8;

		case BufferedImage.TYPE_INT_ARGB:
		case BufferedImage.TYPE_INT_ARGB_PRE:
			return PixelFormat.ARGB;

		case BufferedImage.TYPE_INT_RGB:
			return PixelFormat.RGB24;

		case BufferedImage.TYPE_USHORT_GRAY:
			return PixelFormat.GRAY16;

		case BufferedImage.TYPE_USHORT_555_RGB:
			return PixelFormat.RGB555;

		case BufferedImage.TYPE_USHORT_565_RGB:
			return PixelFormat.RGB565;

		case BufferedImage.TYPE_CUSTOM:
			return null;

		default:
			return null;
	}
}
 
Example 20
Source File: QCodeService.java    From dts-shop with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 将商品图片,商品名字画到模版图中
 *
 * @param qrCodeImg
 *            二维码图片
 * @param goodPicUrl
 *            商品图片地址
 * @param goodName
 *            商品名称
 * @return
 * @throws IOException
 */
private byte[] drawPicture(InputStream qrCodeImg, String goodPicUrl, String goodName,BigDecimal counterPrice,BigDecimal retailPrice) throws IOException {
	// 底图
	ClassPathResource redResource = new ClassPathResource("back.png");
	BufferedImage red = ImageIO.read(redResource.getInputStream());

	// 商品图片
	URL goodPic = new URL(goodPicUrl);
	BufferedImage goodImage = ImageIO.read(goodPic);

	// 小程序二维码
	BufferedImage qrCodeImage = ImageIO.read(qrCodeImg);

	// --- 画图 ---

	// 底层空白 bufferedImage
	BufferedImage baseImage = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_4BYTE_ABGR_PRE);

	// 画上图片
	drawImgInImg(baseImage, red, 0, 0, red.getWidth(), red.getHeight());

	// 画上商品图片
	drawImgInImg(baseImage, goodImage, 71, 69, 660, 660);

	// 画上小程序二维码
	drawImgInImg(baseImage, qrCodeImage, 448, 767, 300, 300);

	// 写上商品名称,截取前面的12个字符长度
	if (goodName.length() > 12) {
		goodName = goodName.substring(0, 12) + "...";
	}
	Color colorComm = new Color(60, 60, 60);
	drawTextInImg(baseImage, goodName, 65, 867, colorComm, 28);

	Color priceColor = new Color(240, 20, 20);
	drawTextInImg(baseImage, "现价  ", 65, 787, colorComm, 24);
	drawTextInImg(baseImage, "¥ ", 120, 787, priceColor, 24);
	drawTextInImg(baseImage, retailPrice.toString(), 145, 787, priceColor, 34);
	drawStrikethroughTextInImg(baseImage, counterPrice.toString(), 275, 787, colorComm, 24);
	
	
	// 写上商城名称
	// drawTextInImgCenter(baseImage, shopName, 98);

	// 转jpg
	BufferedImage result = new BufferedImage(baseImage.getWidth(), baseImage.getHeight(),
			BufferedImage.TYPE_3BYTE_BGR);
	result.getGraphics().drawImage(baseImage, 0, 0, null);
	ByteArrayOutputStream bs = new ByteArrayOutputStream();
	ImageIO.write(result, "jpg", bs);

	// 最终byte数组
	return bs.toByteArray();
}