I've read many articles and tutorials but still have not found a solution. I also read in some posts that are on stackoverflow but still have not found a solution. everything I ask for help to solve my problem. Thank you earlier. sorry for my English is garbled
try {
int baris = tabelpelanggan.getSelectedRow();
stt = K.PrepareStatement();
rs = stt.executeQuery("select image from pelanggan where idpelanggan='"
+ tabelpelanggan.getValueAt(baris, 0).toString() + "'");
byte[] imagee = null;
while (rs.next()) {
Blob blob = rs.getBlob("image");
imagee = blob.getBytes(1, (int) blob.length());
}
Image i = Toolkit.getDefaultToolkit().createImage(imagee);
ImageIcon ii = new ImageIcon(i);
Image iii = ii.getImage();
Image newicon = iii.getScaledInstance(labelImage.getWidth(), labelImage.getHeight(), Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon(newicon);
labelImage.setIcon(icon);
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
Related
I am experiencing an EOF Exception as follows when attempting to read tiff files using iText 5.5.10
ExceptionConverter: java.io.EOFException
at com.itextpdf.text.pdf.RandomAccessFileOrArray.readFully(RandomAccessFileOrArray.java:249)
at com.itextpdf.text.pdf.RandomAccessFileOrArray.readFully(RandomAccessFileOrArray.java:241)
at com.itextpdf.text.pdf.codec.TiffImage.getTiffImage(TiffImage.java:209)
at com.itextpdf.text.pdf.codec.TiffImage.getTiffImage(TiffImage.java:314)
at com.itextpdf.text.pdf.codec.TiffImage.getTiffImage(TiffImage.java:302)
at com.itextpdf.text.Image.getInstance(Image.java:428)
at com.itextpdf.text.Image.getInstance(Image.java:374)
at TiffToPdf.main(TiffToPdf.java:137)
The code I am using is:
byte[] data = null;
Image img = null;
try {
data = Files.readAllBytes(Paths.get("tiff.tif"));
img = Image.getInstance(data, true);
}
catch (Exception e) {
e.printStackTrace();
}
I have tried skipping the Image step and using the TiffImage class explicitly but I experience the same error.
byte[] data = null;
Image img = null;
try {
data = Files.readAllBytes(Paths.get("tiff.tif"));
RandomAccessSourceFactory factory = new RandomAccessSourceFactory();
RandomAccessSource fileBytes = factory.createSource(data);
RandomAccessFileOrArray s = new RandomAccessFileOrArray(fileBytes);
img = TiffImage.getTiffImage(s, true, 1, true);
}
catch (Exception e) {
e.printStackTrace();
}
I noticed that there are 2 classes within iText called TIFFFaxDecompressor and TIFFFaxDecoder but I haven't been able to find any resources online on how to use them.
with your given tiff image, the following code does worked for me i.e., converted to pdf successfully.
byte[] data = null;
com.itextpdf.text.Image img = null;
try {
//System.out.println(Paths.get("src/main/resources/tiff.tif"));
data = Files.readAllBytes(Paths.get("src/main/resources/file.tif"));
RandomAccessSourceFactory factory = new RandomAccessSourceFactory();
RandomAccessSource fileBytes = factory.createSource(data);
RandomAccessFileOrArray s = new RandomAccessFileOrArray(fileBytes);
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("src/main/resources/destination.pdf"));
document.open();
int pages = TiffImage.getNumberOfPages(s);
Image image;
for (int i = 1; i <= pages; i++) {
image = TiffImage.getTiffImage(s, i);
Rectangle pageSize = new Rectangle(image.getWidth(),
image.getHeight());
document.setPageSize(pageSize);
document.newPage();
document.add(image);
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
I have a small requirement please help me
Firstly, I have a label, i set a icon to that label as
lbl_photo.setIcon(new javax.swing.ImageIcon(getClass().getResource(
"/images/photo.png")));
and i have a button browse to select the image.
private void btn_browseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"jpeg, gif and png files", "jpg", "gif", "png");
int i = chooser.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
image = chooser.getSelectedFile();
try {
BufferedImage originalImage = ImageIO.read(image);
int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB
: originalImage.getType();
BufferedImage resizeImageJpg = resizeImage(originalImage, type);
photo = new ImageIcon(toImage(resizeImageJpg));
raster = resizeImageJpg.getRaster();
data = (DataBufferByte) raster.getDataBuffer();
} catch (Exception e) {
System.out.println(e.getMessage());
}
lbl_photo.setIcon(photo);
}
}
now, I am storing the selected image from browse button into database
Date date1 = new Date();
Timestamp timestamp1 = new Timestamp(date1.getTime());
String sql4 = "insert into std_photos values(?,?,?)";
pstmt5 = con.prepareStatement(sql4);
pstmt5.setInt(1, Integer.parseInt(txt_eno.getText()));
pstmt5.setString(2, "");
pstmt5.setTimestamp(3, timestamp1);
byte[] extractBytes = data.getData();
pstmt5.setBytes(2, extractBytes);
System.out.println(sql4);
image is successfully storing.but,if the user doesn't select the image through browse button the default jlabel icon should be store in the database.
please help me as early as possible
You are already checking the return value of chooser.showOpenDialog(this) for JFileChooser.APPROVE_OPTION.
In case you receive some other option or if the try-catch block reading the new image fails you could just get the old icon and write it into the database.
I am trying to retrieve blob data type from database table and display the image in a imageView. Here is my select SQL statement:
public boolean retrieve(){
boolean success = false;
ResultSet rs = null;
DBController db = new DBController();
db.getConnection();
String dbQuery = "SELECT * FROM sm_product WHERE productName ='" + name +"'";
rs = db.readRequest(dbQuery);
try {
if(rs.next()){
desc = rs.getString("productDescription");
price = rs.getInt("productPrice");
quantity = rs.getInt("productQuantity");
dateStr = rs.getString("dateOfCreation");
category = rs.getString("productCategory");
Blob blob = rs.getBlob("productImage");
byte[] data = blob.getBytes(0, (int) blob.length());
image = ImageIO.read(new ByteArrayInputStream(data)); //Error here
success = true;
}
}
catch(Exception e){
e.printStackTrace();
}
db.terminate();
return success;
}
After retrieved, I want to display it in a imageview. Here is the code:
panel.getMyImageView().setImage(product.getImage());
However, I got incompatible type error message. I know image = ImageIO.read(new ByteArrayInputStream(data)); this line supposed to store as BufferedImage and then from there I slowly convert to image datatype and display in image view. But after 2 hours of researching, I got no luck. Can somebody please help me with it?
Thanks in advance.
Toolkit.getDefaultToolkit().createImage(data)
Call this for the byte array read from the blob
I have a blob type field in my MySQL, I want to put the data in this field in JLabel as Icon. For example this JLabel will be user's Profile Picture in my form.
I used this codes but nothing happens
and also I want to fix to width or fix any image size in my jlabel
DefaultTableModel pic = MyDB.DataTable("SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'");
if (pic.getRowCount() > 0){
Blob blob = pic.getBlob(1);
byte[] image1 = blob.getBytes(1, ALLBITS);
ImageIcon image = new ImageIcon(image1);
picture.setIcon(image);
getContentPane().add(picture);
setVisible(true);
}
picture is the name of my jlabel
First : Return the Input stream from your database :
String query = "SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'";
stmt = (PreparedStatement) con.prepareStatement(query);
ResultSet result = stmt.executeQuery();
Returned image from Database
BufferedImage im = ImageIO.read(result.getBinaryStream(1));
Then make rezise to this image :
im =linearResizeBi(im, /*width*/, /*height*/);
linearResizeBi Method :
static public BufferedImage linearResizeBi(BufferedImage origin, int width, int height) {
BufferedImage resizedImage = new BufferedImage(width, height ,BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
float xScale = (float)width / origin.getWidth();
float yScale = (float)height / origin.getHeight();
AffineTransform at = AffineTransform.getScaleInstance(xScale,yScale);
g.drawRenderedImage(origin,at);
g.dispose();
return resizedImage;
}
then make the image is an Icon:
ImageIcon image1 = new ImageIcon(im);
then add the Icon to The Jlabel :
picture.setIcon(image);
getContentPane().add(picture);
setVisible(true);
Use a resultset
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'");
You may change from
Blob blob = rs.getBlob(1);
to another altenative of
InputStream binaryStream = rs.getBinaryStream(1);
You can refer to the official guide of getting image from a blog here
http://docs.oracle.com/javase/1.5.0/docs/guide/jdbc/blob.html
I got
my filename should be like this
txtPicPath.setText(file.getAbsoluteFile().toString());
and i used these codes, and also it fits with the jlabel size
ResultSet rst = MyDB.rsFetch("SELECT `Picture` FROM `photo` WHERE `Employee ID` = '"+ Data.User.getText()+"'");
while (rst.next()) {
Blob filenameBlob = rst.getBlob("Picture");
byte[] content = filenameBlob.getBytes(1L,(int)filenameBlob.length());
ImageIcon ik = new ImageIcon(content);
Image img = ik.getImage();
Image newimg = img.getScaledInstance(Data.picture.getWidth(), Data.picture.getHeight(), java.awt.Image.SCALE_SMOOTH);
ik = new ImageIcon(newimg);
Data.picture.setIcon(ik);
}
Blob has a getBinaryStream() which returns a stream of bytes containing the data stored in the blob.
ImageIcon, which implements Icon, has a constructor which takes a byte array as argument.
JLabel has a setIcon(Icon) method.
label.setIcon(new ImageIcon(ByteStreams.toByteArray(blob.getBinaryStream())));
Try:
picture.setIcon(new ImageIcon(ByteStreams.toByteArray(blob.getBinaryStream())));
I looked at these links, none helped me. Im making a game with a JFrame using KeyListener and Runnalbe. I've never had a problem with it in games i have made in the past, but now heres what im talking about:
i start my program and the window comes up, message displays everything looks fine.
no keys work
-i close the window
-The 2nd time its opened everything works fine.
-If i reset the virtual machine and try again, back to square 1.
here is my constructor:
public Game()
{
super("ZMan");
addKeyListener(this);
setSize(800,600);
setVisible(true);
i = createImage(800,600);
b = i.getGraphics();
makeLevel();
souls = new Image[3];
particles = new double[20][3];
pc = 10;
pH = 20;
player = new Image[8];
nme = new Image[8];
showMessage = false;
try
{
bg = ImageIO.read(new File("bg.png"));
abg = ImageIO.read(new File("abg.png"));
bil = ImageIO.read(new File("buildingP.png"));
bilP = ImageIO.read(new File("building.png"));
message = ImageIO.read(new File("./system/messageBG.png"));
souls[2] = ImageIO.read(new File("sR.png"));
souls[1] = ImageIO.read(new File("sG.png"));
souls[0] = ImageIO.read(new File("sB.png"));
rem = souls[0];
//player
player[0] = ImageIO.read(new File("./player/normal.png"));
player[1] = ImageIO.read(new File("./player/pBloop.png"));
player[2] = ImageIO.read(new File("./player/pscorp.png"));
player[3] = ImageIO.read(new File("./player/pSqui.png"));
player[4] = ImageIO.read(new File("./player/pCato.png"));
player[5] = ImageIO.read(new File("./player/pChubs.png"));
player[6] = ImageIO.read(new File("./player/pPluckers.png"));
player[7] = ImageIO.read(new File("./player/pSpooky.png"));
gate = ImageIO.read(new File("./gate.png"));
levelUp = ImageIO.read(new File("./system/levelUp.png"));
xL = ImageIO.read(new File("./player/x.png"));
/*
nme[0] = ImageIO.read(new File("./enemies/bloop.png"));
nme[1] = ImageIO.read(new File("./enemies/bloop.png"));
nme[2] = ImageIO.read(new File("./enemies/scorp.png"));
nme[3] = ImageIO.read(new File("./enemies/squi.png"));
nme[4] = ImageIO.read(new File("./enemies/cato.png"));
*/
nme[0] = Toolkit.getDefaultToolkit().createImage("./enemies/bloob.png");
nme[1] = Toolkit.getDefaultToolkit().createImage("./enemies/bloob.png");
nme[2] = Toolkit.getDefaultToolkit().createImage("./enemies/scorp.png");
nme[3] = Toolkit.getDefaultToolkit().createImage("./enemies/squi.png");
nme[4] = Toolkit.getDefaultToolkit().createImage("./enemies/cato.png");
nme[5] = Toolkit.getDefaultToolkit().createImage("./enemies/chubs.png");
nme[6] = Toolkit.getDefaultToolkit().createImage("./enemies/pluckers.png");
nme[7] = Toolkit.getDefaultToolkit().createImage("./enemies/spooky.png");
}
catch(Exception e){e.printStackTrace();}
powerT = new String[8];
powerT[0] = "You went back to normal";
powerT[1] = "You are now Bloop!";
powerT[2] = "You are now Scorp!";
powerT[3] = "You are now Squi!";
powerT[4] = "You are now Cato!";
powerT[5] = "You are now Chubs!";
powerT[6] = "You are now Pluckers";
powerT[7] = "You are now Spooky";
startAim();
//music = new Music("./system/music/0.wav");
t.start();
}
does nyone know the problem?
"To fire keyboard events, a component must have the keyboard focus."—How to Write a Key Listener. I'm guessing the relevant component has focus the second time. You can try requestFocusInWindow() or, preferably, look at How to Use Key Bindings.
Addendum: There's a key binding example here.
This may sound dumb, but it's more of a sanity check. Check your main method. I'd recommend doing a debug stepthrough, making sure that the first time the window is launched, the constructor to Game is actually called...Nothing about the constructor appears to be bad (to me), which makes me think that the instantiation of the object might be hosed.
If it isn't that, I can't help you; you'll have to wait for someone smarter in the specifics of your problem to answer :)