Work with images in Jbox2d (java) - java

I am trying to make the image move rather than a geometric shape in jbox2d.
I dont see the image .
BodyDef bodyDef =new BodyDef();
bodyDef.position.set(100,100);
bodyDef.type=BodyType.DYNAMIC;
//texture=TextureLoader.getTexture("PNG",ResourceLoader.getResourceAsStream("resources/small.png"));
BufferedImage img = null;
try {
img = ImageIO.read(new File("resources/small.png"));
} catch (IOException e) {
}
bodyDef.userData=img;

JBox2D is based on the Box2D engine, which is a physics engine, nothing relative to something visual. You have to draw the image yourself in the main loop of the program, a method often called render().

Related

Java3D - Mapping animated gif to sphere

I have a simple solar system consisting of two planets and a sun.
I can now map images to spheres, but I want to map an animated gif to a sphere.
I assumed this would be the same code, but when I load the animated gif, it simply plays the first frame and doesn't animated through the other frames.
The code that loads the image
Texture tex = null;
File img = new File("src/images/earth.jpg");
try {
tex = new TextureLoader(ImageIO.read(img)).getTexture();
} catch (IOException e) {
e.printStackTrace();
}
tex.setBoundaryModeS(Texture.WRAP);
tex.setBoundaryModeT(Texture.WRAP);
TextureAttributes texAttr = new TextureAttributes();
texAttr.setTextureMode(TextureAttributes.MODULATE);
Appearance ap = new Appearance();
ap.setTexture(tex);
ap.setTextureAttributes(texAttr);
------
Node earth = new Sphere(earthRadius, primflags, 100, ap);
Am I missing something obvious?
Any help or pointers would be fanatics. If I have failed to give enough detail, please ask and I will be more than happy to supply more code.

PDFBox draw black image from BufferedImage

I try to draw an image from a bufferedImage into a PDF using PDFBox but fails, and I get black images and Acrobat Reader warns whith errors like "Out of memory" (but PDF is display).
I use a bufferedImage because I need to draw a JavaFX Image object (with came from call to Funciones.crearImagenDesdeTexto(), is a function which converts a text into an Image) into PDF. Rest of images works well without using bufferedimage.
PDPixelMap img = null;
BufferedImage bi;
try {
//If item has id, I try to get image with that id (image it's shows OK on PDF)
img = new PDPixelMap(documento, read(getClass().getResourceAsStream("/com/img/" + item.getId() + ".png")));
}
catch (Exception e) {
//If item has not id or fails load image, I create image on the fly (which contains item name. This not work on PDF, shows black images)
bi = new BufferedImage(alto, ancho, BufferedImage.TYPE_INT_ARGB);
bi.createGraphics().drawImage(SwingFXUtils.fromFXImage(Funciones.crearImagenDesdeTexto(item.getNombre()), null), ancho, alto, null);
img = new PDPixelMap(documento, bi);
}
finally {
contenedor.drawXObject(img, x, y, alto, ancho);
}
NOTE: crearImagenDesdeTexto() returns a JavaFX Image Object that is create on the fly (I try this function in other parts of the program and works well, function is take from other stackOverflow response).
Your code is confusing, you have three "new PDJpeg" and one of them is in a catch (which should just handle the error). And what does "read()" do? Does it pass a stream or a BufferedImage? If it is a stream, then it is wrong, because PDJpeg is for JPEGs, not for PNG.
The second one
img = new PDJpeg(documento, (getClass().getResourceAsStream("/com/img/" + Byte.toString(item.getId()) + ".png")));
is definitively wrong for the same reason: PDJPeg is not for PNG files / streams.
If you want to create an image from a PNG file / stream, use PDPixelMap.
It is possible to create a PDJpeg object from a BufferedImage, but this is recommended only if the image wasn't encoded before. Because if you would read a BufferedImage from a JPEG, and then use PDJPeg for this, you'll have a slight loss of quality as the image is decoded and encoded again (JPEG is a "lossy" compression format).
If my advice doesn't help, please upload the JPEG file and the PDF somewhere.
Also make sure that you're using the latest version, which is 1.8.7.
Update after comments:
the parameters to createGraphics.drawImage() should be 0, 0 and not width, height. The two parameters are a location, not a size.
Finally, I find a solution (thanks also to Tilman Hausherr):
private void dibujarImagen(Item i, int x, int y, int alto, int ancho) throws IOException {
PDPixelMap img = null;
try {
img = new PDPixelMap(documento, read(getClass().getResourceAsStream("/com/img/" + i.getId() + ".png")));
}
catch (IllegalArgumentException e) {
img = new PDPixelMap(documento, SwingFXUtils.fromFXImage(Funciones.crearImagenDesdeTexto(i.getNombre()),null));
}
finally {
contenedor.drawXObject(img, x, y, alto, ancho);
}
}

