コード例 #1
0
ファイル: BitmapUtils.java プロジェクト: yangjianhong/Course
  /** 压缩图片 */
  public static Bitmap compressImageFromFile(String srcPath) {
    Options newOpts = new Options();
    newOpts.inJustDecodeBounds = true; // 只读边,不读内容
    Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);

    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;
    float hh = 800f; //
    float ww = 480f; //
    int be = 1;
    if (w > h && w > ww) {
      be = (int) (newOpts.outWidth / ww);
    } else if (w < h && h > hh) {
      be = (int) (newOpts.outHeight / hh);
    }
    if (be <= 0) be = 1;
    newOpts.inSampleSize = be; // 设置采样率

    newOpts.inPreferredConfig = Config.ARGB_8888; // 该模式是默认的,可不设
    newOpts.inPurgeable = true; // 同时设置才会有效
    newOpts.inInputShareable = true; // 当系统内存不够时候图片自动被回收

    bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
    // return compressBmpFromBmp(bitmap);//原来的方法调用了这个方法企图进行二次压缩
    // 其实是无效的,大家尽管尝试
    return bitmap;
  }
コード例 #2
0
  @Override
  public Image newImage(String fileName, ImageFormat format) {
    Config config = null;
    if (format == ImageFormat.RGB565) config = Config.RGB_565;
    else if (format == ImageFormat.ARGB4444) config = Config.ARGB_4444;
    else config = Config.ARGB_8888;

    Options options = new Options();
    options.inPreferredConfig = config;

    InputStream in = null;
    Bitmap bitmap = null;
    try {
      in = assets.open(fileName);
      bitmap = BitmapFactory.decodeStream(in, null, options);
      if (bitmap == null)
        throw new RuntimeException("Couldn't load bitmap from asset '" + fileName + "'");
    } catch (IOException e) {
      throw new RuntimeException("Couldn't load bitmap from asset '" + fileName + "'");
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
        }
      }
    }

    if (bitmap.getConfig() == Config.RGB_565) format = ImageFormat.RGB565;
    else if (bitmap.getConfig() == Config.ARGB_4444) format = ImageFormat.ARGB4444;
    else format = ImageFormat.ARGB8888;

    return new AndroidImage(bitmap, format);
  }
コード例 #3
0
  private Bitmap getBitmap(String fileName) {
    if (this.bitmaps.containsKey(fileName)) {
      return this.bitmaps.get(fileName);
    }

    Options options = new Options();
    options.inPreferredConfig = Config.ARGB_8888;

    InputStream in = null;
    Bitmap bitmap = null;
    try {
      in = this.assets.open(fileName);
      bitmap = BitmapFactory.decodeStream(in, null, options);
      if (bitmap == null)
        throw new RuntimeException("Couldn't load bitmap from asset '" + fileName + "'");

    } catch (IOException e) {
      throw new RuntimeException("Couldn't load bitmap from asset '" + fileName + "'");

    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
        }
      }
    }

    this.bitmaps.put(fileName, bitmap);

    return bitmap;
  }
コード例 #4
0
ファイル: ab.java プロジェクト: cpjreynolds/swar
 private n<Bitmap> b(i iVar) {
   Object decodeByteArray;
   byte[] bArr = iVar.b;
   Options options = new Options();
   if (this.c == 0 && this.d == 0) {
     options.inPreferredConfig = this.b;
     decodeByteArray = BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options);
   } else {
     options.inJustDecodeBounds = true;
     BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options);
     int i = options.outWidth;
     int i2 = options.outHeight;
     int b = b(this.c, this.d, i, i2);
     int b2 = b(this.d, this.c, i2, i);
     options.inJustDecodeBounds = false;
     options.inSampleSize = a(i, i2, b, b2);
     Bitmap decodeByteArray2 = BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options);
     if (decodeByteArray2 == null
         || (decodeByteArray2.getWidth() <= b && decodeByteArray2.getHeight() <= b2)) {
       Bitmap bitmap = decodeByteArray2;
     } else {
       decodeByteArray = Bitmap.createScaledBitmap(decodeByteArray2, b, b2, true);
       decodeByteArray2.recycle();
     }
   }
   if (decodeByteArray == null) {
     return n.a(new k(iVar));
   }
   return n.a(decodeByteArray, y.a(iVar));
 }
