?
Solved

Need to resize pics which are retrieved from a BTRIEVE DB

Posted on 2010-09-07
7
Medium Priority
?
365 Views
Last Modified: 2012-05-10
Hi,
     Just got done with retrieving images from btrieve db but they are only of 20 X 20 if i try to inc the resolution it does not generate the image.....Plz Help....
public void readFromUNFfile(){
		try{
		       File file = new File("C:/sg01b.txt");
		      //File length
		      int size = (int)file.length(); 
		      if (size > Integer.MAX_VALUE)
		      {
		        System.out.println("File is to larger");
		      }
		      System.out.println("file name: "+file.length());
		      byte[] bytes = new byte[928]; 
		      DataInputStream dis = new DataInputStream(new FileInputStream(file)); 
		      dis.skip(4);
		      int read = 0;
		      int numRead = 0;
		      while (read < bytes.length && (numRead=dis.read(bytes, read,
		                                                bytes.length-read)) >= 0) {
		        read = read + numRead;
		       
		      }
		        System.out.println("File size: " + read);
		      // Ensure all the bytes have been read in
		    if (read < bytes.length) 
		    {
		        System.out.println("Could not completely read: "+file.getName());
		    }
		    
		  int w = 20, h = 20;
		    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
		    System.out.println("1");
	        int[] nBits = {8};
	        ColorModel cm = new ComponentColorModel(cs, nBits, false, true,
	                                                Transparency.OPAQUE,
	                                                DataBuffer.TYPE_BYTE);
	        System.out.println("2");
	        SampleModel sm = cm.createCompatibleSampleModel(w, h);
	        System.out.println("3");
	        DataBufferByte db = new DataBufferByte(bytes, w*h);
	        System.out.println("4");
	        WritableRaster raster = Raster.createWritableRaster(sm, db, null);
	        System.out.println("5");
	        BufferedImage bm = new BufferedImage(cm, raster, false, null);
	        System.out.println("6");
	        System.out.println("bm="+bm);
	        Graphics2D g2 = bm.createGraphics();
	        System.out.println("7");
	        g2.drawImage(image, null, null);
	        System.out.println("8");
	        File imageFile = new File("C:/newrose2.jpg");
	        ImageIO.write(bm,"JPG", imageFile);
	        System.out.println(imageFile.getPath());

		      Connection connection = null;
				 String connectionURL = "jdbc:mysql://172.16.10.26:3306/cbs";
				  PreparedStatement psmnt = null;
				  Class.forName("com.mysql.jdbc.Driver").newInstance();
				  connection = (Connection) DriverManager.getConnection(connectionURL, "admin", "admin123");
				  psmnt = (PreparedStatement) connection.prepareStatement("Insert into sign (s_branch_code,s_product_id,s_account_no,s_image)Values(?,?,?,?)");
				  
				  psmnt.setInt(1, 400209002);
				  psmnt.setInt(2, 101);
				  psmnt.setInt(3, 15);
				  psmnt.setBytes(4,bytes);
				  System.out.println("!!!!!!!!!@@@@@@@@@@@@@@@@@@@");
				  int s = psmnt.executeUpdate();
				  System.out.println("!!!!!!!!!");
				  if(s>0) {
	                System.out.println("Uploaded successfully !");
				  }
				  else 
				  {
		            System.out.println("unsucessfull to upload image.");
				  }
				  connection.close();
		    }
		    catch (Exception e){
		      e.getMessage();
		    }

	}

Open in new window

0
Comment
Question by:CCBRONET
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Expert Comment

by:Valeri
ID: 33616128
Hope it will help you :
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;

// This applet takes an image and displays it stretched and shrunk.

public class ShrinkStretch extends Applet
{
      Image image;

      public void init()
      {
// Get the image
            image = getImage(getDocumentBase(), "samantha.gif");

// Create a media tracker to wait for the image
            MediaTracker tracker = new MediaTracker(this);
           
// Tell the media tracker to watch the image
            tracker.addImage(image, 0);

// Wait for the image to be loaded
            try {
                  tracker.waitForAll();
            } catch (Exception ignore) {
            }
      }

      public void paint(Graphics g)
      {
// Get the width of the image
            int width = image.getWidth(this);

// Get the height of the image
            int height = image.getHeight(this);

// Draw the image in its normal size
            g.drawImage(image, 10, 10, width, height, this);

// Draw the image at half-size.
            g.drawImage(image, width+20, 10, width / 2,
                  height / 2, this);

// Draw the image at twice its size. Notice that the x coordinate
// for this image is width * 3 / 2 + 30. The 30 represents a 10-pixel
// padding between each image, for 3 images. The 3/2 represents the
// total image size of the previous two images. One full image, plus
// one half the original size.

            g.drawImage(image, width * 3 / 2 + 30, 10,
                  width * 2, height * 2, this);
      }
}

0
 

Author Comment

by:CCBRONET
ID: 33616208
Well gurvinder372, I resized the image and the image looks broken and cannot be deiphered.....
       
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33616307
which link did you tried?
0
 
LVL 16

Accepted Solution

by:
Valeri earned 1500 total points
ID: 33616665
This is part of my code that creates thumbnails, you can rework it in order to stretch the image in the way you want. Just replace the line
g.drawImage(input, 0, 0, width, height, null);
with something like that
g.drawImage(input, 0, 0, width * 2, height * 2, null);

public static void constrain(InputStream is, ImageOutputStream os, int boxSize) {
        try {
            // Read the source file
            BufferedImage input = ImageIO.read(is);
            if (input==null) return;

            // Get the original size of the image
            int srcHeight = input.getHeight();
            int srcWidth = input.getWidth();

            // Constrain the thumbnail to a predefined box size
            int height = boxSize;
            int width = boxSize;
            if (srcHeight > srcWidth) {
                width = (int) (((float) height / (float) srcHeight) * (float) srcWidth);
            } else if (srcWidth > srcHeight) {
                height = (int) (((float) width / (float) srcWidth) * (float) srcHeight);
            }

            // Create a new thumbnail BufferedImage
            BufferedImage thumb = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_565_RGB);
            Graphics g = thumb.getGraphics();
            g.drawImage(input, 0, 0, width, height, null);

            // Get Writer and set compression
            Iterator iter = ImageIO.getImageWritersByFormatName("JPG");
            if (iter.hasNext()) {
                ImageWriter writer = (ImageWriter) iter.next();
                ImageWriteParam iwp = writer.getDefaultWriteParam();
                iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                iwp.setCompressionQuality(0.75f);
                writer.setOutput(os);
                IIOImage image = new IIOImage(thumb, null, null);
                writer.write(null, image, iwp);
                writer.dispose();
            }
            g.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

0
 

Author Comment

by:CCBRONET
ID: 33634256
gurvinder372,
                          Sorry for the delay, I tried this link: http://www.javalobby.org/articles/ultimate-image/
0
 

Author Closing Comment

by:CCBRONET
ID: 33912088
Great....
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a previous article here at Experts Exchange (http://www.experts-exchange.com/articles/18414/Create-a-PDF-file-with-Contact-Sheets-montage-of-thumbnails-for-all-JPG-files-in-a-folder-and-each-of-its-subfolders-using-an-automated-batch-method.html)…
Use email signature images to promote corporate certifications and industry awards.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question