Custom Cursor Icon is loaded with an extra shaded pixel - java

I changed the cursor Icon whenever a certain toggle button is clicked. But the loaded image contains an extra pixel in it at the bottom corner! It's annoying, like there's constant dirt on the screen. I created the cursor icon using junior icon editor. When I open the picture using windows photo viewer or photoshop, the pixel doesn't manifest itself. It only shows when I use it in the application.
The application is a Java application, and this is how I set the cursor.
Image img = getResourceMap().getImageIcon( iconFilename ).getImage();
Cursor newCursor = Toolkit.getDefaultToolkit().createCustomCursor( img,
new Point( 5, 5 ), "cursor" );
Does anyone know what could be the possible reasons for this extra shaded pixel? It occurs right below the east-facing arrow, about 2 milimeters below it.
You can see the effect running this code. The image appears as expected in a label, but as a pointer there is a darkened pixel on the lower left.
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.imageio.ImageIO;
import java.net.URL;
class ShowImage {
public static void main(String[] args) throws Exception {
URL url = new URL("http://i.stack.imgur.com/kP1jv.png");
final BufferedImage img = ImageIO.read(url);
System.out.println(
"Image is: " + img.getWidth() + "x" + img.getHeight());
SwingUtilities.invokeLater( new Runnable() {
public void run() {
JLabel l = new JLabel(new ImageIcon(img));
l.setBorder(new EmptyBorder(5,5,5,5));
l.setOpaque(true);
l.setBackground(Color.GREEN.darker());
Cursor newCursor = Toolkit.getDefaultToolkit().
createCustomCursor( img,new Point( 5, 5 ), "c" );
l.setCursor(newCursor);
JOptionPane.showMessageDialog(null, l);
}
});
}
}

At first i thought that we were specifying a bigger image than the system cursor size could handle but than i realized that if we remove the last column(!!) of pixels it worked fine!!
I don't know how to explain this!
BufferedImage originalImg = ImageIO.read(new File("kP1jv.png"));
System.out.println("originalImage is: " + originalImg.getWidth() + "x"
+ originalImg.getHeight());
Dimension d = Toolkit.getDefaultToolkit().getBestCursorSize(
originalImg.getWidth(), originalImg.getHeight());
final BufferedImage img = originalImg.getSubimage(0, 0, d.width/*-1*/, d.height-1);

Related

How to remove text from Barcode image

