I tried importing image via JFileChooser into JPanel. It worked. but I need to fit the image inside the panel without losing its aspect ratio or its proportion.
I tried rescaling the image, here's my code:
Image img = Toolkit.getDefaultToolkit().createImage(picture.getSource());
Image scaledImage = img.getScaledInstance(jPanel1.getWidth(),jPanel1.getHeight(),Image.SCALE_SMOOTH);
g2.drawImage(scaledImage, 0, 0,null,null);
But unable to protect its ratio. I need a simple code for this.
Try reading the instructions:
If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions. If both width and height are negative, then the original image dimensions are used.
Related
I am using Pax A920 device.
I have an issue when printing a bitmap, there is no option to center the bitmap(image) documentation
Methods reference:
The issue is when the bitmap's width is short, its left-aligned on the printed receipt.
I can scale the bitmap using:
Bitmap.createScaledBitmap(receiptLogo, 512, receiptLogo.getHeight(), false);
But it looks bad, I'm thinking of creating a new Bitmap with width of 512 & height of receipt.Logo.getHeight() then put the receipt logo in the middle so it wont look stretch.
Is this doable?
I'm creating a game in libGDX. I want to create some UI elements (buttons and stuff), because of my app design, I would like to draw them in the world space, as other game objects.
I'm using Freetype generator that generates a bitmap font from true type font files(.ttf). The problem is that the dimension of the font is in pixels.
Orthographic camera that I use to to render the world, has viewport size of approximately 10x10, so when I generate a font at the size of 10, it covers almost whole screen(too big) and also looks very ugly because generated bitmap for the font is too small (too few pixels).
What I want is to create sprite, draw it at same size(world space) and draw text over it, and basicly create a button.
Is there some well established way how to deal with this?
Thanks to clarifying comments, I've came up with the solution.
I took a point at which I wanted to draw the text, projected it to the screen space by my world camera. Then I flipped y axis by:
point.y = viewportHeight - point.y;
Then I unprojected it with ScreenViewport (separate viewport for drawing the text, is uses camera of the size of the screen so 1unit == 1pixel).
Now I can draw text in projection where 1unit = 1pixel, on the point that is at the same place on the screen as previously chosen point in world space.
I also wanted to be able to draw text inside rectangular boundaries. For this I chose another point. At this point text should end. Did the same procedure as with start point, and then calculated width
targetWidth = endpoint.x - startpoint.x;
Then I used GlypthLayout class to get actual width of my text at some(generated) font size.
actualWidth = glyphLayout.width;
And when I scaled font like this
font.getData().setScale(targetWidth / actualWidth);
my font get scaled so drawed text is wide as target width.
But be aware of another problem! When I generate bimap font via FreetypeGenerator with size bigger when approximately 300, some letters don't draw, and are missing. (probably bug).
I am loading an external image with JavaFX:
Image tile = new Image(imageFile.toURI().toURL().toString(),width, height, true,true);
You need to supply the width and height. How can I get the image's width and height?
Now, I know that the width and height of a PNG image are determined by the bytes from 12 to 20 or something like that, so technically you could open a byte stream and interpret the bytes. However, this seems overkill for the simple task of drawing an image in my JavaFX application. Is there not a way to draw an image with its full size without asking me to supply the dimensions?
Just create the Image using a constructor which does not include dimensions (e.g. new Image(url)):
Image image = new Image(url);
I am currently working with a DICOM project in java. I am calculating the dicom values such as depth, off-axis ratio, width and height. I want to create a sample image with the calculated width and height. I should be a gray scale image, with varying densities of gray color in appropriate area. I have created a sample image with imagemagick, using concentric circles. Bit am not getting the exact image. It does not look like an actual dicom image. The variation of gray color does not have a linear behavior. Sample image is attached. Please suggest any other method to create dicom image. The density values are available in a list. depending upon the distance from the center, the gray color also changes according to the density value provided.
Pixel Data or image in DICOM data set is always rectangular and image is described using Rows (0028, 0010) and Columns (0028, 0011) attributes. So the best way to implement this is to draw your circular image on a rectangular background. Fill the background with a color that is not present in your image. Use the Pixel Padding Value (0028,0120) to specify the color used to pad grayscale images (those with a Photometric Interpretation of MONOCHROME1 or MONOCHROME2) to rectangular format.
If you do not have any color to spare (with-in the bit stored), you can add Overlay Plane or Display Shutter to the data set to mask the area that is not part of image.
I need to crop images for a "Photoprint" app of mine. To create appropriate image for printing paper, image will be scaled up, scaled down and cropped by dragging to specific field of image in a frame. Only short edge or long edge of the image could have empty space, two edges shouldn't have empty spaces at the same time. Any of height or width should fit the crop frame. By the way, crop frame should stay the same size. For example; for a 5x7" printing paper, frame will have 504x306px size.
Could you help me, image crop algorithm?