I have a gif image and I would like to be able to detect whether the gif is an animated gif or not using JAVA. This question is about detection rather than displaying the gif.
I see that MIME type of animated gif isn't different of static gif.
How I can do it?
You need an ImageReader. if size is 1 its not animated, everything above is animated.
Check this out:
public Snippet() {
File f = new File("test.gif");
ImageReader is = ImageIO.getImageReadersBySuffix("GIF").next();
ImageInputStream iis;
try {
iis = ImageIO.createImageInputStream(f);
is.setInput(iis);
int images= is.getNumImages(true);
} catch (IOException e) {
e.printStackTrace();
}
}
Related
I am trying to make an image processing program in Android Studio and I have a problem concerning how to save a Bitmap.
My problem is about saving an image which comes from a ByteBuffer.
To show it here, I have done this: I load an image in a ByteBuffer and I try to save it, rescaled to 1014x1163 pixels. And the image I get is distorted, messy.
For example, here is an image I load:
And here is what I get in the image I save:
Here is my code:
imageSize = (int) (bmWidth*bmHeight*4);
ByteBuffer pixelsArray = ByteBuffer.allocateDirect(imageSize);
ByteBuffer outputArray = ByteBuffer.allocateDirect(imageSize);
workingBitmap.copyPixelsToBuffer(pixelsArray);
workingBitmap.copyPixelsToBuffer(outputArray);
int thiswidth = workingBitmap.getWidth();
int thisheight = workingBitmap.getHeight();
Bitmap copiedBitmap = Bitmap.createScaledBitmap(workingBitmap, Width, Height, false);
int thiswidth2 = copiedBitmap.getWidth();
int thisheight2 = copiedBitmap.getHeight();
outputArray.rewind();
copiedBitmap.copyPixelsFromBuffer(outputArray);
SaveJPG(copiedBitmap);
The function SaveJPG works fine because if I call SaveJPG(workingBitmap) it saves a normal image.
Here is the code with variables values during debugging:
I am wondering if the problem comes from the fact that the output resolution is not a multiple of 4. That's mandatory in my program: the output image resolution can be of any value (odd or even).
I have tried many different things (copying workingBitmap and resizing the copy for example).
No success. I don't know what the cause of the problem is.
Does anyone have some source code which can save an image of any resolution, stored in a ByteBuffer ?
Thanks in advance.
Try this
try (FileOutputStream out = new FileOutputStream(yourFileName)) {
yourBitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (IOException e) {
e.printStackTrace();
}
Or use this
MediaStore.Images.Media.insertImage(getContentResolver(), yourbitmap,
"File name", "description of the image");
I have found the cause of the problem. It was simply due to a problem in the width and height of the input image: I was using the width and height of the View which displays the input bitmap, instead of using the bitmap's width and height.
I wonder if there is a way in java to put a gif image over png image at particular location (say at particular value of x,y). If so please help me through this.
This is the case :
I have a base Image which is of png type. and I have gif images of size 62*62. I wanted to put several such gif images on png image and I need to render the png image on front end at every 5 seconds..
To extract image from GIF file.. This save the first image into png file from GIF.
try {
ImageReader reader = ImageIO.getImageReadersByFormatName("gif").next();
ImageInputStream stream = ImageIO.createImageInputStream(new File("c:/aaa.gif");
reader.setInput(stream);
int count = reader.getNumImages(true);
if(count>0){
BufferedImage frame = reader.read(0);
ImageIO.write(frame, "png", new File(filePath+fileName+".png"));
System.out.println("Donesss");
}
} catch (IOException ex) {
}
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);
}
}
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);
I have a layout in an Android activity, I convert it as an image and then share it on different social networks.
The things is that the quality is not good at all, I have artifacts on the image, and the quality depend of the size of the screen.
Is there any way to improve the quality ? I can't store the complete layout image as a static image in the application directory, I need it to be generated on the go.
Here is code I use to transform the layout into a PNG.
Bitmap bitmap = null;
try {
bitmap = Bitmap.createBitmap(dashboardView.getWidth(),
dashboardView.getHeight(), Bitmap.Config.ARGB_4444);
dashboardView.draw(new Canvas(bitmap));
} catch (Exception e) {
// Logger.e(e.toString());
}
FileOutputStream fileOutputStream = null;
File path = Environment
.getExternalStorageDirectory();
File file = new File(path, "wayzupDashboard" + ".png");
try {
fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bitmap.compress(CompressFormat.PNG, 100, bos);
try {
bos.flush();
bos.close();
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Edit: My bad, I left ARGB_4444, the fact is I made a lot of test before posting the question on SO, I just forgot to put ARGB_8888 in. Still the image quality is bad on Android phone with tiny screen. If only I could tell to always use HDPI drawable while capturing the layout.
ARGB_4444 means that you have only 4 bits per channel (a really poor quality).
Use ARGB_8888.
you are using Bitmap.Config.ARGB_4444. I suggest you to use ARGB_8888, like Google tells us
ARGB_4444 This field was deprecated in API level . Because of the
poor quality of this configuration, it is advised to use ARGB_8888
instead.