Java Utililty Methods ByteBuffer Size

List of utility methods to do ByteBuffer Size

Description

The list of methods to do ByteBuffer Size are organized into topic(s).

Method

intFindResolution(BufferedImage img, double print_width, double print_height)
Find Resolution
int img_width = img.getWidth();
int img_height = img.getHeight();
System.out.println("Buffered Image Width: " + img_width + " Image Ht: " + img_height);
int ppi1 = (int) ((double) img_width / (double) print_width);
int ppi2 = (int) ((double) img_height / (double) print_height);
int ppi = Math.min(ppi1, ppi2);
System.out.println(" PPi1 " + ppi1 + " PPi2 " + ppi2 + " PPi " + ppi);
return (ppi);
...
BufferedImagefitToSize(BufferedImage source, int targetWidth, int targetHeight)
Scale the image with original aspect ratio, then cut to fit the target size if necessary.
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
if (sourceWidth == targetWidth && sourceHeight == targetHeight) {
    return source;
int sx1 = 0;
int sy1 = 0;
int sx2 = sourceWidth;
...
BufferedImagegenerateScaledImage(final BufferedImage bufImg, @SuppressWarnings("unused") final Object hintsArg, final int size)
generate Scaled Image
BufferedImage sourceImage = bufImg;
int srcWidth = sourceImage.getWidth();
int srcHeight = sourceImage.getHeight();
double longSideForSource = Math.max(srcWidth, srcHeight);
double longSideForDest = size;
double multiplier = longSideForDest / longSideForSource;
int destWidth = (int) (srcWidth * multiplier);
int destHeight = (int) (srcHeight * multiplier);
...
voidmakeBrighterRectangleOnImage(BufferedImage image, int leftPosition, int topPosition, int width, int height, double scaleFactor, Color borderColor, Stroke borderStroke)
make Brighter Rectangle On Image
double invertedScaleFactor = 1. / scaleFactor;
int scaledLeftPosition = (int) (invertedScaleFactor * leftPosition);
int scaledTopPosition = (int) (invertedScaleFactor * topPosition);
int scaledWidth = (int) (invertedScaleFactor * width);
int scaledHeight = (int) (invertedScaleFactor * height);
for (int iw = scaledLeftPosition; iw < scaledLeftPosition + scaledWidth; ++iw) {
    for (int ih = scaledTopPosition; ih < scaledTopPosition + scaledHeight; ++ih) {
        Color color = new Color(image.getRGB(iw, ih));
...
BufferedImagemakeOval(BufferedImage input, int width, int height)
Crop the image to a smooth oval
BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = output.createGraphics();
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fill(new Ellipse2D.Float(0, 0, width, height));
g.setComposite(AlphaComposite.SrcAtop);
g.drawImage(input, 0, 0, null);
...
int[][]makeTarget(BufferedImage image, int x, int y, int width, int height)
make Target
int[][] target = new int[height][width];
for (int i = 0; i < height; ++i) {
    for (int j = 0; j < width; ++j) {
        int argb = image.getRGB(j + x, i + y);
        int a = (argb >> 24) & 0xFF;
        int r = (argb >> 16) & 0xFF;
        int g = (argb >> 8) & 0xFF;
        int b = argb & 0xFF;
...
ByteBuffermapByteBuffer(File cachedFile, int cacheFileSize)
map Byte Buffer
ByteBuffer byteBufferPositions = null;
if (cacheFileSize > 1024) 
    try {
        if (cachedFile.canRead() && cachedFile.length() == cacheFileSize) {
            RandomAccessFile raf = new RandomAccessFile(cachedFile, "r");
            byteBufferPositions = raf.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, cacheFileSize);
            return byteBufferPositions;
        if (cachedFile.exists())
            cachedFile.delete();
        cachedFile.getParentFile().mkdirs();
        cachedFile.createNewFile();
        if (cachedFile.canWrite()) {
            RandomAccessFile raf = new RandomAccessFile(cachedFile, "rw");
            byteBufferPositions = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, cacheFileSize);
            return byteBufferPositions;
    } catch (Exception e) {
byteBufferPositions = ByteBuffer.allocate(cacheFileSize); 
return byteBufferPositions;
BufferedImagemarkDifferencesWithAMarker(final BufferedImage image, final Point[] pixels, final int markingSizeX, final int markingSizeY)
Method to mark areas around the detected differences.
if (pixels == null) {
    return null;
final BufferedImage imageCopy = copyImage(image);
final Color highlighterColor = new Color(228, 252, 90, 50);
final Color pixelEmphasizeColor = new Color(228, 0, 0);
final Graphics2D g = imageCopy.createGraphics();
g.setColor(highlighterColor);
...
BufferedImagemarkDifferencesWithBoxes(final BufferedImage image, final Point[] pixels, final int markingSizeX, final int markingSizeY)
Method to mark areas around the detected differences.
if (pixels == null) {
    return null;
final BufferedImage copy = copyImage(image);
if (markingSizeX == 1 || markingSizeY == 1) {
    for (final Point pixel : pixels) {
        colorPixel(copy, pixel.x, pixel.y, null);
    return copy;
final int imageWidth = copy.getWidth();
final int imageHeight = copy.getHeight();
final int blocksX = imageWidth / markingSizeX;
final int blocksY = imageHeight / markingSizeY;
final boolean[][] markedBlocks = new boolean[blocksX + 1][blocksY + 1];
int xBlock, yBlock, subImageWidth, subImageHeight;
for (final Point pixel : pixels) {
    xBlock = pixel.x / markingSizeX;
    yBlock = pixel.y / markingSizeY;
    subImageWidth = calcBlockLength(markingSizeX, xBlock, imageWidth);
    subImageHeight = calcBlockLength(markingSizeY, yBlock, imageHeight);
    if (!markedBlocks[xBlock][yBlock]) {
        drawBorders(copy, xBlock, yBlock, markingSizeX, markingSizeY, subImageWidth, subImageHeight, null);
        markedBlocks[xBlock][yBlock] = true;
return copy;
ListmaybeGrow(List l, int size)
maybe Grow
if (l.size() >= size)
    return l;
List<ByteBuffer> nl = new ArrayList<>(size);
nl.addAll(l);
for (int i = l.size(); i < size; i++)
    nl.add(null);
return nl;