I have an image and want to scale it down using imgscalr [1].
The source image's size is 1836 x 3264 (so it's portrait mode) and destination resolution is 1336 x 768.
The failure is that the image is in landscape mode and not portrait mode anymore after scaling. The scaling itself works like a charm.
My code:
BufferedImage src = ImageIO.read(new File("sourceimage.jpg"));
BufferedImage scaled = Scalr.resize(src, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_HEIGHT, 1336, 768, Scalr.OP_ANTIALIAS);
ImageIO.write(scaled, "jpg", f);
The metadata of the file looks correct (orientation = 1).
My expactation is when downscaling an portrait picture that it is still a portrait picture after the scaling process.
I also tried some other libraries/classes like this: https://frickelblog.wordpress.com/2009/06/08/fast-image-scaling-in-java/ but the effect is the same.
Can you please help me? This one drives me crazy! I'm pretty sure there is just a detail to change.
[1] http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/#maven
edit:
Here's the original image: http://i.stack.imgur.com/NEc8j.jpg
the scaled version: http://i.stack.imgur.com/SYx6p.jpg
So the problem isn't with ImgScar but with ImageIO, ImageIO won't read the orientation of the image, so it was been read in as 3264x1836 - Landscape.
So what I ended up doing was rotating the image 90 degrees...
BufferedImage src = ImageIO.read(new File("/Users/swhitehead/Downloads/original.jpg"));
System.out.println(src.getWidth() + "x" + src.getHeight());
BufferedImage rotated = Scalr.rotate(src, Scalr.Rotation.CW_90, Scalr.OP_ANTIALIAS);
BufferedImage scaled = Scalr.resize(rotated, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_HEIGHT, 1336, 768, Scalr.OP_ANTIALIAS);
System.out.println(scaled.getWidth() + "x" + scaled.getHeight());
ImageIO.write(scaled, "jpg", new File("Scaled.jpg"));
Which seems to have fixed "this" problem.
You can have a look at ImageIO.read( ) always rotates my uploaded picture for more details
Browsers implement <img> differently.
Firefox will not read the EXIF-information so portrait pictures are displayed in landscape.
Chrome does read out the EXIF-information so portait stays portrait.
I fixed it with this CSS declaration:
image-orientation: from-image;
Related
**I need high quality image with arcore. Currently I can extract the image but the armodel does not show.
I have tried to get the image from draw frame
**
First get some supported camera configs and select one fitting you. For example highest width*height
val bestConfig = session.getSupportedCameraConfigs(CameraConfigFilter(session)).maxByOrNull {
it.imageSize.width * it.imageSize.height
}
Then reconfigure ARCore session to use this camera configuration.
session.cameraConfig = bestConfig
Docs here: https://developers.google.com/ar/reference/java/com/google/ar/core/Session#setCameraConfig-cameraConfig
I'm developing a game and use batcher.drawSprite method to draw all images in the game (background and all characters)
in assets.java :
charAtlas = new Texture(game, "charAtlas.png");
charEnemy = new TextureRegion(charAtlas, 0,0,250,300);
in worldGame.java :
batcher.beginBatch(Assets.charAtlas); // set atlas
batcher.drawSprite(130, 628, 120,140, Assets.charEnemy);
//assets.charEnemy
is it right to use this method in all condition ?
I have 3 atlas in game , i even use 2048x2048 atlas size so i can include all my images in there..
However, the image looks blurry in game (Tested in galaxy note, tab, and galaxy young). looks at the code above, i even have the enemy char take size in my atlas as much as 250x300 , it's not make sense that it'll look blurry as i only draw it in 120x140.
note : i use no layout (i mean no layout file in res folder) .. i use drawsprite to draw all image (Character,menu, button, etc)..
update :
I tried to use character image files from other game that i unzipped, when i run the app, it also looks blurry and jagged. while in the original game, it's so smooth and sharp. why is that ?
check your code, you might use scaledbitmap , see if you can set like this.
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);
I want to resize my images (original size is 1080p) but they don't resize properly and I don't know why. The Images just don't have the right size sometimes. On my emulator and my old 800*480 smartphone it works fine but on my nexus 4 with 1280*768 things don't look right. There is no problem reading the right screen resolution. There is just a bug with my resize procedure. Please help me. Heres a Snippet:
private Bitmap bitmap,bitmap1;
private float factor;
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displaymetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displaymetrics);
width = displaymetrics.widthPixels;
height = displaymetrics.heightPixels;
factor = (float)height/1080;
Int bitmapheight,bitmapwidth;
bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.picture),(int)(factor*BitmapFactory.decodeResource(getResources(), R.drawable.picture).getwidth() ,(int)(factor*BitmapFactory.decodeResource(getResources(), R.drawable.picture).getHeight(), true);
canvas.drawBitmap(bitmap,.....
In the end the height is not resized to 768/1080*bitmapheight on my nexus and i don't know why. Note everything else works. Mathmatics indicates that the height should be the same on every phone.
These are screenshots of my programm showing the images have not the same height
First image:
Second:
As you can see the Images are not equal in terms of height. On my emulator and my old smartphone they look right. The Images should not touch the bottom but on my nexus 4 they do touch the bottom.
For anyone who is interessted why it didnt work. Finally i found out.
Now
BitmapFactory.decodeResource(getResources(), R.drawable.picture)
not only loads the pictures but also scales it depending on your resolution ( not always just on some resolution) thats why it confused me. I thought it just loads the damn picture. Now how to solve this:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
BitmapFactory.decodeResource(getResources(), R.drawable.picture,opts)
any body please help me to understand how to use method convertTo in JavaCV. i was trying and searching a lot of time and not find the proper answer. so my problem is, when i try to convert type of mat from gray image to CV_32F and try to display it in android screen it will give me no result. this my source :
Imgproc.cvtColor(src, dst, Imgproc.COLOR_RGBA2GRAY, 1); // dst is gray image
dst.convertTo(dst_f, CvType.CV_32F, 1./255, 0); //convert dst to CV_32F with optional scale
Imgproc.cvtColor(dst_f, src, Imgproc.COLOR_GRAY2RGBA, 4); // to display it in android before convert to bitmap
I was trying also in opencv C++ with this code and it run smoothly :
imshow("dst", image);
cvtColor(image, dst, CV_BGR2GRAY);
dst.convertTo(dst_f, CV_32F, 1.0/255, 0);
imshow("dst_f", dst_f);
UPDATED :
I think i wrong when i try to display image online in android screen. i don't know how to display it altough i knew that android need 4 channel to display image in screen. but when i convert gray to CV_32F image type and i try to display with same code like above and it run smoothly without no result.
After some research and Experiment, i just got my golden answer, here it is :
Android can only display 4 channels image in the screen so you need to convert it to 4 channels (RGBA), but before you convert to RGBA/BGRA you have to change Mat image to CV_8U with same scaled. just look at this example :
Imgproc.cvtColor(src, dst, Imgproc.COLOR_RGBA2GRAY, 1);
dst.convertTo(dst_f, CvType.CV_32F, 1./255, 0);
dst_f.convertTo(final, CvType.CV_8U, 255, 0);
Imgproc.cvtColor(final, src, Imgproc.COLOR_GRAY2RGBA, 4);
With above code, your code will work perfectly in Android screen, but it will give you a broken picture if you try to save it to jpg image or other picture extension. if you want to save it to sdcard you can use this source :
Imgproc.cvtColor(src, dst, Imgproc.COLOR_RGB2GRAY, 1);
dst.convertTo(dst_f, CvType.CV_32F, 1.0/255, 0); // Convert Mat image to CV_32F and scale it to 1.0/255
dst_f.convertTo(final, CvType.CV_32F, 255, 0); //Convert it Back to Default type before
Imgproc.cvtColor(final, roi, Imgproc.COLOR_GRAY2RGB, 0);
Imgproc.cvtColor(roi, roi, Imgproc.COLOR_RGB2RGBA, 0);
above all is my own experiment, if you wanna ask me something, i will answer as good as i can. Thanks. Hope this help others a lot
I am using GifDecoder to read an animated .gif file and AnimGifEncoder to write it. (link)
If I display the original frames read by GifDecoder they display correctly and are transparent, but if I display the frames created by AnimatedGifEncoder the transparency is all wrong.
GifDecoder gif = new GifDecoder();
gif.read("image.gif");
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.start("newimage.gif");
e.setTransparent(Color.BLACK);
for (int i=0;i<gif.getFrameCount();i++) {
anim.addFrame(gif.getFrame(i));
anim.setDelay(gif.getDelay(i));
}
anim.finish();
In this example I set the transparent color to black. But actually I want to get the transparent color information from the GifDecoder but I don't know how.
I'm using the following solution, although there are still some gif files that
are a mess after scaling... :
(At least it seems to get along with most non-transparent gifs)
Color transparentColor = null;
BufferedImage firstFrameImage = ImageIO.read([sourceFileHere]);
if (firstFrameImage.getColorModel() instanceof IndexColorModel) {
IndexColorModel cm = (IndexColorModel) firstFrameImage.getColorModel();
int transparentPixel = cm.getTransparentPixel();
transparentColor = new Color(cm.getRGB(transparentPixel), true);
}
e.setTransparent(transparentColor);
This was all a long time ago, but I did manage to get it working at the time. Without digging out the code again... I got it working by:
Scan the original image and set the transparent areas to a colour that does not exist inside the original image.
Set the transparent colour inside the AnimGifEncoder to the colour that was previously assigned to the transparent areas.