I have written a code which can generate a bar-code with respect to String codeText = "1104006"; and also can read data from that bar-code. But the problem is, while generating a bar-code below the bar-code it also write the text (codeText). How can I remove the human readable text, circled red in example?
public class Main {
private static String strBarFolder = ("C:\\Users\\Jobayer__\\Desktop\\");
public static void main(String[] args) {
String codeText = "1104006";
String strImageFile = ("barcode.jpg");
BarCodeBuilder builder = new BarCodeBuilder(Symbology.CODE39STANDARD, codeText);
builder.save(strBarFolder + strImageFile);
System.out.println("Successfully Done");
Image img = Toolkit.getDefaultToolkit().getImage(strBarFolder + strImageFile);
BarCodeReader reader = new BarCodeReader(img, BarCodeReadType.Code39Standard);
while(reader.read()){
System.out.println("Code Text Found: " + reader.getCodeText());
}
reader.close();
}
}
Get into the Aspose source code (if it's allowable) and comment out the drawString() method which is drawing the text to the image OR modify the bar code image by drawing a white rectangle with fillRect() over the text area(s) of the image.
Below is a small runnable console application I quickly whipped up that performs the latter. It is based off of the Bar Code image you provided:
package barcodetextcoverup;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
public class BarCodeTextCoverUp {
public static void main(String[] args) {
startTextCover();
}
private static void startTextCover() {
Scanner scnr = new Scanner(System.in);
String userInput = "";
while (!userInput.equalsIgnoreCase("quit")) {
System.out.println("\nEnter the path and file name to the Bar Code image file\n"
+ "to modify or enter quit to exit:");
userInput = scnr.nextLine();
if (userInput.equalsIgnoreCase("quit")) { break; }
// opening a bar code image from disk
BufferedImage src = null;
try {
src = ImageIO.read(new File(userInput));
int iWidth = src.getWidth();
int iHeight = src.getHeight();
// Modify the image...
Graphics2D g2 = src.createGraphics();
g2.setColor(Color.WHITE);
//Cover the text: Aspose.Demo
g2.fillRect(0, 0, 150, 30);
// Cover the codeText at bottom of Bar Code
g2.fillRect((iWidth/2) - 75, iHeight - 40, 150, 35);
g2.dispose();
System.out.println("\nEnter a NEW path and file name for the modified Bar Code image.\n"
+ "You can use the same file name just change the extention to .png:");
userInput = scnr.nextLine();
// If nothing is supplied then the modifications are not saved.
if (!userInput.equals("")) {
ImageIO.write((RenderedImage) src, "PNG", new File(userInput));
}
System.out.println("----------------------------------------------------------------------");
} catch (IOException ex) { }
}
}
}
Set this property:
builder.CodeLocation = CodeLocation.None;
Setting CodeLocation to None will hide the barcode text thus creating
the effect of barcode without any value in it.
from Aspose support forum: https://forum.aspose.com/t/creating-2d-bar-code-using-aspose-and-merging-it-in-pdf/7265
if you want to change caption text use this property...
builder.Display2DText = "this is caption text";
By default if it's set to empty string it displays codetext.
Read the properties of bar-code methods in the below link
http://barbecue.sourceforge.net/apidocs/net/sourceforge/barbecue/Barcode.html
Using the setDrawingText(Boolean) default method we remove the text in below bar-code
Example :
Barcode barcode = BarcodeFactory.createCode128(id);
barcode.setDrawingText(false);
BarcodeImageHandler.writePNG(barcode, new FileOutputStream(new File(file Name)));

The whole file icon is not visible in javafx tableview

I have a JavaFX tableview in which I'm listing files and their properties, including the ICON of the file. My problem is that a part of the icon is missing.
I don't know how to describe it completely so I included an image below.
My tableview is to the right and windows explorer is to the left.
Here is the code:
#Override
public void updateItem(ImageIcon imageIcon, boolean empty){
if (imageIcon != null){
// I have tried adding minimum width and height too
HBox box= new HBox();
// JavaFX scene imageview
ImageView imageView = new ImageView();
// Have tried with and without setFitHeight
imageView.setFitHeight(16);
imageView.setFitWidth(16);
// This line prints: 16 16
System.out.println(imageIcon.getIconWidth() + " " + imageIcon.getIconHeight());
// Create BufferedImage of the imageicon
BufferedImage bi = new BufferedImage(
imageIcon.getIconWidth(),
imageIcon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
imageIcon.paintIcon(null, g, 0, 0);
g.dispose();
// BufferedImage to fxImage
Image fxImage = SwingFXUtils.toFXImage(bi, null);
imageView.setImage(fxImage);
box.getChildren().addAll(imageView);
setGraphic(box);
}
}
Thanks.
UPDATE
I have google around some more and i copied an example directly and pasted into a new project, and the same thing happened with the icons. In the example, the user was probably using windows 7 by judging the look of the icons, so maybe this is just the way it looks in windows 8. It was the code in the answer at the bottom with a picture: www.stackoverflow.com/questions/28034432/

Getting different values from getRGB and setRGB after XORing two images

I want to XOR two images pixel by pixel. I am using the following code.
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.Scanner;
import java.security.*;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
public class sefi
{
public static void main(String[] args) throws Exception
{
encdec ed = new encdec();
String plainimagename = args[0];
String keyfilename = args[1];
String choice = args[2];
BufferedImage bikey = ImageIO.read(new File(keyfilename));
BufferedImage biplain = ImageIO.read(new File(plainimagename));
BufferedImage resizedImage = new BufferedImage(biplain.getWidth(), biplain.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(bikey, 0, 0, biplain.getWidth(), biplain.getHeight(), null);
g.dispose();
ImageIO.write(resizedImage, "jpg", new File("resizeimage.jpg"));
if(choice.equals("enc"))
{
ed.encrypt(resizedImage,biplain);
}
else if(choice.equals("dec"))
{
ed.decrypt(resizedImage,biplain);
}
}
}
class encdec
{
public void encrypt(BufferedImage bikey, BufferedImage biplain) throws Exception
{
BufferedImage xoredimage = xor(bikey, biplain);
File xored = new File("xored.jpg");
ImageIO.write(xoredimage, "JPEG", xored);
}
public void decrypt(BufferedImage bikey, BufferedImage biplain) throws Exception
{
BufferedImage xoredimage = xor(bikey, biplain);
File xored = new File("newplain.jpg");
ImageIO.write(xoredimage, "JPEG", xored);
}
private BufferedImage xor(BufferedImage image1, BufferedImage image2) throws Exception
{
BufferedImage outputimage = new BufferedImage(image1.getWidth(), image1.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < image1.getHeight(); y++)
{
for (int x = 0; x < image1.getWidth(); x++)
{
outputimage.setRGB(x,y,((image1.getRGB(x, y))^(image2.getRGB(x, y))));
System.out.println("one:" + image1.getRGB(x, y) + "\ttwo:" + image2.getRGB(x, y) + "\txor:" + ((image1.getRGB(x, y))^(image2.getRGB(x, y))));
System.out.println("one:" + Integer.toBinaryString(image1.getRGB(x, y)) + "\ttwo:" + Integer.toBinaryString(image2.getRGB(x, y)) + "\txor:" + Integer.toBinaryString((image1.getRGB(x, y)^image2.getRGB(x, y))));
}
}
return outputimage;
}
}
First time I run this code where image1 is a 4-pixel colored image and image2 is a 4-pixel white image, I get the output as:
input: #java sefi white.jpg key.jpg enc
one:-201053 two:-1 xor:201052
one:11111111111111001110111010100011 two:11111111111111111111111111111111 xor:110001000101011100
one:-265579 two:-1 xor:265578
one:11111111111110111111001010010101 two:11111111111111111111111111111111 xor:1000000110101101010
one:-664247 two:-1 xor:664246
one:11111111111101011101110101001001 two:11111111111111111111111111111111 xor:10100010001010110110
one:-925624 two:-1 xor:925623
one:11111111111100011110000001001000 two:11111111111111111111111111111111 xor:11100001111110110111
Next time I run with image1 as the xored image file and image 2 as the 4-pixel colored file, which should give me the original white image as output. But I get this as output instead:
Input:#java sefi xored.jpg key.jpg dec
one:-1 two:-16773753 xor:16773752
one:11111111111111111111111111111111 two:11111111000000000000110110000111 xor:111111111111001001111000
one:-1 two:-16773753 xor:16773752
one:11111111111111111111111111111111 two:11111111000000000000110110000111 xor:111111111111001001111000
one:-1 two:-15786601 xor:15786600
one:11111111111111111111111111111111 two:11111111000011110001110110010111 xor:111100001110001001101000
one:-1 two:-15786601 xor:15786600
one:11111111111111111111111111111111 two:11111111000011110001110110010111 xor:111100001110001001101000
If you look at the output we can see that the colors of the xored image from first time has changed.
I am not able to understand why I am getting different color value for the same image file.
There's something wrong with your image selection. If you were selecting the RGB for the generated image then the first pixel would be 110001000101011100 and not 11111111000000000000110110000111.
So my advice is for you to check if you are using the correct images on the second step.
Your code looks ok, although you'd have to paste the whole code for me to have a better idea.
Found the Answer.
It is because I am using JPEG images. JPEG compresses raw image, but when it decompress it does not guarantee to produce the exact same colors as it was before compression. Thus the different color values before and after.
When I used bmp images, I got the same colors before and after.

Replacing image pixel in Java2D

I am trying to replace some pixels from my source image(PNG format). But i am end up with some confusing result. Basically i am replacing a particular RGB values with black and white colors. Here is my code,
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ChangePixel
{
public static void main(String args[]) throws IOException
{
File file = new File(System.getProperty("user.dir"), "D4635.png");
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis);
int[] replaceColors = new int[2];
replaceColors[0] = Color.BLACK.getRGB();
replaceColors[1] = Color.WHITE.getRGB();
Color src = new Color(133, 101, 51);
int srcRGBvalue = src.getRGB();
changeAlg2(image, srcRGBvalue, replaceColors);
}
private static void changeAlg2(BufferedImage image, int srcRGBvalue, int[] replaceColors) throws IOException
{
for (int width = 0; width < image.getWidth(); width++)
{
for (int height = 0; height < image.getHeight(); height++)
{
if (image.getRGB(width, height) == srcRGBvalue)
{
image.setRGB(width, height, ((width + height) % 2 == 0 ? replaceColors[0] : replaceColors[1]));
}
}
}
File file = new File(System.getProperty("user.dir"), "107.png");
ImageIO.write(image, "png", file);
}
}
It changes my source pixels to black and some other color, instead of white. Please advice me, what's going wrong here.
Since this is my first post, I can't able to attach my images. Sorry for the inconvenience.
Edit: I have uploaded the source and the output images in a site. Here is the URL,
Source : http://s20.postimage.org/d7zdt7kwt/D4635.png
Output : http://s20.postimage.org/kdr4vntzx/107.png
Expected output : After the black pixel, white pixel has to come.
Edit : Resolved code as per Jan Dvorak advice,
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ChangePixel
{
public static void main(String args[]) throws IOException
{
File file = new File(System.getProperty("user.dir"), "D12014.gif");
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis);
Color src = new Color(223, 170, 66);
int srcRGBvalue = src.getRGB();
int[] replaceColors = new int[2];
replaceColors[0] = Color.MAGENTA.getRGB();
replaceColors[1] = Color.CYAN.getRGB();
changeAlg2(image, srcRGBvalue, replaceColors);
}
private static void changeAlg2(BufferedImage image, int srcRGBvalue, int[] replaceColors) throws IOException
{
BufferedImage image2 = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int width = 0; width < image.getWidth(); width++)
{
for (int height = 0; height < image.getHeight(); height++)
{
if (image.getRGB(width, height) == srcRGBvalue)
{
image2.setRGB(width, height, ((width + height) % 2 == 0 ? replaceColors[0] : replaceColors[1]));
}
else
{
image2.setRGB(width, height, image.getRGB(width, height));
}
}
}
File file = new File(System.getProperty("user.dir"), "110.gif");
ImageIO.write(image2, "gif", file);
}
}
Regards
Raja.
Since you are adding colors that are not present in the original image palette, the pixels you are trying to set are clipped to the nearest color in the palette. You need to set a new color mode. Either convert to 24bpp RGB (true-color) or extend the palette with the new colors.
It doesn't seem to be possible to modify an existing BufferedImage ColorModel or assign a new one, but you can create a new buffer and copy the data there. Creating a new BufferedImage with the same Raster might work as well (only if the bit depth does not change?).
If you don't mind, you can always create a True-color image. try:
{
BufferedImage old = image;
image = new BufferedImage(
old.getWidth(),
old.getHeight(),
BufferedImage.TYPE_INT_RGB
);
image.setData(old.getRaster());
} // old is no longer needed
API reference: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html
You could try to detect if the image is already in true-color (image.getColorModel() instanceof ???) to avoid having to copy the buffer when not needed.
You could try to extend the existing palette. If that is not possible (there is no palette to start with or there is not enough space), you have to fallback to RGB.
See:
http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html (getColorModel and the constructor taking a ColorModel and type)
http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/IndexColorModel.html (getMapSize, getRGBs and the corresponding constructor)
From seeing your actual palette, you'll need some sort of deduplication logic because your palette is already 256 bytes - the maximum size of a PNG palette. Note that you should not save the image with larger palette than there are colors in the image (especially when you want to add new colors later). your original file could have been saved with a 2-color palette, saving 762 bytes.
Note that you don't gain much from storing the image as indexed as opposed to true-color with the same number of colors. The reason is that the byte stream (palette = 1 byte per pixel, true-color = 3 or 4 bytes per pixel) is losslessly compressed (with DEFLATE) anyways. Indexed can save you a few bytes (or lose you a few bytes, if the palette is big), but it won't reduce the file size to one third.

