2012-06-02 91 views
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(); 
      } 
     } 
}  

威爾我要創建我自己的相機活動?我是初學者..所以請幫助

+1

你的問題的解決方案,可以發現[這裏](http://stackoverflow.com/questions/2729267/android-camera-intent) – Renard

+0

u能PLZ指定鏈接? – Kumar

+0

「數據」額外不是爲了將捕獲的圖像傳送到您的應用程序;它只是一個縮略圖,可以用作圖標或網格視圖。除非您明確要求不同的位置(不是普遍支持的功能),否則全尺寸照片將與相機應用拍攝的所有常規膠片卷一起存儲。 –

回答

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; 
    } 
} 
+1

解釋你的代碼可能是一個好主意,這樣更多的用戶可以理解正在發生的事情。 – jazzurro