Trying to make a program that would detect an specific .png picture in the screen?

I am a java programmer,I am trying to use the screen which are open and take a screen shot and compare it with an other picture and see how many of that object is on my screen. I tried to look for examples in java, but they were all in c++ and i couldn't convert them. I know my problem is in the first line, since CascadeClassifier wants a .xml file not a .png. Can someone help me with this, all I want is a program in java that do the same as this example but using the screen shot, or using screen-capture.
CascadeClassifier balDec = new CascadeClassifier("img/ball.png");
BufferedImage screenShot = r.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
try {
ImageIO.write(screenShot, "png", new File("screenShot"+i+".png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Mat screen =Highgui.imread("screenShot0.png");
MatOfRect balls = new MatOfRect();
balDec.detectMultiScale(screen, balls);

JavaCV Grabber webcam only working on my computer?

So I created an executable JAR using this code and everything works fine on my machine however I tested it on some other computers and the webcam capture never starts. The indicator light doesn't come on. This is the example I see in most tutorials for doing image capture and I'm doing face recognition so it's easiest to utilize the javaCV function rather than adding another library. All suggestions appreciated, thank you.
CanvasFrame canvas = new CanvasFrame("Webcam");
//Set Canvas frame to close on exit
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
try {
//Start grabber to capture video
grabber.start();
//Declare img as IplImage
IplImage img;
long starttime = System.currentTimeMillis();
while (temptime < 4000) {
//inser grabed video fram to IplImage img
img = grabber.grab();
//Set canvas size as per dimentions of video frame.
canvas.setCanvasSize(grabber.getImageWidth(), grabber.getImageHeight());
if (img != null) {
//Flip image horizontally
cvFlip(img, img, 1);
//Draw text over the canvas
Graphics g = canvas.createGraphics();
g.setFont(camfont);
g.setColor(Color.red);
//Show video frame in canvas
canvas.showImage(img);
if (temptime > 2000 && tempcount == 1) {
//take and save the picture
cvSaveImage("User-cap.jpg", img);
tempcount++;
}
temptime = System.currentTimeMillis() - starttime;
}
}
} catch (Exception e) {
}
try {
grabber.stop();
canvas.dispose();
} catch (Exception e) {
System.out.println("Grabber couldn't close.");
}
you need to have OpenCV installed in the machine where you are running that program , jar only will contain javacv wrapper , but doesnt contains dll of opencv

How can I create an image with Java?

I would like to create a gif image of a filled red circle on a green background. What is the easiest way to do it in Java?
If you already have an image file or image URL, then you can use Toolkit to get an Image:
Image img = Toolkit.getDefaultToolkit().createImage(filename);
If you need to construct a new image raster and paint into the image, then you can use BufferedImage. You can paint onto a BufferedImage, by calling its createGraphics function and painting on the graphics object. To save the BufferedImage into a GIF, you can use the ImageIO class to write out the image.
The best way is to generate a BufferedImage:
BufferedImage img = new BufferedImage(int width, int height, int imageType)
// you can find the Types variables in the api
Then, generated the Graphics2D of this image, this object allows you to set a background and to draw shapes:
Graphics2D g = img.createGraphics();
g.setBackground(Color color) ; //Find how to built this object look at the java api
g.draw(Shape s);
g.dispose(); //don't forget it!!!
To built the image:
File file = new File(dir, name);
try{
ImageIO.write(img, "gif", file);
}catch(IOException e){
e.printStackTrace();
}
Create a BufferedImage and then write it to a file with ImageIO.write(image, "gif", fileName).

Categories

Resources