Solved

Need to resize pics which are retrieved from a BTRIEVE DB

Posted on 2010-09-07
7
352 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 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article provides the solution to a question (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28674207.html) posed here at Experts Exchange. The asker of the question has many JPG images in many folders, and all of t…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The goal of the tutorial is to teach the user how to use import presets downloaded from the internet into Adobe Lightroom. Once you downloaded the presets go into the preset folder and press import then import your preset and your set it to go.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

726 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