Android Open Source - my-wallpaper Image Utilities






From Project

Back to project page my-wallpaper.

License

The source code is released under:

MIT License

If you think the Android project my-wallpaper listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2008 Google Inc./* ww w .j  a  va  2s.  c  o m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.koonen.photostream;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import java.util.Random;

/**
 * This class contains various utilities to manipulate Bitmaps. The methods of
 * this class, although static, are not thread safe and cannot be invoked by
 * several threads at the same time. Synchronization is required by the caller.
 */
public final class ImageUtilities {
  private static final float PHOTO_BORDER_WIDTH = 3.0f;
  private static final int PHOTO_BORDER_COLOR = 0xffffffff;

  private static final float ROTATION_ANGLE_MIN = 2.5f;
  private static final float ROTATION_ANGLE_EXTRA = 5.5f;

  private static final Random sRandom = new Random();
  private static final Paint sPaint = new Paint(Paint.ANTI_ALIAS_FLAG
      | Paint.FILTER_BITMAP_FLAG);
  private static final Paint sStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

  static {
    sStrokePaint.setStrokeWidth(PHOTO_BORDER_WIDTH);
    sStrokePaint.setStyle(Paint.Style.STROKE);
    sStrokePaint.setColor(PHOTO_BORDER_COLOR);
  }