コード例 #5
0
  @Override
  public Pixmap newPixmap(String fileName, PixmapFormat format) {
    Config config = null;
    if (format == PixmapFormat.RGB565) config = Config.RGB_565;
    else if (format == PixmapFormat.ARGB4444) config = Config.ARGB_4444;
    else config = Config.ARGB_8888;

    Options options = new Options();
    options.inPreferredConfig = config;

    InputStream in = null;
    Bitmap bitmap = null;
    try {
      in = assets.open(fileName);
      bitmap = BitmapFactory.decodeStream(in);
      if (bitmap == null)
        throw new RuntimeException("No se ha podido cargar bitmap desde asset '" + fileName + "'");
    } catch (IOException e) {
      throw new RuntimeException("No se ha podido cargar bitmap desde asset '" + fileName + "'");
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
        }
      }
    }

    if (bitmap.getConfig() == Config.RGB_565) format = PixmapFormat.RGB565;
    else if (bitmap.getConfig() == Config.ARGB_4444) format = PixmapFormat.ARGB4444;
    else format = PixmapFormat.ARGB8888;

    return new AndroidPixmap(bitmap, format);
  }
コード例 #6
0
 private void copyOptions(Options srcOptions, Options destOptions) {
   destOptions.inDensity = srcOptions.inDensity;
   destOptions.inDither = srcOptions.inDither;
   destOptions.inInputShareable = srcOptions.inInputShareable;
   destOptions.inJustDecodeBounds = srcOptions.inJustDecodeBounds;
   destOptions.inPreferredConfig = srcOptions.inPreferredConfig;
   destOptions.inPurgeable = srcOptions.inPurgeable;
   destOptions.inSampleSize = srcOptions.inSampleSize;
   destOptions.inScaled = srcOptions.inScaled;
   destOptions.inScreenDensity = srcOptions.inScreenDensity;
   destOptions.inTargetDensity = srcOptions.inTargetDensity;
   destOptions.inTempStorage = srcOptions.inTempStorage;
   if (Build.VERSION.SDK_INT >= 10) copyOptions10(srcOptions, destOptions);
   if (Build.VERSION.SDK_INT >= 11) copyOptions11(srcOptions, destOptions);
 }
コード例 #7
0
  @Override
  public Pixmap newPixmap(String fileName, PixmapFormat format) {
    Config config = null;
    if (format == PixmapFormat.RGR565) {
      config = Config.RGB_565;
    } else if (format == PixmapFormat.ARGB4444) {
      config = Config.ARGB_4444;
    } else {
      config = Config.ARGB_8888;
    }

    Options options = new Options();
    options.inPreferredConfig = config;

    InputStream in = null;
    Bitmap bitmap = null;

    try {
      in = assets.open(fileName);
      bitmap = BitmapFactory.decodeStream(in);
      if (bitmap == null) {
        throw new RuntimeException("Can't load bitmap from asset: " + fileName);
      }
    } catch (IOException e) {
      throw new RuntimeException("Can't load bitmap from asset: " + fileName);
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
          throw new RuntimeException("Can't close stream");
        }
      }
    }

    if (bitmap.getConfig() == Config.RGB_565) {
      format = PixmapFormat.RGR565;
    } else if (bitmap.getConfig() == Config.ARGB_4444) {
      format = PixmapFormat.ARGB4444;
    } else {
      format = PixmapFormat.ARGB8888;
    }

    return new AndroidPixmap(bitmap, format);
  }
