将一张图片转换成指定格式的Base64字符串编码

@param image      指定一张图片
@param formatName 图片格式名,如gif,png,jpg,jpeg等,默认为jpeg
@return 返回编码好的字符串, 失败返回null
164942
public static String imgToBase64Str(BufferedImage image, String formatName) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        String base64Str = null;
        try {
            ImageIO.write(image, formatName == null ? "jpeg" : formatName, baos);
            byte[] bytes = baos.toByteArray();
            base64Str = CommonUtils.bytesToBase64Str(bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            CommonUtils.closeIOStream(null, baos);
        }
        return base64Str;
    }
90534
public JSONObject imageCensorUserDefined(byte[] imgData, HashMap<String, String> options) {
        AipRequest request = new AipRequest();

        String base64Content = Base64Util.encode(imgData);
        request.addBody("image", base64Content);

        return imageCensorUserDefinedHelper(request, options);
    }
986770
private function setImageFormat($image, $format, $backgroundColor='#FFFFFF')
    {
        if ($image->getImageFormat() !== $format) {
            if ($format == 'jpg' || $format == 'jpeg') {
                $image->setImageBackgroundColor(new \ImagickPixel($backgroundColor));
                $image = $image->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);
            }

            $image->setImageFormat($format);
        }
        if ($format == 'jpeg') {
            $image->setImageCompressionQuality(90);
        }

        return $image;
    }
584510
public ThumborUrlBuilder buildImage(String image) {
    if (image == null || image.length() == 0) {
      throw new IllegalArgumentException("Image must not be blank.");
    }
    return new ThumborUrlBuilder(host, key, image);
  }
1143470
public static byte[] getBufferByteArray(BufferedImage image) {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try {
			ImageIO.write(image, "png", os);
		} catch (IOException e) {
			logger.error("getBufferByteArray error", e);
		}
		return os.toByteArray();
	}
1131417
private Bitmap  checkForImageDataURL(String url)
   {
      if (!url.startsWith("data:"))
         return null;
      if (url.length() < 14)
         return null;

      int  comma = url.indexOf(',');
      if (comma == -1 || comma < 12)
         return null;
      if (!";base64".equals(url.substring(comma-7, comma)))
         return null;
      byte[]  imageData = Base64.decode(url.substring(comma+1), Base64.DEFAULT);
      return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
   }
880941
public void setImage(final Image image) {
		LinkModel model = getOrCreateComponentModel();
		model.image = image;
		model.imageUrl = null;
	}
1186599
public static String format(ImageFormat format) {
    if (format == null) {
      throw new IllegalArgumentException("You must specify an image format.");
    }
    return FILTER_FORMAT + "(" + format.value + ")";
  }
1115374
public static String getImageFileFormat(File imageFile) {
        String imageFileName = imageFile.getName();
        String imageFormat = imageFileName.substring(imageFileName.lastIndexOf('.') + 1);
        if (imageFormat.matches("(pbm|pgm|ppm)")) {
            imageFormat = "pnm";
        } else if (imageFormat.matches("(jp2|j2k|jpf|jpx|jpm)")) {
            imageFormat = "jpeg2000";
        }
        return imageFormat;
    }
1176951
protected Graphics2D getGraphics(BufferedImage image) {
		Graphics2D graphics = image.createGraphics();
		graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		return graphics;
	}
417239
public static BufferedImage convertImageToBinary(BufferedImage image) {
        BufferedImage tmp = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
        Graphics2D g2 = tmp.createGraphics();
        g2.drawImage(image, 0, 0, null);
        g2.dispose();
        return tmp;
    }
1371464
public static BufferedImage createBufferedImage(int width, int height,
			String imageFormat) {

		int imageType;

		switch (imageFormat.toLowerCase()) {
		case IMAGE_FORMAT_JPG:
		case IMAGE_FORMAT_JPEG:
			imageType = BufferedImage.TYPE_INT_RGB;
			break;
		default:
			imageType = BufferedImage.TYPE_INT_ARGB;
		}

		BufferedImage image = new BufferedImage(width, height, imageType);

		return image;
	}
925753
public static byte[] bufferedImageToJPGByteArray(BufferedImage image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "jpg", baos);
            //baos.flush();
            byte[] imageInByte = baos.toByteArray();
            //baos.close();
            return imageInByte;
        } catch (IOException ex) {
            Logger.getLogger(PHATImageUtils.class.getName()).log(Level.SEVERE, "bufferedImageToByteArray", ex);
        }
        return null;
    }
710883
public static BufferedImage convertImageToARGB( Image image ) {
		if ( image instanceof BufferedImage && ((BufferedImage)image).getType() == BufferedImage.TYPE_INT_ARGB )
			return (BufferedImage)image;
		BufferedImage p = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = p.createGraphics();
		g.drawImage( image, 0, 0, null );
		g.dispose();
		return p;
	}
803383
public static BufferedImage toBufferedImage(Image image, String imageType) {
		BufferedImage bufferedImage;
		if (false == imageType.equalsIgnoreCase(IMAGE_TYPE_PNG)) {
			// 当目标为非PNG类图片时，源图片统一转换为RGB格式
			if (image instanceof BufferedImage) {
				bufferedImage = (BufferedImage) image;
				if (BufferedImage.TYPE_INT_RGB != bufferedImage.getType()) {
					bufferedImage = copyImage(image, BufferedImage.TYPE_INT_RGB);
				}
			} else {
				bufferedImage = copyImage(image, BufferedImage.TYPE_INT_RGB);
			}
		} else {
			bufferedImage = toBufferedImage(image);
		}
		return bufferedImage;
	}
1274809
public static BufferedImage flipHorizontal(BufferedImage image)
    {
        final int width = image.getWidth();
        final int height = image.getHeight();
        final BufferedImage flipped = createImage(width, height, image.getColorModel().getTransparency());
        final Graphics2D g = flipped.createGraphics();

        optimizeGraphicsSpeed(g);
        g.drawImage(image, 0, 0, width, height, width, 0, 0, height, null);
        g.dispose();

        return flipped;
    }
1062253
public static byte[] writeImageToBytes(BufferedImage image,
			String formatName) throws IOException {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		ImageIO.write(image, formatName, stream);
		stream.flush();
		byte[] bytes = stream.toByteArray();
		stream.close();
		return bytes;
	}