下面列出了java.awt.image.BufferedImage#TYPE_BYTE_GRAY 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static String describeType(int type) {
switch(type) {
case BufferedImage.TYPE_3BYTE_BGR:
return "TYPE_3BYTE_BGR";
case BufferedImage.TYPE_4BYTE_ABGR:
return "TYPE_4BYTE_ABGR";
case BufferedImage.TYPE_BYTE_GRAY:
return "TYPE_BYTE_GRAY";
case BufferedImage.TYPE_INT_ARGB:
return "TYPE_INT_ARGB";
case BufferedImage.TYPE_INT_BGR:
return "TYPE_INT_BGR";
case BufferedImage.TYPE_INT_RGB:
return "TYPE_INT_RGB";
case BufferedImage.TYPE_BYTE_INDEXED:
return "TYPE_BYTE_INDEXED";
default:
throw new RuntimeException("Test FAILED: unknown type " + type);
}
}
private BufferedImage buildPattern (int cols,
int rows,
int dx,
int dy)
{
int width = cols * dx;
int height = rows * dy;
BufferedImage img = new BufferedImage(
width,
height,
BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = (Graphics2D) img.getGraphics();
g.setColor(Color.BLACK);
for (int ir = 0; ir < rows; ir++) {
int y = ir * dy;
for (int ic = 0; ic < cols; ic++) {
int x = ic * dx;
g.setColor((((ir + ic) % 2) == 0) ? Color.GRAY : Color.WHITE);
g.fillRect(x, y, dx, dy);
}
}
return img;
}
/**
* <p>Returns an array of pixels, stored as integers, from a <code>BufferedImage</code>. The pixels are grabbed from
* a rectangular area defined by a location and two dimensions. Calling this method on an image of type different
* from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the
* image.</p>
*
* @param img the source image
* @param x the x location at which to start grabbing pixels
* @param y the y location at which to start grabbing pixels
* @param w the width of the rectangle of pixels to grab
* @param h the height of the rectangle of pixels to grab
* @param pixels a pre-allocated array of pixels of size w*h; can be null
* @return <code>pixels</code> if non-null, a new array of integers otherwise
* @throws IllegalArgumentException is <code>pixels</code> is non-null and of length < w*h
*/
static byte[] getPixels(BufferedImage img,
int x, int y, int w, int h, byte[] pixels) {
if (w == 0 || h == 0) {
return new byte[0];
}
if (pixels == null) {
pixels = new byte[w * h];
} else if (pixels.length < w * h) {
throw new IllegalArgumentException("pixels array must have a length >= w*h");
}
int imageType = img.getType();
if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
Raster raster = img.getRaster();
return (byte[]) raster.getDataElements(x, y, w, h, pixels);
} else {
throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
}
}
/**
* <p>Writes a rectangular area of pixels in the destination <code>BufferedImage</code>. Calling this method on an
* image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code>
* will unmanage the image.</p>
*
* @param img the destination image
* @param x the x location at which to start storing pixels
* @param y the y location at which to start storing pixels
* @param w the width of the rectangle of pixels to store
* @param h the height of the rectangle of pixels to store
* @param pixels an array of pixels, stored as integers
* @throws IllegalArgumentException is <code>pixels</code> is non-null and of length < w*h
*/
static void setPixels(BufferedImage img,
int x, int y, int w, int h, byte[] pixels) {
if (pixels == null || w == 0 || h == 0) {
return;
} else if (pixels.length < w * h) {
throw new IllegalArgumentException("pixels array must have a length >= w*h");
}
int imageType = img.getType();
if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
WritableRaster raster = img.getRaster();
raster.setDataElements(x, y, w, h, pixels);
} else {
throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
}
}
public Wiz4Image (String name, byte[] buffer, int rows, int cols) // 5, 6
// ---------------------------------------------------------------------------------//
{
super (name, buffer);
image = new BufferedImage (cols * 7, rows * 8, BufferedImage.TYPE_BYTE_GRAY);
DataBuffer db = image.getRaster ().getDataBuffer ();
int element = 0;
int rowSize = cols * 8;
for (int row = 0; row < rows; row++)
for (int line = 0; line < 8; line++)
for (int col = 0; col < cols; col++)
{
byte b = buffer[row * rowSize + col * 8 + line];
for (int bit = 0; bit < 7; bit++)
{
if ((b & 0x01) == 0x01)
db.setElem (element, 255);
b >>>= 1;
element++;
}
}
}
private static BufferedImage getAlphaImage(BufferedImage image)
{
if (!image.getColorModel().hasAlpha())
{
return null;
}
if (image.getTransparency() == Transparency.BITMASK)
{
throw new UnsupportedOperationException("BITMASK Transparency JPEG compression is not"
+ " useful, use LosslessImageFactory instead");
}
WritableRaster alphaRaster = image.getAlphaRaster();
if (alphaRaster == null)
{
// happens sometimes (PDFBOX-2654) despite colormodel claiming to have alpha
return null;
}
BufferedImage alphaImage = new BufferedImage(image.getWidth(), image.getHeight(),
BufferedImage.TYPE_BYTE_GRAY);
alphaImage.setData(alphaRaster);
return alphaImage;
}
public BufferedImageLuminanceSource(BufferedImage image, int left,
int top, int width, int height) {
super(width, height);
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
if (left + width > sourceWidth || top + height > sourceHeight) {
throw new IllegalArgumentException(
"Crop rectangle does not fit within image data.");
}
for (int y = top; y < top + height; y++) {
for (int x = left; x < left + width; x++) {
if ((image.getRGB(x, y) & 0xFF000000) == 0) {
image.setRGB(x, y, 0xFFFFFFFF); // = white
}
}
}
this.image = new BufferedImage(sourceWidth, sourceHeight,
BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image, 0, 0, null);
this.left = left;
this.top = top;
}
/**
* <p>Returns an array of pixels, stored as integers, from a <code>BufferedImage</code>. The pixels are grabbed from
* a rectangular area defined by a location and two dimensions. Calling this method on an image of type different
* from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the
* image.</p>
*
* @param img the source image
* @param x the x location at which to start grabbing pixels
* @param y the y location at which to start grabbing pixels
* @param w the width of the rectangle of pixels to grab
* @param h the height of the rectangle of pixels to grab
* @param pixels a pre-allocated array of pixels of size w*h; can be null
* @return <code>pixels</code> if non-null, a new array of integers otherwise
* @throws IllegalArgumentException is <code>pixels</code> is non-null and of length < w*h
*/
static byte[] getPixels(BufferedImage img,
int x, int y, int w, int h, byte[] pixels) {
if (w == 0 || h == 0) {
return new byte[0];
}
if (pixels == null) {
pixels = new byte[w * h];
} else if (pixels.length < w * h) {
throw new IllegalArgumentException("pixels array must have a length >= w*h");
}
int imageType = img.getType();
if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
Raster raster = img.getRaster();
return (byte[]) raster.getDataElements(x, y, w, h, pixels);
} else {
throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
}
}
private static String describeType(int type) {
switch(type) {
case BufferedImage.TYPE_3BYTE_BGR:
return "TYPE_3BYTE_BGR";
case BufferedImage.TYPE_4BYTE_ABGR:
return "TYPE_4BYTE_ABGR";
case BufferedImage.TYPE_BYTE_GRAY:
return "TYPE_BYTE_GRAY";
case BufferedImage.TYPE_INT_ARGB:
return "TYPE_INT_ARGB";
case BufferedImage.TYPE_INT_BGR:
return "TYPE_INT_BGR";
case BufferedImage.TYPE_INT_RGB:
return "TYPE_INT_RGB";
case BufferedImage.TYPE_BYTE_INDEXED:
return "TYPE_BYTE_INDEXED";
default:
throw new RuntimeException("Test FAILED: unknown type " + type);
}
}
/**
* <p>Writes a rectangular area of pixels in the destination <code>BufferedImage</code>. Calling this method on an
* image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code>
* will unmanage the image.</p>
*
* @param img the destination image
* @param x the x location at which to start storing pixels
* @param y the y location at which to start storing pixels
* @param w the width of the rectangle of pixels to store
* @param h the height of the rectangle of pixels to store
* @param pixels an array of pixels, stored as integers
* @throws IllegalArgumentException is <code>pixels</code> is non-null and of length < w*h
*/
static void setPixels(BufferedImage img,
int x, int y, int w, int h, byte[] pixels) {
if (pixels == null || w == 0 || h == 0) {
return;
} else if (pixels.length < w * h) {
throw new IllegalArgumentException("pixels array must have a length >= w*h");
}
int imageType = img.getType();
if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
WritableRaster raster = img.getRaster();
raster.setDataElements(x, y, w, h, pixels);
} else {
throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
}
}
static String getImageTypeName(int type) {
switch(type) {
case BufferedImage.TYPE_INT_RGB:
return "TYPE_INT_RGB";
case BufferedImage.TYPE_3BYTE_BGR:
return "TYPE_3BYTE_BGR";
case BufferedImage.TYPE_USHORT_555_RGB:
return "TYPE_USHORT_555_RGB";
case BufferedImage.TYPE_BYTE_GRAY:
return "TYPE_BYTE_GRAY";
case BufferedImage.TYPE_BYTE_BINARY:
return "TYPE_BYTE_BINARY";
default:
return "TBD";
}
}
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";
}
private void reconstruct (Shape shape,
MomentsExtractor<D> extractor)
{
int size = 200;
BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_BYTE_GRAY);
WritableRaster raster = img.getRaster();
extractor.reconstruct(raster);
try {
ImageIO.write(img, "png", new File(temp, shape + ".png"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static BufferedImage convertToBufferedImage(Mat mat) {
byte[] data = new byte[mat.cols() * mat.rows() * (int) mat.elemSize()];
mat.get(0, 0, data);
BufferedImage image = new BufferedImage(
mat.cols(),
mat.rows(),
mat.channels() == 1
? BufferedImage.TYPE_BYTE_GRAY
: BufferedImage.TYPE_3BYTE_BGR);
image.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), data);
return image;
}
public TextView(String str)
{
super(TAG_NAME);
text = new StringProperty("Text", "android:text", "");
if (str != null)
{
text.setStringValue(str);
}
hint = new StringProperty("Default Text", "android:hint", "");
fontSz = new StringProperty("Font Size", "android:textSize", fontSize
+ "sp");
face = new SelectProperty("Font Face", "android:typeface",
new String[] { "normal", "sans", "serif", "monospace" }, 0);
style = new SelectProperty("Font Style", "android:textStyle",
new String[] { "normal", "bold", "italic", "bold|italic" }, 0);
textColor = new ColorProperty("Text Color", "android:textColor", null);
align = new SelectProperty("Text Alignment", "android:gravity",
new String[] { "left", "center", "right" }, 2);
props.add(text);
props.add(hint);
props.add(fontSz);
props.add(face);
props.add(style);
props.add(textColor);
props.add(align);
osx = (System.getProperty("os.name").toLowerCase().contains("mac os x"));
buildFont();
_tempUsedForMessureStringWidth = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY);
bgInTools = WidgetDefaultNPIconFactory.getInstance().getTextView_normal();
apply();
}
public static void init() {
destroot = new Group.EnableSet(TestEnvironment.globaloptroot,
"dest", "Output Destination Options");
new Screen();
new OffScreen();
if (GraphicsTests.hasGraphics2D) {
if (ImageTests.hasCompatImage) {
compatimgdestroot =
new Group.EnableSet(destroot, "compatimg",
"Compatible Image Destinations");
compatimgdestroot.setHorizontal();
new CompatImg();
new CompatImg(Transparency.OPAQUE);
new CompatImg(Transparency.BITMASK);
new CompatImg(Transparency.TRANSLUCENT);
}
if (ImageTests.hasVolatileImage) {
new VolatileImg();
}
bufimgdestroot = new Group.EnableSet(destroot, "bufimg",
"BufferedImage Destinations");
new BufImg(BufferedImage.TYPE_INT_RGB);
new BufImg(BufferedImage.TYPE_INT_ARGB);
new BufImg(BufferedImage.TYPE_INT_ARGB_PRE);
new BufImg(BufferedImage.TYPE_3BYTE_BGR);
new BufImg(BufferedImage.TYPE_BYTE_INDEXED);
new BufImg(BufferedImage.TYPE_BYTE_GRAY);
new CustomImg();
}
}
private Mat bufferedImageToMat(BufferedImage bi) {
Mat mat;
if (bi.getType() == BufferedImage.TYPE_BYTE_GRAY) {
mat = new Mat(bi.getHeight(), bi.getWidth(), CV_8UC1);
} else {
mat = new Mat(bi.getHeight(), bi.getWidth(), CV_8UC3);
}
byte[] data = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
mat.data().put(data);
return mat;
}
boolean save(final BufferedImage bi, final String path, final float quality, final boolean as_grey) {
switch (bi.getType()) {
case BufferedImage.TYPE_BYTE_GRAY:
return save(new ByteProcessor(bi), path, quality, false);
default:
if (as_grey) return save(new ByteProcessor(bi), path, quality, false);
return save(new ColorProcessor(bi), path, quality, false);
}
}
public static void init() {
pixelroot = new Group("pixel", "Pixel Access Benchmarks");
pixeloptroot = new Group(pixelroot, "opts", "Pixel Access Options");
doRenderTo = new Option.Toggle(pixeloptroot, "renderto",
"Render to Image before test",
Option.Toggle.Off);
doRenderFrom = new Option.Toggle(pixeloptroot, "renderfrom",
"Render from Image before test",
Option.Toggle.Off);
// BufferedImage Sources
bufimgsrcroot = new Group.EnableSet(pixelroot, "src",
"BufferedImage Sources");
new BufImg(BufferedImage.TYPE_BYTE_BINARY, 1);
new BufImg(BufferedImage.TYPE_BYTE_BINARY, 2);
new BufImg(BufferedImage.TYPE_BYTE_BINARY, 4);
new BufImg(BufferedImage.TYPE_BYTE_INDEXED);
new BufImg(BufferedImage.TYPE_BYTE_GRAY);
new BufImg(BufferedImage.TYPE_USHORT_555_RGB);
new BufImg(BufferedImage.TYPE_USHORT_565_RGB);
new BufImg(BufferedImage.TYPE_USHORT_GRAY);
new BufImg(BufferedImage.TYPE_3BYTE_BGR);
new BufImg(BufferedImage.TYPE_4BYTE_ABGR);
new BufImg(BufferedImage.TYPE_INT_RGB);
new BufImg(BufferedImage.TYPE_INT_BGR);
new BufImg(BufferedImage.TYPE_INT_ARGB);
// BufferedImage Tests
bufimgtestroot = new Group(pixelroot, "bimgtests",
"BufferedImage Tests");
new BufImgTest.GetRGB();
new BufImgTest.SetRGB();
// Raster Tests
rastertestroot = new Group(pixelroot, "rastests",
"Raster Tests");
new RasTest.GetDataElements();
new RasTest.SetDataElements();
new RasTest.GetPixel();
new RasTest.SetPixel();
// DataBuffer Tests
dbtestroot = new Group(pixelroot, "dbtests",
"DataBuffer Tests");
new DataBufTest.GetElem();
new DataBufTest.SetElem();
}
@Override
public Response getDossierBarcode(HttpServletRequest request, HttpHeaders header, Company company, Locale locale,
User user, ServiceContext serviceContext, String id) {
long groupId = GetterUtil.getLong(header.getHeaderString("groupId"));
try {
long dossierId = GetterUtil.getLong(id);
Dossier dossier = DossierLocalServiceUtil.fetchDossier(dossierId);
if (dossier == null) {
dossier = DossierLocalServiceUtil.getByRef(groupId, id);
}
Code128 barcode = new Code128();
barcode.setFontName("Monospaced");
barcode.setFontSize(16);
barcode.setModuleWidth(2);
barcode.setBarHeight(50);
barcode.setHumanReadableLocation(HumanReadableLocation.BOTTOM);
barcode.setContent(dossier.getDossierNo());
int width = barcode.getWidth();
int height = barcode.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2d = image.createGraphics();
Java2DRenderer renderer = new Java2DRenderer(g2d, 1, Color.WHITE, Color.BLACK);
renderer.render(barcode);
File destDir = new File("barcode");
if (!destDir.exists()) {
destDir.mkdir();
}
File file = new File("barcode/" + dossier.getDossierId() + ".png");
if (!file.exists()) {
file.createNewFile();
}
if (file.exists()) {
ImageIO.write(image, "png", file);
// String fileType = Files.probeContentType(file.toPath());
ResponseBuilder responseBuilder = Response.ok((Object) file);
responseBuilder.header("Content-Disposition",
"attachment; filename=\"" + file.getName() + "\"");
responseBuilder.header("Content-Type", "image/png");
return responseBuilder.build();
} else {
return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
}
} catch (Exception e) {
return BusinessExceptionImpl.processException(e);
}
}