コード例 #8
0
    @Override
    public Bitmap run(JobContext jc) {
      if (!prepareInputFile(jc)) return null;
      int targetSize = MediaItem.getTargetSize(mType);
      Options options = new Options();
      options.inPreferredConfig = Config.ARGB_8888;
      Bitmap bitmap =
          DecodeUtils.decodeThumbnail(
              jc, mFileDescriptor.getFileDescriptor(), options, targetSize, mType);

      if (jc.isCancelled() || bitmap == null) {
        return null;
      }

      if (mType == MediaItem.TYPE_MICROTHUMBNAIL) {
        bitmap = BitmapUtils.resizeAndCropCenter(bitmap, targetSize, true);
      } else {
        bitmap = BitmapUtils.resizeDownBySideLength(bitmap, targetSize, true);
      }
      return bitmap;
    }
コード例 #9
0
    public SimulationView(Context context) {
      super(context);
      mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

      DisplayMetrics metrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(metrics);
      mXDpi = metrics.xdpi;
      mYDpi = metrics.ydpi;
      mMetersToPixelsX = mXDpi / 0.0254f;
      mMetersToPixelsY = mYDpi / 0.0254f;

      // rescale the ball so it's about 0.5 cm on screen
      Bitmap ball = BitmapFactory.decodeResource(getResources(), R.drawable.ballw);
      final int dstWidth = (int) (sBallDiameter * mMetersToPixelsX + 0.5f);
      final int dstHeight = (int) (sBallDiameter * mMetersToPixelsY + 0.5f);
      mBitmap = Bitmap.createScaledBitmap(ball, dstWidth, dstHeight, true);

      Options opts = new Options();
      opts.inDither = true;
      opts.inPreferredConfig = Bitmap.Config.RGB_565;
      mPool = BitmapFactory.decodeResource(getResources(), R.drawable.tableg, opts);
    }
コード例 #10
0
  // -- Intenta cargar un bitmap desde un archivo assets usando el PixmapFormat para el formato que
  // se le halla especificado--//
  @Override
  public Pixmap newPixmap(String fileName, PixmapFormat format) {
    Config config = null;
    // -- Aqui convertimos el formato a una de las constantes de la clase android config--//
    if (format == PixmapFormat.RGB565) config = Config.RGB_565;
    else if (format == PixmapFormat.ARGB4444) config = Config.ARGB_4444;
    else config = Config.ARGB_8888;

    Options options = new Options();
    options.inPreferredConfig = config; // Configuramos nuestro formato de colo preferido

    InputStream in = null;
    Bitmap bitmap = null;
    try {
      // -- Tratamos de cargar el bitmap desde el assets atravez de BitmapFactory
      in = assets.open(fileName);
      bitmap = BitmapFactory.decodeStream(in);
      if (bitmap == null)
        throw new RuntimeException("No se ha podido cargar bitmap desde asset '" + fileName + "'");
    } catch (IOException e) {
      throw new RuntimeException("No se ha podido cargar bitmap desde asset '" + fileName + "'");
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
        }
      }
    }
    // -- Se vuelve a reslizar una condicional para comprobar
    if (bitmap.getConfig() == Config.RGB_565) format = PixmapFormat.RGB565;
    else if (bitmap.getConfig() == Config.ARGB_4444) format = PixmapFormat.ARGB4444;
    else format = PixmapFormat.ARGB8888;

    return new AndroidPixmap(
        bitmap,
        format); // Construimos una nueva instancia basado en el bitmap y en el formato configurado
  }
コード例 #11
0
  // This is the same as the method above except the source data comes
  // from a file descriptor instead of a byte array.
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  public static Bitmap decodeUsingPool(
      JobContext jc, FileDescriptor fileDescriptor, Options options) {
    if (options == null) options = new BitmapFactory.Options();
    if (options.inSampleSize < 1) options.inSampleSize = 1;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inBitmap =
        (options.inSampleSize == 1) ? findCachedBitmap(jc, fileDescriptor, options) : null;
    try {
      Bitmap bitmap = DecodeUtils.decode(jc, fileDescriptor, options);
      if (options.inBitmap != null && options.inBitmap != bitmap) {
        GalleryBitmapPool.getInstance().put(options.inBitmap);
        options.inBitmap = null;
      }
      return bitmap;
    } catch (IllegalArgumentException e) {
      if (options.inBitmap == null) throw e;

      Log.w(TAG, "decode fail with a given bitmap, try decode to a new bitmap");
      GalleryBitmapPool.getInstance().put(options.inBitmap);
      options.inBitmap = null;
      return decode(jc, fileDescriptor, options);
    }
  }
