예제 #1
0
 /**
  * Override paint method
  *
  * @see javax.swing.JComponent#paint(java.awt.Graphics)
  */
 public void paint(Graphics inG) {
   super.paint(inG);
   if (_photo != null) {
     // read thumbnail in separate thread
     if (_thumbnail == null && !_loadingImage && !_loadFailed) {
       _loadingImage = true;
       new Thread(this).start();
     }
     // if loading, display message
     if (_loadingImage) {
       inG.setColor(Color.BLACK);
       inG.drawString(LOADING_STRING, 10, 30);
     } else if (_thumbnail != null && !_loadFailed) {
       // Copy scaled, smoothed (and rotated) image into scaled
       int usableWidth = getParent().getWidth() - 10;
       int usableHeight = (_inPanel ? usableWidth : getHeight() - 10);
       Image scaled =
           ImageUtils.rotateImage(
               _thumbnail, usableWidth, usableHeight, _photo.getRotationDegrees());
       int scaleWidth = scaled.getWidth(null);
       int scaleHeight = scaled.getHeight(null);
       // Draw scaled / rotated image to component
       int horizOffset = (getWidth() - scaleWidth) / 2;
       int vertOffset = (getHeight() - scaleHeight) / 2;
       inG.drawImage(scaled, horizOffset, vertOffset, scaleWidth, scaleHeight, null);
       // Special resize behaviour when locked inside details panel
       if (_inPanel && (getHeight() < getWidth() || getHeight() > usableWidth)) {
         Dimension newsize = new Dimension(usableWidth, usableWidth);
         setPreferredSize(newsize);
         setSize(newsize);
         invalidate();
         // Schedule a relayout because the size has changed
         SwingUtilities.invokeLater(
             new Runnable() {
               public void run() {
                 try {
                   Thread.sleep(200);
                 } catch (InterruptedException e) {
                 }
                 getParent().getParent().getParent().validate();
               }
             });
       }
     }
   }
 }
예제 #2
0
  private static boolean rotateImage(
      String imageFile, String outputFile, int quality, Bitmap.CompressFormat compressFormat) {
    int degree = ImageUtils.getExifOrientation(imageFile);
    if (degree != 0) {
      Log.i(TAG, "rotate image from:" + degree);
      Bitmap origin = BitmapFactory.decodeFile(imageFile);
      Bitmap rotate = ImageUtils.rotateImage(degree, origin);
      if (rotate != null) {
        ImageUtils.saveBitmap(rotate, outputFile, compressFormat, quality);
        rotate.recycle();
        origin.recycle();
        return true;
      } else {
        Log.i(TAG, "rotate image failed:" + imageFile);
        Log.i(TAG, "use origin image");
      }
      origin.recycle();
    }

    return false;
  }
예제 #3
0
  /**
   * 压缩图片文件
   *
   * @param srcFile 源文件
   * @param dstFile 目标文件
   * @param maxWidth 最大宽度
   * @param maxHeight 最大高度
   * @return true进行了压缩,false无需压缩
   */
  public static boolean compressImageFile(
      String srcFile,
      String dstFile,
      int maxWidth,
      int maxHeight,
      int quality,
      Bitmap.CompressFormat compressFormat) {

    Log.i(TAG, "compress file:" + srcFile);
    Log.i(TAG, "file length:" + (int) (new File(srcFile).length() / 1024d) + "kb");
    Log.i(TAG, "output size:(" + maxWidth + ", " + maxHeight + ")");

    BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
    decodeOptions.inJustDecodeBounds = true;

    BitmapFactory.decodeFile(srcFile, decodeOptions);
    int actualWidth = decodeOptions.outWidth;
    int actualHeight = decodeOptions.outHeight;

    Log.i(TAG, "input size:(" + actualWidth + ", " + actualHeight + ")");

    if (actualWidth < maxWidth && actualHeight < maxHeight) {
      Log.i(TAG, "stop compress: input size < output size");
      return rotateImage(srcFile, dstFile, quality, compressFormat);
    }

    int sampleSize;
    int w;
    int h;
    if (actualWidth * maxHeight > maxWidth * actualHeight) {
      w = maxWidth;
      h = (int) (w * actualHeight / (double) actualWidth);
      sampleSize = (int) (actualWidth / (double) maxWidth);
    } else {
      h = maxHeight;
      w = (int) (h * actualWidth / (double) actualHeight);
      sampleSize = (int) (actualHeight / (double) maxHeight);
    }

    Log.i(TAG, "in simple size:" + sampleSize);

    decodeOptions.inJustDecodeBounds = false;
    decodeOptions.inSampleSize = sampleSize;
    decodeOptions.inPreferredConfig = Bitmap.Config.RGB_565;
    decodeOptions.inPurgeable = true;
    decodeOptions.inInputShareable = true;
    Bitmap bitmap = null;
    try {
      bitmap = BitmapFactory.decodeFile(srcFile, decodeOptions);
    } catch (OutOfMemoryError error) {
      error.printStackTrace();
      Log.i(
          TAG, "OutOfMemoryError:" + srcFile + ", size(" + actualWidth + ", " + actualHeight + ")");
    }

    if (bitmap == null) {
      Log.i(TAG, "stop compress:decode file error");
      return false;
    }

    Log.i(TAG, "origin bitmap size:(" + bitmap.getWidth() + ", " + bitmap.getHeight() + ")");

    if (bitmap.getWidth() > maxWidth || bitmap.getHeight() > maxHeight) {
      Bitmap tempBitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
      bitmap.recycle();
      bitmap = tempBitmap;
      Log.i(TAG, "scale down:(" + bitmap.getWidth() + ", " + bitmap.getHeight() + ")");
    }

    int degree = ImageUtils.getExifOrientation(srcFile);
    if (degree != 0) {
      Log.i(TAG, "rotate image from:" + degree);
      Bitmap rotate = ImageUtils.rotateImage(degree, bitmap);
      bitmap.recycle();
      bitmap = rotate;
    }

    ImageUtils.saveBitmap(bitmap, dstFile, compressFormat, quality);

    Log.i(TAG, "output file length:" + (int) (new File(dstFile).length() / 1024d) + "kb");
    Log.i(TAG, "------------------ compress file complete ---------------");
    return true;
  }