Image Resolution

250 views Asked by At

I have a camera application in which i am taking an image and storing it in the SD Card .

When i check the resolution of the image stored , it is found as 120X160 pixels.

But , the normal image taken by the camera is 640X480 pixels.

I donot want the resolution to get decreased . Can u plz explain why is this happening??

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            Bundle extras = data.getExtras();



            Bitmap bmp1 = (Bitmap) extras.get("data");



            //now storing the image in sdcard, folder name is same as the model name

            ByteArrayOutputStream bytes1 = new ByteArrayOutputStream();
            scaled1.compress(Bitmap.CompressFormat.PNG, 100, bytes1);

            File f1 = new File(folder,"bmp1.png");

            try {
                f1.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            FileOutputStream fo1 = null;

            try {
                fo1 = new FileOutputStream(f1);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                fo1.write(bytes1.toByteArray());
                fo1.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
}       

Wil i have to create my own camera Activity?? I am a beginner .. so please help

1

There are 1 answers

1
user2400695 On
package com.net.test;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class TestImagesDpi {


    public static void main(String[] args) throws ImageFormatException, IOException {
        FileOutputStream fos = null;  

//      byte[] rawData = getRawBytesFromFile(""); // some code to read raw bytes from image file
//      ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(rawData));
        BufferedImage images = ImageIO.read(new File("C:\\Users\\Rajesh\\Desktop\\richagangopadhyay217.jpg"));
//      images.se

        int type = images.getType() == 0? BufferedImage.TYPE_INT_ARGB : images.getType();

        images = resizeImageWithHint(images, type);

        //        for (int i = 0; i < imageFiles.length; i++) {  
            fos = new FileOutputStream(new File("C:\\Users\\Rajesh\\Desktop\\test.jpg"));  
            JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(fos);  
            JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(images);  
            jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);  
            jpegEncodeParam.setXDensity(600);
            jpegEncodeParam.setYDensity(600);
            jpegEncodeParam.setQuality(1.0f, true);  
            jpegEncoder.encode(images, jpegEncodeParam);  
            fos.close();  
//        }  
    }

    private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type){

        BufferedImage resizedImage = new BufferedImage(256, 256, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, 256, 256, null);
        g.dispose();    
        g.setComposite(AlphaComposite.Src);

        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING,
        RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

        return resizedImage;
        }   

    private static byte[] getRawBytesFromFile(String string) throws FileNotFoundException {
        // TODO Auto-generated method stub

        File file = new File("C:\\Users\\Rajesh\\Desktop\\richagangopadhyay217.jpg");

        FileInputStream fis = new FileInputStream(file);
        //create FileInputStream which obtains input bytes from a file in a file system
        //FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        try {
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                //Writes to this byte array output stream
                bos.write(buf, 0, readNum); 
                System.out.println("read " + readNum + " bytes,");
            }
        } catch (IOException ex) {
//            Logger.getLogger(ConvertImage.class.getName()).log(Level.SEVERE, null, ex);
        }

        byte[] bytes = bos.toByteArray();
        return bytes;
    }
}