コード例 #12
0
 /**
  * Sets {@link android.graphics.Bitmap.Config bitmap config} for image decoding. Default value -
  * {@link android.graphics.Bitmap.Config#ARGB_8888}
  */
 public Builder bitmapConfig(Bitmap.Config bitmapConfig) {
   if (bitmapConfig == null) throw new IllegalArgumentException("bitmapConfig can't be null");
   decodingOptions.inPreferredConfig = bitmapConfig;
   return this;
 }
コード例 #13
0
    @Override
    public RemoteViews getViewAt(int position) {
      if (mCurrentFolder == null) {
        Log.w(TAG, "No current folder data available.");
        return null;
      }

      BookmarkNode bookmark = getBookmarkForPosition(position);
      if (bookmark == null) {
        Log.w(TAG, "Couldn't get bookmark for position " + position);
        return null;
      }

      if (bookmark == mCurrentFolder && bookmark.parent() == null) {
        Log.w(TAG, "Invalid bookmark data: loop detected.");
        return null;
      }

      String title = bookmark.name();
      String url = bookmark.url();
      long id = (bookmark == mCurrentFolder) ? bookmark.parent().id() : bookmark.id();

      // Two layouts are needed because RemoteView does not supporting changing the scale type
      // of an ImageView: boomarks crop their thumbnails, while folders stretch their icon.
      RemoteViews views =
          !bookmark.isUrl()
              ? new RemoteViews(
                  mContext.getPackageName(), R.layout.bookmark_thumbnail_widget_item_folder)
              : new RemoteViews(mContext.getPackageName(), R.layout.bookmark_thumbnail_widget_item);

      // Set the title of the bookmark. Use the url as a backup.
      views.setTextViewText(R.id.label, TextUtils.isEmpty(title) ? url : title);

      if (!bookmark.isUrl()) {
        int thumbId =
            (bookmark == mCurrentFolder)
                ? R.drawable.thumb_bookmark_widget_folder_back_holo
                : R.drawable.thumb_bookmark_widget_folder_holo;
        views.setImageViewResource(R.id.thumb, thumbId);
        views.setImageViewResource(R.id.favicon, R.drawable.ic_bookmark_widget_bookmark_holo_dark);
      } else {
        // RemoteViews require a valid bitmap config.
        Options options = new Options();
        options.inPreferredConfig = Config.ARGB_8888;

        byte[] favicon = bookmark.favicon();
        if (favicon != null && favicon.length > 0) {
          views.setImageViewBitmap(
              R.id.favicon, BitmapFactory.decodeByteArray(favicon, 0, favicon.length, options));
        } else {
          views.setImageViewResource(R.id.favicon, org.chromium.chrome.R.drawable.globe_favicon);
        }

        byte[] thumbnail = bookmark.thumbnail();
        if (thumbnail != null && thumbnail.length > 0) {
          views.setImageViewBitmap(
              R.id.thumb, BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length, options));
        } else {
          views.setImageViewResource(R.id.thumb, R.drawable.browser_thumbnail);
        }
      }

      Intent fillIn;
      if (!bookmark.isUrl()) {
        fillIn =
            new Intent(getChangeFolderAction(mContext))
                .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId)
                .putExtra(BookmarkColumns._ID, id);
      } else {
        fillIn = new Intent(Intent.ACTION_VIEW);
        if (!TextUtils.isEmpty(url)) {
          fillIn = fillIn.addCategory(Intent.CATEGORY_BROWSABLE).setData(Uri.parse(url));
        } else {
          fillIn = fillIn.addCategory(Intent.CATEGORY_LAUNCHER);
        }
      }
      views.setOnClickFillInIntent(R.id.list_item, fillIn);
      return views;
    }
コード例 #14
0
 @Override
 public Object getItem(int position) {
   Options opts = new BitmapFactory.Options();
   opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
   return BitmapFactory.decodeResource(mContext.getResources(), mThumbs.get(position), opts);
 }