2016-06-20 16 views
4

Nachdem ein Bild aufgenommen und in Aktivität angezeigt wurde, wird es aufgrund der großen Bildgröße nicht in die Datenbank eingefügt. Ich muss wissen, wie man Bildgröße und Pixel komprimiert und verkleinert.So passen Sie die Bitmap nach dem Capture-Bild in meiner App an

  try { 
       // Bitmap bitmap; 
       BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 

       bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), 
         bitmapOptions); 

       imageView.setImageBitmap(bitmap); 

       String path = android.os.Environment 
         .getExternalStorageDirectory() 
         + File.separator 
         + "Phoenix" + File.separator + "default"; 
       f.delete(); 
       OutputStream outFile = null; 
       File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg"); 
       try { 
        outFile = new FileOutputStream(file); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile); 
        outFile.flush(); 
        outFile.close(); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 

Antwort

2

Antwort in Ihrer Frage selbst,

In Ihrem Code Bitmap komprimieren Bereich 85 ist

bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile); 

Sie müssen es ändern bis 25

bitmap.compress(Bitmap.CompressFormat.JPEG, 25, outFile); 
+0

Nicht sicher, ob OP ist bewusst, aber dies wird die Qualität der JPEG erheblich senken. – earthw0rmjim

+0

Er möchte niedrige Bildgröße sowie niedrige Pixel. –

-2
public static Bitmap decodeSampledBitmapFromResource(String imagePath, 
     int reqWidth, int reqHeight) { 
    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 

    Uri uri = Uri.parse(imagePath); 

    BitmapFactory.decodeFile(uri.getPath(), options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, 
      reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 

    options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    options.inDither = true; 
    return BitmapFactory.decodeFile(uri.getPath(), options); 
} 

private static int calculateInSampleSize(BitmapFactory.Options options, 
     int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     final int halfHeight = height/2; 
     final int halfWidth = width/2; 
     // Calculate the largest inSampleSize value that is a power of 2 and 
     // keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) > reqHeight 
       && (halfWidth/inSampleSize) > reqWidth) { 
      inSampleSize *= 3; 
     } 
    } 
    return inSampleSize; 
} 

Versuchen Sie, diese

+0

Warum unten abstimmen? – darwin

2
try { 
      BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 

      bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), 
         bitmapOptions); 

      bitmap =getResizedBitmap(bitmap); 
      imageView.setImageBitmap(bitmap); 


       String path = android.os.Environment 
         .getExternalStorageDirectory() 
         + File.separator 
         + "Phoenix" + File.separator + "default"; 
       f.delete(); 
       OutputStream outFile = null; 
       File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg"); 
       try { 
        outFile = new FileOutputStream(file); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile); 
        outFile.flush(); 
        outFile.close(); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
      } 

public Bitmap getResizedBitmap(Bitmap myBitmap) { 
     final int maxSize = 1024; 
     int outWidth; 
     int outHeight; 
     int inWidth = myBitmap.getWidth(); 
     int inHeight = myBitmap.getHeight(); 
     if (inWidth > inHeight) { 
      outWidth = maxSize; 
      outHeight = (inHeight * maxSize)/inWidth; 
     } else { 
      outHeight = maxSize; 
      outWidth = (inWidth * maxSize)/inHeight; 
     } 

     return Bitmap.createScaledBitmap(myBitmap, outWidth, outHeight, false); 
    } 
+0

Es funktioniert gut, Danke Sohail Zahid. –

+1

@HarishReddy Sie sind willkommen –