use of java.awt.Rectangle in project jna by java-native-access.
the class AlphaMaskDemo2 method centerOnScreen.
/** Center the given {@link Window} on the default screen. */
private static void centerOnScreen(Window window) {
Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
Rectangle max = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
int x = Math.max(center.x - Math.round(window.getWidth() / 2f), max.x);
int y = Math.max(center.y - Math.round(window.getHeight() / 2f), max.y);
window.setLocation(new Point(x, y));
}
use of java.awt.Rectangle in project jna by java-native-access.
the class RasterRangesUtils method outputOccupiedRangesOfBinaryPixels.
/**
* Output the non-null values of a binary image as ranges of contiguous values.
* @param binaryBits byte-packed binary bits of an image
* @param w width of the image (in pixels)
* @param h height of the image
* @param out
* @return true if the output succeeded, false otherwise
*/
public static boolean outputOccupiedRangesOfBinaryPixels(byte[] binaryBits, int w, int h, RangesOutput out) {
Set<Rectangle> rects = new HashSet<Rectangle>();
Set<Rectangle> prevLine = Collections.<Rectangle>emptySet();
int scanlineBytes = binaryBits.length / h;
for (int row = 0; row < h; row++) {
Set<Rectangle> curLine = new TreeSet<Rectangle>(COMPARATOR);
int rowOffsetBytes = row * scanlineBytes;
int startCol = -1;
// Look at each batch of 8 columns in this row
for (int byteCol = 0; byteCol < scanlineBytes; byteCol++) {
int firstByteCol = byteCol << 3;
byte byteColBits = binaryBits[rowOffsetBytes + byteCol];
if (byteColBits == 0) {
// all 8 bits are zeroes
if (startCol >= 0) {
// end of current region
curLine.add(new Rectangle(startCol, row, firstByteCol - startCol, 1));
startCol = -1;
}
} else if (byteColBits == 0xff) {
// all 8 bits are ones
if (startCol < 0) {
// start of new region
startCol = firstByteCol;
}
} else {
// mixed case : some bits are ones, others are zeroes
for (int subCol = 0; subCol < 8; subCol++) {
int col = firstByteCol | subCol;
if ((byteColBits & subColMasks[subCol]) != 0) {
if (startCol < 0) {
// start of new region
startCol = col;
}
} else {
if (startCol >= 0) {
// end of current region
curLine.add(new Rectangle(startCol, row, col - startCol, 1));
startCol = -1;
}
}
}
}
}
if (startCol >= 0) {
// end of last region
curLine.add(new Rectangle(startCol, row, w - startCol, 1));
}
Set<Rectangle> unmerged = mergeRects(prevLine, curLine);
rects.addAll(unmerged);
prevLine = curLine;
}
// Add anything left over
rects.addAll(prevLine);
for (Iterator<Rectangle> i = rects.iterator(); i.hasNext(); ) {
Rectangle r = i.next();
if (!out.outputRange(r.x, r.y, r.width, r.height)) {
return false;
}
}
return true;
}
use of java.awt.Rectangle in project jna by java-native-access.
the class RasterRangesUtils method outputOccupiedRanges.
/**
* Outputs ranges of occupied pixels.
* In a raster that has an alpha layer, a pixel is occupied if its alpha value is not null.
* In a raster without alpha layer, a pixel is occupied if it is not completely black.
* @param raster image to be segmented in non black or non-transparent ranges
* @param out destination of the non null ranges
* @return true if the output succeeded, false otherwise
*/
public static boolean outputOccupiedRanges(Raster raster, RangesOutput out) {
Rectangle bounds = raster.getBounds();
SampleModel sampleModel = raster.getSampleModel();
boolean hasAlpha = sampleModel.getNumBands() == 4;
// Try to use the underlying data array directly for a few common raster formats
if (raster.getParent() == null && bounds.x == 0 && bounds.y == 0) {
// No support for subraster (as obtained with Image.getSubimage(...))
DataBuffer data = raster.getDataBuffer();
if (data.getNumBanks() == 1) {
if (sampleModel instanceof MultiPixelPackedSampleModel) {
MultiPixelPackedSampleModel packedSampleModel = (MultiPixelPackedSampleModel) sampleModel;
if (packedSampleModel.getPixelBitStride() == 1) {
// TYPE_BYTE_BINARY
return outputOccupiedRangesOfBinaryPixels(((DataBufferByte) data).getData(), bounds.width, bounds.height, out);
}
} else if (sampleModel instanceof SinglePixelPackedSampleModel) {
if (sampleModel.getDataType() == DataBuffer.TYPE_INT) {
// TYPE_INT_ARGB, TYPE_INT_ARGB_PRE, TYPE_INT_BGR or TYPE_INT_RGB
return outputOccupiedRanges(((DataBufferInt) data).getData(), bounds.width, bounds.height, hasAlpha ? 0xff000000 : 0xffffff, out);
}
// TODO could easily handle cases of TYPE_USHORT_GRAY and TYPE_BYTE_GRAY.
}
}
}
// Fallback behaviour : copy pixels of raster
int[] pixels = raster.getPixels(0, 0, bounds.width, bounds.height, (int[]) null);
return outputOccupiedRanges(pixels, bounds.width, bounds.height, hasAlpha ? 0xff000000 : 0xffffff, out);
}
use of java.awt.Rectangle in project screenbird by adamhub.
the class CaptureBox method load.
/**
* In order to hold the current state of the capturebox, we implement a
* load/save feature using Java Properties. Upon calling load(), the
* saved state is loaded into the current running state.
*
* @return True if everything saved properly
*/
public boolean load() {
try {
Properties properties = this.propertiesManager.readPropertyFile();
String position = properties.getProperty(POSITION);
String isVisible = properties.getProperty(IS_VISIBLE);
String arState = properties.getProperty(ASPECT_RATIO_STATE, CaptureBoxState.WIDESCREEN.name());
// If data is not found, return default
if (position == null || isVisible == null) {
return false;
}
// Load Capturebox Screen Aspect Ratio
if (arState.equalsIgnoreCase(CaptureBoxState.WIDESCREEN.name())) {
this.aspectRatioState = CaptureBoxState.WIDESCREEN;
} else if (arState.equalsIgnoreCase(CaptureBoxState.STANDARD_SCREEN.name())) {
this.aspectRatioState = CaptureBoxState.STANDARD_SCREEN;
}
log("Capturebox Aspect Ratio " + this.aspectRatioState.name());
// Update loaded Capturebox Screen Aspect Ratio data
if (this.dragBox != null) {
this.dragBox.setAspectRatio(this.aspectRatioState);
} else {
throw new UnsupportedOperationException("Can not set dragbox aspect because it is null");
}
log("Loading data capturebox: " + position);
log("Loading data isVisible: " + isVisible);
String[] rectangle = properties.getProperty(POSITION, DEFAULT_RECTANGLE).split(" ");
// Build and return data
this.setPosition(new Rectangle(Integer.parseInt(rectangle[0]), Integer.parseInt(rectangle[1]), Integer.parseInt(rectangle[2]), Integer.parseInt(rectangle[3])));
if (Boolean.valueOf(properties.getProperty(IS_VISIBLE, "false"))) {
this.setCaptureboxVisible(true, false, true);
} else {
this.setCaptureboxVisible(false, false, null);
}
if (properties.getProperty(RECORDING_VIEW_STATE, recordingViewState.name()).equalsIgnoreCase(CaptureBoxState.FULLSCREEN.name())) {
this.recordingViewState = CaptureBoxState.FULLSCREEN;
} else if (properties.getProperty(RECORDING_VIEW_STATE).equalsIgnoreCase(CaptureBoxState.CUSTOM_SCREEN.name())) {
this.recordingViewState = CaptureBoxState.CUSTOM_SCREEN;
this.setDragBoxVisible(true);
}
return true;
} catch (IOException e) {
log(e);
} catch (NumberFormatException e) {
log(e);
} finally {
this.propertiesManager.closeProperties();
}
return false;
}
use of java.awt.Rectangle in project screenbird by adamhub.
the class CaptureBox method initComponents.
/**
* Sets the default screen size and capture rectangle to the fullscreen
* dimensions of the current screen.
*/
private void initComponents() {
this.screenSize = ScreenUtil.getScreenDimension(recorder.getScreen());
this.fullCaptureRect = new Rectangle(this.screenSize);
}
Aggregations