I tried everything I found to save just my image without borders without success.
My image is 124x126, so when the image is saved it's always with borders and new size 300x300, If I use another program to select only my image it's 208x209.
so I don't know why my image is scaled and why it's always 300x300, If I change the size of vtkRenderWindow to 124x126 the borders are still there and my image is shrunk.
My question is how I can save my image of 124x126 without borders?
mapper.SetInputConnection(polyDataReader.GetOutputPort());
vtkActor actor = new vtkActor();
actor.SetMapper(mapper);
actor.GetBounds(bounds);
System.out.println("actor: " + bounds[1] + " x " + bounds[3]); // 124x126 (everything's ok)
//Create a renderer, render window, and interactor
vtkRenderer renderer = new vtkRenderer();
vtkRenderWindow renderWindow = new vtkRenderWindow();
renderWindow.BordersOff(); // the borders are still there
renderWindow.AddRenderer(renderer);
vtkRenderWindowInteractor renderWindowInteractor = new vtkRenderWindowInteractor();
renderWindowInteractor.SetRenderWindow(renderWindow);
//Add the actor to the scene
renderer.AddActor(actor);
renderer.SetBackground(0, 0, 0);
//Render and interact
renderWindow.Render();
//screenshot code:
vtkWindowToImageFilter w2if = new vtkWindowToImageFilter();
w2if.SetInput(renderWindow);
w2if.Update();
vtkPNGWriter writer = new vtkPNGWriter();
writer.SetFileName("out.png"); //my image with borders 300x300 !!!!??
writer.SetInputData(w2if.GetOutput());
writer.Write();
ps: sorry for my English
It is because your renderwindow has the default size of 300x300.
What you have to realize is that the polydata is rendered in a 3D scene, and what you call "borders" is the background, the amount of which depends on the zoom level, i.e. on your camera's position. To get what you want, you need to do two things:
Resize renderwindow to desired image size:
renderWindow.SetSize(bounds[1], bounds[3]);
Reposition camera so that the actor fills the viewport completely. Try
renderer.GetActiveCamera().SetParallelScale(1); // we want parallel projection
renderer.GetActiveCamera().SetParallelScale(0.5 * (bounds[3] - bounds[2])); // zoom
The bounds in zoom may be 0.5 * (bounds[1] - bounds[0]), not exactly sure which side of the rectangle it should be.
Hope this helps,
Miro
Related
In my app I am trying to create watermarks on the image when either user captures it from camera or uploads from gallery. but the problem is that when I draw text on the canvas(Which is my image itself). I get different results every time.
I want to show 3 texts vertically in center, 3 texts vertically at left and 1 logo at right. I want the watermarks in such way that. if user captures the image from camera or browse through gallery. The texts and logo(watermarks) should be remain of same size and clearly visible, irrespective of the image size or pixel density.
Can anyone show me how to achieve this. I tried Bitmap and Canvas but I am getting different text sizes and blurred text and everything different on different images.
In the below images, here is my problem.
1.Image which was clicked from camera but I used it from browse images
2.Image which was selected form Capture Image button
3.random low quality image from gallery
Here is the code
Glide.with(getApplicationContext())
.load(uri)
.asBitmap()
.into(new SimpleTarget<Bitmap>(200, 100) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
//this is the required bitmap
pikedPhoto = resource.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(pikedPhoto);
Paint paintCenter = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);
paintCenter.setStyle(Paint.Style.FILL);
paintCenter.setColor(getResources().getColor(android.R.color.black)); // Text Color
paintCenter.setColor(Color.RED);
//float size = convertDpToPx(context, 25);
//paintCenter.setTextSize(size); //Text Size
paintCenter.setTextAlign(Paint.Align.CENTER);
float textY= pikedPhoto.getHeight();
float textX= pikedPhoto.getWidth()/2;
String text1 = "Centre Text";
canvas.drawText(text1,textX,textY,paintCenter);
canvas.drawText(text1,textX,textY-10,paintCenter);
canvas.drawText(text1,textX,textY-20,paintCenter);
String text2 = "Left Text";
Paint paintLeft = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);
paintLeft.setStyle(Paint.Style.FILL);
paintLeft.setColor(getResources().getColor(android.R.color.black)); // Text Color
paintLeft.setColor(Color.RED);
//float sizeLeft = convertDpToPx(context, 25);
//paintLeft.setTextSize(sizeLeft); //Text Size
paintLeft.setTextAlign(Paint.Align.LEFT);
float textYL= pikedPhoto.getHeight();
canvas.drawText(text2,0,textYL,paintLeft);
canvas.drawText(text2,0,textYL-10,paintLeft);
canvas.drawText(text2,0,textYL-20,paintLeft);
// in this case, center.x and center.y represent the coordinates of the center of the rectangle in which the text is being placed
//canvas.drawBitmap(logo, (pikedPhoto.getWidth()) / 4, 250, new Paint());
img.setImageBitmap(pikedPhoto);
}
});
I am using libgdx for my game and I have a viewport of 360x630, the problem is that I am using FreetypeFont for my fonts and the size is set to 24, I don't know how many pixels is 24. So what I did is
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
font.draw(batch, wc.string, width / 2 - string.length / 2, height / 2 - string.length / 2);
but the string length is always changed because the string is always changed too.
bellow A work around I always use to center any text with changing size, I don't know if it's a good practice to add useless sciene2D table for the purpose, anyway here is the solution :
Create a table :
private Table table;
In your show/create methode ( depend on your libgdx version ... ) add the following :
table = new Table(skin); // if you have a skin or you leave it empty
table.setFillParent(true);
YourText = new Label("YourText", skin, "YourFont");
table.add(YourText).colspan(3).expandX().spaceBottom(50).row(); // you change 50 depend on your screen resolution till you get to the center
This will put your text in a cell/row and center it H/V depend on the screen of course.
In the create/show method dont forget to add the table to the stage :
stage.addActor(table);
Now in your render method your add the following:
stage.act(delta);
stage.draw();
Finally you have to dispose the stage because the GC don't do it for you ( a libgdx way to do GC stuff ), in your dispose method you add this :
stage.dispose();
I'm using LibGDX and Scene2d and I want to create a pop-up that looks like the one shown in the image below.
So far, I created a table and set the background to a semitransparent color. Then I created an image with a white background. This image expands x, so it is centered. Now I want to have a label centered inside the image. To overlay these two actors, I created a group, but I couldn't find out how to center something in a group. How can I do this?
Here is my code:
Table table = new Table();
table.top();
table.setFillParent(true);
table.setBackground(getColoredBackground(color));
labelStyle = new Label.LabelStyle(font, fontColor);
label = new Label(message, labelStyle);
Image image = new Image(whiteBackground);
image.setSize(table.getWidth() - padding, label.getHeight() + extraHeight);
Group group = new Group();
group.setSize(image.getWidth(), image.getHeight());
group.addActor(image);
group.addActor(label);
table.add(group).expandX().padTop(padTop);
The group has it own coordinates system. It means that when you are setting actor position to (0,0) for example and then adding it to group which is actually at (100, 100) position then the object will be at (100, 100) position relative to the stage.
All you need to do is just to set:
label.setPosition( group.getWidth() / 2f - label.getWidth() / 2f, group.getHeight() / 2f - label.getHeight() / 2f );
after setting Group size. Remember that setting actor's position we are defining its left bottom corner position and that's why you need to also subtract the half of label width/height.
Of course you can modify the y position it doesn't have to be centered vertically also
So I have an assignment where I need to create a catalog.
The catalog needs to have a list, an image and a description.
My entire code works, so I have no issue with the coding as such.
I do have an issue with the image size.
How do I take care of images on a java gui program to make them all into one size when it is running.
Please let me know :D
When you read in an image, create a new BufferedImage that is the exact size that you desire, get it's Graphics object via getGraphics(), draw the original image into the new image using Graphics#drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) where x and y are 0 and width and height are from the dimensions of the new image, dispose() of the Graphics object, and then display the new Image as an ImageIcon in a JLabel. Make sure though that the original image is the same size or larger than the new one, else your images will look gawd-awful.
For example, and note that this code may not be exactly correct since I don't have my IDE up:
BufferedImage originalImage = ImageIO.read(something); // read in original image
// create new empty image of desired size
BufferedImage newImage = new BufferedImage(desiredWidth, desiredHeight, BufferedImage.TYPE_INT_ARGB);
Graphics g = newImage.getGraphics(); // get its graphics object
// draw old image into new image
g.drawImage(originalImage, 0, 0, desiredWidth, desiredHeight, null);
g.dispose(); // get rid of Graphics object
// create ImageIcon and put in JLabel to display
Icon newIcon = new ImageIcon(newImage);
myJLabel.setIcon(newIcon);
I would propably create a JPanel to draw on one Image, and then work with the method:
myPanel.setSize(new Dimension(x,y))
or
myPanel.setPreferredSize(new Dimension....)
There is a method (image = imgobj.getScaledInstance(width, height, hints)) in awt.Image class which provides re-sizing capabilities very nicely, I always use this to re-size my images when I need. Please see here some examples :-), I hope it will work for you, it is the most convenient way to scale images I have ever seen. create a method pass the image to the method and size of the image you want and return the image back in return to reuse the code ;)
I have an Android app that loads an image as a bitmap and displays it in an ImageView. The problem is that the image appears to have a transparent background; this causes some of the black text on the image to disappear against the black background.
If I set the ImageView background to white, that sort of works, but I get ugly big borders on the image where it is stretched to fit the parent (the actual image is scaled in the middle).
So - I want to convert the transparent pixels in the Bitmap to a solid colour - but I cannot figure out how to do it!
Any help would be appreciate!
Thanks
Chris
If you are including the image as a resource, it is easiest to just edit the image yourself in a program like gimp. You can add your background there, and be sure of what it is going to look like and don't have use to processing power modifying the image each time it is loaded.
If you do not have control over the image yourself, you can modify it by doing something like, assuming your Bitmap is called image.
Bitmap imageWithBG = Bitmap.createBitmap(image.getWidth(), image.getHeight(),image.getConfig()); // Create another image the same size
imageWithBG.eraseColor(Color.WHITE); // set its background to white, or whatever color you want
Canvas canvas = new Canvas(imageWithBG); // create a canvas to draw on the new image
canvas.drawBitmap(image, 0f, 0f, null); // draw old image on the background
image.recycle(); // clear out old image
You can loop through each pixel and check if it is transparent.
Something like this. (Untested)
Bitmap b = ...;
for(int x = 0; x<b.getWidth(); x++){
for(int y = 0; y<b.getHeight(); y++){
if(b.getPixel(x, y) == Color.TRANSPARENT){
b.setPixel(x, y, Color.WHITE);
}
}
}