Java Full Screenshot

I wanted to display on the JFrame on the program the full screenshot of my screen.
So far using the code below, I was able only to display part of the screen.
The code below is the content of the paint(Graphics g).
How can I make it full screen?
// the screen resolution is 1280 x 1024 while the JPanel size is only 1024 x 768
Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(resolution);
robot = new Robot();
BufferedImage bufferedImage = robot.createScreenCapture(rectangle);
g.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT), 0, 0, null);
Maybe using something like this:
//get the screen size
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
BufferedImage image = robot.createScreenCapture (dim);
//other code
//...
I see you have some errors, I don't know if your code even compiles, 'cause references seems not to be declared, but a code similar to this one will caputure a screenshoot of your desktop:
import java.awt.geom.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class ScreenCapturer
{
public static void main(String[] args)throws Exception
{
Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(resolution);
Robot robot = new Robot();
BufferedImage bufferedImage = robot.createScreenCapture(rectangle);
Graphics g = bufferedImage.getGraphics();
//g.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT), 0, 0, null);
File out = new File("image.png");
ImageIO.write(bufferedImage,"png",out);
}
}
I saved to an png image file instead of drawing it on the screen or the frame.
Use java.awt.Toolkit.getDefaultToolkit().getScreenSize() to get the size of the screen: http://www.roseindia.net/java/java-get-example/screen-dimensions.shtml
g.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT), 0, 0, null);
Simplify your code by only using one statement per line then you might be able to understand the code.
Why are you ue the width and height of the image? How does that scale the image if you specify the full size of the image? I would guess you want:
Image scaled = bufferedImage.getScaledInstance(1024, 768, Image.SCALE_DEFAULT);
Now instead of doing custom painting you can just add your image to a JLabel:
ImageIcon icon = new ImageIcon( scaled );
JLabel label = new JLabel( icon );
frame.add( label );

Categories