Solved

Need to resize pics which are retrieved from a BTRIEVE DB

Posted on 2010-09-07
7
332 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
Comment Utility
0
 
LVL 16

Expert Comment

by:Valeri
Comment Utility
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
Comment Utility
Well gurvinder372, I resized the image and the image looks broken and cannot be deiphered.....
       
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
which link did you tried?
0
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
Comment Utility
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
Comment Utility
gurvinder372,
                          Sorry for the delay, I tried this link: http://www.javalobby.org/articles/ultimate-image/
0
 

Author Closing Comment

by:CCBRONET
Comment Utility
Great....
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the tutorial is to teach the user how to make his/her own presets while editing so it is easier to edit there photos. Create a preset you like and copy that setting then save it in to your presets folder.

772 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

12 Experts available now in Live!

Get 1:1 Help Now