Solved

Need to resize pics which are retrieved from a BTRIEVE DB

Posted on 2010-09-07
7
343 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create image with transparent background for selected content in MS Excel or Word 5 76
Java Options in Zimbra Server 2 52
null output 3 25
Search an image for an image 3 30
PaperPort 14.5 Patch 1 update is often not detected or downloaded automatically. This article provides direct download links to solve the problem for retail (non-bundled) versions of the Standard and Professional editions, as well as the Professiona…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to remove a natural vignette your photos and how to add vignette to your photos.
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…

770 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