1

我有一个相机应用程序,我在其中拍摄图像并将其存储在 SD 卡中。

当我检查存储图像的分辨率时,发现它是 120X160 像素。

但是,相机拍摄的正常图像是 640X480 像素。

我不希望分辨率降低。你能解释一下为什么会这样吗?

@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();
            }
        }
}       

我必须创建自己的相机活动吗?我是初学者..所以请帮忙

4

1 回答 1

0
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;
    }
}
于 2014-10-08T01:50:56.370 回答