Solved

Need to resize pics which are retrieved from a BTRIEVE DB

Posted on 2010-09-07
7
338 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33616097
0
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:gurvinder372
ID: 33616307
which link did you tried?
0
 
LVL 16

Accepted Solution

by:
Valeri earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Use email signature images to promote corporate certifications and industry awards.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The goal of this is to tech the user how to export photos out of Lightroom and the different options they have. Select which photos you want to export : Select where you want to export and with which options : Export :

867 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now