  /**
   * Rotate specified Bitmap by a random angle. The angle is either negative
   * or positive, and ranges, in degrees, from 2.5 to 8. After rotation a
   * frame is overlaid on top of the rotated image.
   * 
   * This method is not thread safe.
   * 
   * @param bitmap
   *            The Bitmap to rotate and apply a frame onto.
   * 
   * @return A new Bitmap whose dimension are different from the original
   *         bitmap.
   */
  static Bitmap rotateAndFrame(Bitmap bitmap) {
    final boolean positive = sRandom.nextFloat() >= 0.5f;
    final float angle = (ROTATION_ANGLE_MIN + sRandom.nextFloat()
        * ROTATION_ANGLE_EXTRA)
        * (positive ? 1.0f : -1.0f);
    final double radAngle = Math.toRadians(angle);

    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    final double cosAngle = Math.abs(Math.cos(radAngle));
    final double sinAngle = Math.abs(Math.sin(radAngle));

    final int strokedWidth = (int) (bitmapWidth + 2 * PHOTO_BORDER_WIDTH);
    final int strokedHeight = (int) (bitmapHeight + 2 * PHOTO_BORDER_WIDTH);

    final int width = (int) (strokedHeight * sinAngle + strokedWidth
        * cosAngle);
    final int height = (int) (strokedWidth * sinAngle + strokedHeight
        * cosAngle);

    final float x = (width - bitmapWidth) / 2.0f;
    final float y = (height - bitmapHeight) / 2.0f;

    final Bitmap decored = Bitmap.createBitmap(width, height,
        Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(decored);

    canvas.rotate(angle, width / 2.0f, height / 2.0f);
    canvas.drawBitmap(bitmap, x, y, sPaint);
    canvas.drawRect(x, y, x + bitmapWidth, y + bitmapHeight, sStrokePaint);

    return decored;
  }

  /**
   * Scales the specified Bitmap to fit within the specified dimensions. After
   * scaling, a frame is overlaid on top of the scaled image.
   * 
   * This method is not thread safe.
   * 
   * @param bitmap
   *            The Bitmap to scale to fit the specified dimensions and to
   *            apply a frame onto.
   * @param width
   *            The maximum width of the new Bitmap.
   * @param height
   *            The maximum height of the new Bitmap.
   * 
   * @return A scaled version of the original bitmap, whose dimension are less
   *         than or equal to the specified width and height.
   */
  static Bitmap scaleAndFrame(Bitmap bitmap, int width, int height) {
    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    final float scale = Math.min((float) width / (float) bitmapWidth,
        (float) height / (float) bitmapHeight);

    final int scaledWidth = (int) (bitmapWidth * scale);
    final int scaledHeight = (int) (bitmapHeight * scale);

    final Bitmap decored = Bitmap.createScaledBitmap(bitmap, scaledWidth,
        scaledHeight, true);
    final Canvas canvas = new Canvas(decored);

    final int offset = (int) (PHOTO_BORDER_WIDTH / 2);
    sStrokePaint.setAntiAlias(false);
    canvas.drawRect(offset, offset, scaledWidth - offset - 1, scaledHeight
        - offset - 1, sStrokePaint);
    sStrokePaint.setAntiAlias(true);

    return decored;

  }

  static Bitmap scale(Bitmap bitmap, int width, int height, boolean scaleBoth) {
    int scaledWidth = width;
    int scaledHeight = height;
    if (!scaleBoth) {
      int bitmapWidth = bitmap.getWidth();
      int bitmapHeight = bitmap.getHeight();

      float scale = Math.min((float) width / (float) bitmapWidth,
          (float) height / (float) bitmapHeight);

      scaledWidth = (int) (bitmapWidth * scale);
      scaledHeight = (int) (bitmapHeight * scale);
    }

    Bitmap decored = Bitmap.createScaledBitmap(bitmap, scaledWidth,
        scaledHeight, true);

    return decored;

  }

  public static int calculateSampleSize(final int screenWidth,
      final int screenHeight, Integer bitmapWidth, Integer bitmapHeight) {
    // float size = bitmapWidth / (float) screenWidth;
    // if (bitmapWidth < bitmapHeight) {
    // size = bitmapHeight / (float) screenHeight;
    // }
    float size = Math.min(bitmapWidth / (float) screenWidth, bitmapHeight
        / (float) screenHeight);
    int sampleSize = Math.round(size);
    if (sampleSize < 1) {
      sampleSize = 1;
    }

    return sampleSize;
  }

  public static int calculateSizeBitmapByScreen(final int screenWidth,
      final int screenHeight, Integer bitmapWidth, Integer bitmapHeight) {
    final float scale = Math.min((float) screenWidth / (float) bitmapWidth,
        (float) screenHeight / (float) bitmapHeight);

    bitmapWidth = (int) (bitmapWidth * scale);
    bitmapHeight = (int) (bitmapHeight * scale);

    return Math.round(scale);
  }
}




Java Source Code List

com.koonen.photostream.ActivityConstants.java
com.koonen.photostream.BootReceiver.java
com.koonen.photostream.CameraPreviewActivity.java
com.koonen.photostream.CategoryActivity.java
com.koonen.photostream.CategoryAdapter.java
com.koonen.photostream.CategoryEditActivity.java
com.koonen.photostream.CropWallpaperTask.java
com.koonen.photostream.Eula.java
com.koonen.photostream.FastBitmapDrawable.java
com.koonen.photostream.FileBrowserActivity.java
com.koonen.photostream.GridLayout.java
com.koonen.photostream.ImageUtilities.java
com.koonen.photostream.PhotostreamActivity.java
com.koonen.photostream.RotationService.java
com.koonen.photostream.ServiceConnector.java
com.koonen.photostream.SetWallpaperTask.java
com.koonen.photostream.UserTask.java
com.koonen.photostream.ViewPhotoActivity.java
com.koonen.photostream.WallPaperExecutor.java
com.koonen.photostream.api.FilePhoto.java
com.koonen.photostream.api.IPhotoService.java
com.koonen.photostream.api.Location.java
com.koonen.photostream.api.PhotoList.java
com.koonen.photostream.api.PhotoSize.java
com.koonen.photostream.api.Photo.java
com.koonen.photostream.api.ResponseHandler.java
com.koonen.photostream.api.ResponseParser.java
com.koonen.photostream.api.ServiceContext.java
com.koonen.photostream.api.ServiceException.java
com.koonen.photostream.api.ServiceManager.java
com.koonen.photostream.api.ServiceNetworkException.java
com.koonen.photostream.api.SourceType.java
com.koonen.photostream.api.Type.java
com.koonen.photostream.api.UserInfo.java
com.koonen.photostream.api.UserNotFoundException.java
com.koonen.photostream.api.User.java
com.koonen.photostream.api.flickr.Auth.java
com.koonen.photostream.api.flickr.FlickrConstants.java
com.koonen.photostream.api.flickr.FlickrService.java
com.koonen.photostream.api.flickr.Perms.java
com.koonen.photostream.dao.CategoryDAO.java
com.koonen.photostream.dao.CategoryList.java
com.koonen.photostream.dao.Category.java
com.koonen.photostream.dao.ImageDAO.java
com.koonen.photostream.dao.PhotoDAO.java
com.koonen.photostream.dao.PhotoUrlListProvider.java
com.koonen.photostream.dao.PhotoUrlList.java
com.koonen.photostream.dao.PhotoUrl.java
com.koonen.photostream.effects.EffectsApplier.java
com.koonen.photostream.effects.EffectsFactory.java
com.koonen.photostream.effects.Rotate3dAnimation.java
com.koonen.photostream.effects.TypeEffect.java
com.koonen.photostream.settings.BackgroundSource.java
com.koonen.photostream.settings.Network.java
com.koonen.photostream.settings.UserPreferences.java
com.koonen.photostream.settings.UserSettingsActivity.java
com.koonen.photostream.settings.WallpaperSettingMode.java
com.koonen.utils.ConfigurationReader.java
com.koonen.utils.DialogUtils.java
com.koonen.utils.Enumeration.java
com.koonen.utils.GroupUtils.java
com.koonen.utils.MailSender.java
com.koonen.utils.StatisticUtils.java
com.koonen.utils.StreamUtils.java