2017-10-05 3 views
2

私はSDカード上のファイルにビットマップを保存するには、この機能を使用します。Androidでビットマップから保存するときにdpiイメージを設定するにはどうすればよいですか?

private static File storeImage(Context context, Bitmap image) { 
    File pictureFile = getOutputMediaFile(context); 
    if (pictureFile == null) { 
     return null; 
    } 
    try { 
     FileOutputStream fos = new FileOutputStream(pictureFile); 
     image.compress(Bitmap.CompressFormat.PNG, 100, fos); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return pictureFile; 
} 

private static File getOutputMediaFile(Context context){ 
    // To be safe, you should check that the SDCard is mounted 
    // using Environment.getExternalStorageState() before doing this. 
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory() 
      + "/Android/data/" 
      + context.getPackageName() 
      + "/Files"); 

    // This location works best if you want the created images to be shared 
    // between applications and persist after your app has been uninstalled. 

    // Create the storage directory if it does not exist 
    if (! mediaStorageDir.exists()){ 
     if (! mediaStorageDir.mkdirs()){ 
      return null; 
     } 
    } 
    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date()); 
    File mediaFile; 
    String mImageName="IMG_"+ timeStamp +".png"; 
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName); 
    return mediaFile; 
} 

私はそれを設定することができますどのように私は、ファイルを開いて、画像DPI情報が表示されたら、それは72ピクセル/インチを示し like this

300ピクセル/インチ、または何か他の値?

+0

PNGメタデータを変更する方法もあります。それは複雑で、Googleで検索し、バイトを混乱させる。 –

+0

関連:https://stackoverflow.com/questions/27323561/do-pngs-or-jpgs-have-a-dpi-or-is-it-irrelevant-when-building-for-retina –

答えて

0

ここでは、スクリーンショットの管理や画像のサイズ変更に使用するヘルパーメソッドをいくつか紹介します。私はあなたがスクリーンショットにしようとしているとは思わないが、あなたはまだリサイズメソッドを使用することができます

   Bitmap screenshot = ImageHelper.takeScreenshotOfView(this, Bitmap.CompressFormat.JPEG); 
      Bitmap croppedImage = ImageHelper.cropImage(screenshot, ImageHelper.mStartXCrop, ImageHelper.mStartYCrop, ImageHelper.mCropWidth, ImageHelper.mCropHeight, true); 
      returnImage = ImageHelper.getResizedBitmap(croppedImage, mCropImageWidth, mCropImageHeight, false); 

public static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight, boolean recycleOriginal) { 
    int width = bm.getWidth(); 
    int height = bm.getHeight(); 

    // Determine scale to change size 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    // Create Matrix for maniuplating size 
    Matrix matrix = new Matrix(); 
    // Set the Resize Scale for the Matrix 
    matrix.postScale(scaleWidth, scaleHeight); 

    //Create a new Bitmap from original using matrix and new width/height 
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 

    //Remove memory leaks if told to recycle, warning, if using original else where do not recycle it here 
    if(recycleOriginal) { 
     bm.recycle(); 

    } 

    //Return the scaled new bitmap 
    return resizedBitmap; 

} 
public static Bitmap cropImage(Bitmap imgToCrop, int startX, int startY, int width, int height, boolean recycleOriginal){ 
    Bitmap croppedImage = Bitmap.createBitmap(imgToCrop, startX, startY , width , height); 

    if(recycleOriginal){ 
     imgToCrop.recycle(); 

    } 

    return croppedImage; 
} 
public static Bitmap takeScreenshotOfView(Activity context, Bitmap.CompressFormat compressFormat){ 
    Bitmap screenshot = null; 

    try { 
     // create bitmap screen capture 
     View v1 = context.getWindow().getDecorView().getRootView(); 
     v1.setDrawingCacheEnabled(true); 
     screenshot = Bitmap.createBitmap(v1.getDrawingCache()); 
     v1.setDrawingCacheEnabled(false); 

     File imageFile = new File(context.getFilesDir() + File.separator + "A35_temp" + File.separator + "screenshot_temp"); 

     FileOutputStream outputStream = new FileOutputStream(imageFile); 
     int quality = 100; 

     screenshot.compress(compressFormat, quality, outputStream); 
     outputStream.flush(); 
     outputStream.close(); 

    } catch (Throwable e) { 
     // Several error may come out with file handling or OOM 
     e.printStackTrace(); 
    } 

    return screenshot; 
} 

これらはすべて私が同じように使用して私のImageHelperクラスの一部です。

0

これらは、保存中iはビットマップDPIを設定するために使用される方法であるhereまたhere参照します。

public void storeImage(Bitmap image) { 
     try { 
      File pictureFile = new File("yourpath"); 

      FileOutputStream fos = new FileOutputStream(pictureFile); 


      ByteArrayOutputStream imageByteArray = new ByteArrayOutputStream(); 
      image.compress(Bitmap.CompressFormat.JPEG, 100, imageByteArray); 
      byte[] imageData = imageByteArray.toByteArray(); 

      //300 will be the dpi of the bitmap 
      setDpi(imageData, 300); 

      fos.write(imageData); 
      fos.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 


    public void setDpi(byte[] imageData, int dpi) { 
     imageData[13] = 1; 
     imageData[14] = (byte) (dpi >> 8); 
     imageData[15] = (byte) (dpi & 0xff); 
     imageData[16] = (byte) (dpi >> 8); 
     imageData[17] = (byte) (dpi & 0xff); 
    } 
+0

CompressFormat .JPEG。私の場合はうまくいかなかった。 私の画像にアルファチャンネルがあり、 "image.compress(Bitmap.CompressFormat.PNG、100、fos);" – Zidd

関連する問題