예제 #1
0
  // AUT:[email protected]. DATE:2012-12-22. START.
  public static Bitmap resizeAndCropCenterExt(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    if (w == size && h == size) return bitmap;

    // scale the image so that the shorter side equals to the target;
    // the longer side will be center-cropped.
    float scale = (float) size / Math.min(w, h);

    int width = Math.round(scale * bitmap.getWidth());
    int height = Math.round(scale * bitmap.getHeight());
    if (width > height) {
      int largeSize = (int) (width <= size * 1.5 ? width : size * 1.5);
      Bitmap target = Bitmap.createBitmap(largeSize, size, getConfig(bitmap));
      Canvas canvas = new Canvas(target);
      canvas.translate((largeSize - width) / 2f, (size - height) / 2f);
      canvas.scale(scale, scale);
      Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
      canvas.drawBitmap(bitmap, 0, 0, paint);
      if (recycle) bitmap.recycle();
      return target;
    } else {
      int largeSize = (int) (height <= size * 1.5 ? height : size * 1.5);
      Bitmap target = Bitmap.createBitmap(size, largeSize, getConfig(bitmap));
      Canvas canvas = new Canvas(target);
      canvas.translate((size - width) / 2f, (largeSize - height) / 2f);
      canvas.scale(scale, scale);
      Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
      canvas.drawBitmap(bitmap, 0, 0, paint);
      if (recycle) bitmap.recycle();
      return target;
    }
  }
예제 #2
0
  @Override
  public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());

    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;

    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
      source.recycle();
    }
    if (source.getConfig() == null) {
      return source;
    }

    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader =
        new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);

    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);

    squaredBitmap.recycle();
    return bitmap;
  }
예제 #3
0
    // Call after onPreExecute method
    protected Void doInBackground(String... urls) {
      // g_imagenes.get(0);
      for (int i = 0; i < g_imagenes.size(); i++) {
        Bitmap bitmap = null;
        Uri uri = null;
        try {
          uri =
              Uri.withAppendedPath(
                  MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + g_imagenes.get(i));

          bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
          if (bitmap != null) {
            Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 400, 400, true);
            bitmap.recycle();
            if (newBitmap != null) {
              g_imagenesBitmap.add(newBitmap);
            }
          }
        } catch (IOException e) {
          cancel(true);
        }
        bitmap.recycle();
      }
      return null;
    }
예제 #4
0
  private void cropAndResize() throws Exception {

    if (pictureFile.exists()) {

      Bitmap bitmap = decodeSampledBitmapFromFile(pictureFile.getAbsolutePath(), size, size);

      int width = bitmap.getWidth();
      int height = bitmap.getHeight();

      int currentSize = (width > height) ? height : width;
      Bitmap croppedBitmap = crop(bitmap, currentSize);
      bitmap.recycle();
      Bitmap resizedBitmap = resize(croppedBitmap, size);

      Bitmap finalBitmap = null;
      // if picture is landscape
      if (width > height) finalBitmap = rotate(resizedBitmap, 90);
      else finalBitmap = resizedBitmap;

      pictureFile.delete();

      try {
        FileOutputStream out = new FileOutputStream(pictureFile);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, quality, out);
        out.flush();
        out.close();
        finalBitmap.recycle();
        resizedBitmap.recycle();

      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    delegate.getResult(pictureFile.getPath());
  }
 private void cleanup() {
   barH = null;
   barS = null;
   barV = null;
   txt = null;
   txtH = null;
   txtS = null;
   txtV = null;
   lblCurrent = null;
   hsv = null;
   hsvTmp = null;
   if (bmpH != null) {
     bmpH.recycle();
     bmpH = null;
   }
   if (bmpS != null) {
     bmpS.recycle();
     bmpS = null;
   }
   if (bmpV != null) {
     bmpV.recycle();
     bmpV = null;
   }
   bmpBuf = null;
   bgCurrent = null;
   parentView = null;
   listener = null;
   System.gc();
 }
예제 #6
0
  /** Null out the static references when the MapView is detached to prevent memory leaks. */
  @Override
  public void onDetach(MapView mapView) {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
      if (mIcon instanceof BitmapDrawable) {
        final Bitmap bitmap = ((BitmapDrawable) mIcon).getBitmap();
        if (bitmap != null) {
          bitmap.recycle();
        }
      }
    }
    mIcon = null;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
      if (mImage instanceof BitmapDrawable) {
        final Bitmap bitmap = ((BitmapDrawable) mImage).getBitmap();
        if (bitmap != null) {
          bitmap.recycle();
        }
      }
    }
    cleanDefaults();
    this.mOnMarkerClickListener = null;
    this.mOnMarkerDragListener = null;
    this.resource = null;
    setRelatedObject(null);
    closeInfoWindow();
    onDestroy();

    super.onDetach(mapView);
  }
예제 #7
0
 public static Bitmap ImageCrop(Bitmap bitmap, int width, int height, boolean isRecycled) {
   if (bitmap == null) {
     return null;
   }
   int bitmapWidth = bitmap.getWidth();
   int bitmapHeight = bitmap.getHeight();
   if (bitmapWidth <= width && bitmapHeight <= height) {
     return bitmap;
   }
   Matrix matrix = new Matrix();
   float scaleWidth = ((float) width / bitmapWidth);
   float scaleHeight = ((float) height / bitmapHeight);
   if (scaleWidth > scaleHeight) {
     matrix.postScale(scaleWidth, scaleWidth);
   } else {
     matrix.postScale(scaleHeight, scaleHeight);
   }
   Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
   if (isRecycled && bitmap != null && !bitmap.equals(newbmp) && !bitmap.isRecycled()) {
     bitmap.recycle();
     bitmap = null;
   }
   int w = newbmp.getWidth();
   int h = newbmp.getHeight();
   int wh = w > h ? h : w;
   int retX = w > h ? (w - h) / 2 : 0;
   int retY = w > h ? 0 : (h - w) / 2;
   Bitmap bmp = Bitmap.createBitmap(newbmp, retX, retY, wh, wh, null, false);
   if (isRecycled && newbmp != null && !newbmp.equals(bmp) && !newbmp.isRecycled()) {
     newbmp.recycle();
     newbmp = null;
   }
   return bmp;
 }
  @Override
  public Bitmap transform(Bitmap bitmap) {

    if (null == bitmap) return null;

    PaintFlagsDrawFilter fliter =
        new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    if (w <= 0 || h <= 0) {
      return null;
    }
    int r = (w < h ? w : h) / 2;
    Bitmap circle = Bitmap.createBitmap(2 * r, 2 * r, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(circle);
    canvas.setDrawFilter(fliter);

    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    p.setColor(Color.RED);
    canvas.drawCircle(r, r, r, p);
    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    // 生成结果图片
    Bitmap out = Bitmap.createBitmap(2 * r, 2 * r, Bitmap.Config.ARGB_8888);
    Canvas outCan = new Canvas(out);
    outCan.setDrawFilter(fliter);

    outCan.drawBitmap(bitmap, 0, 0, null);
    outCan.drawBitmap(circle, 0, 0, p);
    circle.recycle();
    bitmap.recycle();
    return out;
  }
예제 #9
0
    private Bitmap scaleBitmap(Bitmap bitmap) {
      // resize to desired dimensions
      int width = bitmap.getWidth();
      int height = bitmap.getHeight();
      int[] newSize = getScaleSize(width, height);
      Bitmap workBitmap = Bitmap.createScaledBitmap(bitmap, newSize[0], newSize[1], true);
      if (workBitmap != bitmap) {
        bitmap.recycle();
        bitmap = workBitmap;
        System.gc();
      }

      if (mScaleType == ScaleType.CENTER_CROP) {
        // Crop it
        int diffWidth = newSize[0] - mOutputWidth;
        int diffHeight = newSize[1] - mOutputHeight;
        workBitmap =
            Bitmap.createBitmap(
                bitmap,
                diffWidth / 2,
                diffHeight / 2,
                newSize[0] - diffWidth,
                newSize[1] - diffHeight);
        if (workBitmap != bitmap) {
          bitmap.recycle();
          bitmap = workBitmap;
        }
      }

      return bitmap;
    }
예제 #10
0
  public static Bitmap getSmallAvatarWithRoundedCorner(String url, int reqWidth, int reqHeight) {

    if (!FileManager.isExternalStorageMounted()) {
      return null;
    }

    String absoluteFilePath = FileManager.getFilePathFromUrl(url, FileLocationMethod.avatar_small);
    absoluteFilePath = absoluteFilePath + ".jpg";

    Bitmap bitmap = BitmapFactory.decodeFile(absoluteFilePath);

    if (bitmap == null && !SettingUtility.isEnablePic()) {
      return null;
    }

    if (bitmap == null) {
      boolean result = getBitmapFromNetWork(url, absoluteFilePath, null);
      if (result) bitmap = BitmapFactory.decodeFile(absoluteFilePath);
    }

    if (bitmap != null) {
      if (bitmap.getHeight() < reqHeight || bitmap.getWidth() < reqWidth) {
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
        Bitmap roundedBitmap = ImageEdit.getRoundedCornerBitmap(scaledBitmap);
        bitmap.recycle();
        scaledBitmap.recycle();
        return roundedBitmap;
      }
    }

    return null;
  }
예제 #11
0
  @Override
  public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());

    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;

    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
      source.recycle();
    }

    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

    Canvas canvas = new Canvas(bitmap);

    Paint avatarPaint = new Paint();
    BitmapShader shader =
        new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    avatarPaint.setShader(shader);

    Paint outlinePaint = new Paint();
    outlinePaint.setColor(Color.WHITE);
    outlinePaint.setStyle(Paint.Style.STROKE);
    outlinePaint.setStrokeWidth(STROKE_WIDTH);
    outlinePaint.setAntiAlias(true);

    float r = size / 2f;
    canvas.drawCircle(r, r, r, avatarPaint);
    canvas.drawCircle(r, r, r - STROKE_WIDTH / 2, outlinePaint);

    squaredBitmap.recycle();
    return bitmap;
  }
예제 #12
0
  private byte[] resizeImage(byte[] input) {

    if (input == null) {
      return null;
    }

    Bitmap bitmapOrg = BitmapFactory.decodeByteArray(input, 0, input.length);

    if (bitmapOrg == null) {
      return null;
    }

    int height = bitmapOrg.getHeight();
    int width = bitmapOrg.getWidth();
    int newHeight = THUMBNAIL_HEIGHT;

    float scaleHeight = ((float) newHeight) / height;

    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleHeight, scaleHeight);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);

    bitmapOrg.recycle();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    resizedBitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);

    resizedBitmap.recycle();

    return bos.toByteArray();
  }
예제 #13
0
 @Override
 public void onDetach(final MapView pMapView) {
   this.mTileProvider.detach();
   ctx = null;
   if (mLoadingTile != null) {
     // Only recycle if we are running on a project less than 2.3.3 Gingerbread.
     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
       if (mLoadingTile instanceof BitmapDrawable) {
         final Bitmap bitmap = ((BitmapDrawable) mLoadingTile).getBitmap();
         if (bitmap != null) {
           bitmap.recycle();
         }
       }
     }
     if (mLoadingTile instanceof ReusableBitmapDrawable)
       BitmapPool.getInstance().returnDrawableToPool((ReusableBitmapDrawable) mLoadingTile);
   }
   mLoadingTile = null;
   if (userSelectedLoadingDrawable != null) {
     // Only recycle if we are running on a project less than 2.3.3 Gingerbread.
     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
       if (userSelectedLoadingDrawable instanceof BitmapDrawable) {
         final Bitmap bitmap = ((BitmapDrawable) userSelectedLoadingDrawable).getBitmap();
         if (bitmap != null) {
           bitmap.recycle();
         }
       }
     }
     if (userSelectedLoadingDrawable instanceof ReusableBitmapDrawable)
       BitmapPool.getInstance()
           .returnDrawableToPool((ReusableBitmapDrawable) userSelectedLoadingDrawable);
   }
   userSelectedLoadingDrawable = null;
 }
예제 #14
0
  @SmallTest
  public void testEnd() {
    final String inputText = "hello";
    final Bitmap bmp = getTextImage(inputText, 640, 480);

    // Attempt to initialize the API.
    final TessBaseAPI baseApi = new TessBaseAPI();
    baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE);
    baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_LINE);
    baseApi.setImage(bmp);

    // Ensure that getUTF8Text() fails after end() is called.
    baseApi.end();
    try {
      baseApi.getUTF8Text();
      fail("IllegalStateException not thrown");
    } catch (IllegalStateException e) {
      // Continue
    } finally {
      bmp.recycle();
    }

    // Ensure that reinitializing the API is successful.
    boolean success = baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE);
    assertTrue("API failed to initialize after end()", success);

    // Ensure setImage() does not throw an exception.
    final Bitmap bmp2 = getTextImage(inputText, 640, 480);
    baseApi.setImage(bmp2);

    // Attempt to shut down the API.
    baseApi.end();
    bmp2.recycle();
  }
  private byte[] rotateImage(
      byte[] cameraData, final int imageWidth, final int imageHeight, int currentCameraId) {
    Bitmap landscapeCameraDataBitmap =
        BitmapFactory.decodeByteArray(cameraData, 0, cameraData.length);

    Bitmap portraitBitmap = null;
    if (currentCameraId == getCameraId(Camera.CameraInfo.CAMERA_FACING_FRONT)) { // front camera
      portraitBitmap =
          Bitmap.createBitmap(
              landscapeCameraDataBitmap, 0, 0, imageWidth, imageHeight, rotationMatrixFront, true);
    } else { // back camera
      portraitBitmap =
          Bitmap.createBitmap(
              landscapeCameraDataBitmap, 0, 0, imageWidth, imageHeight, rotationMatrixBack, true);
    }

    landscapeCameraDataBitmap.recycle();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    if (!portraitBitmap.isRecycled()) {
      portraitBitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, stream);
      byte[] portraitCameraData = stream.toByteArray();
      portraitBitmap.recycle();
      return portraitCameraData;
    } else {
      return new byte[0];
    }
  }
  public static Bitmap createBitmapOnHeight(Bitmap bitmap, int height, float degrees) {
    if (bitmap == null) return null;

    float ratio = 0;

    if (degrees == 90 || degrees == 270) {
      ratio = (float) height / bitmap.getWidth();
    } else {
      ratio = (float) height / bitmap.getHeight();
    }

    Matrix matrix = new Matrix();
    matrix.postScale(ratio, ratio);
    Bitmap bm_scale =
        Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    bitmap.recycle();

    Bitmap bm_degress = null;
    if (degrees == 0) {
      bm_degress = bm_scale;
    } else {
      matrix.setRotate(degrees);
      bm_degress =
          Bitmap.createBitmap(
              bm_scale, 0, 0, bm_scale.getWidth(), bm_scale.getHeight(), matrix, false);
      bm_scale.recycle();
    }
    return bm_degress;
  }
 public void cleanup() {
   if (waveform != null) waveform.recycle();
   waveform = null;
   if (processedWaveform != null) processedWaveform.recycle();
   processedWaveform = null;
   if (oldProcessedWaveform != null) oldProcessedWaveform.recycle();
   oldProcessedWaveform = null;
 }
 void recycle() {
   if (bmpNormal != null && !bmpNormal.isRecycled()) {
     bmpNormal.recycle();
   }
   if (bmpPress != null && !bmpPress.isRecycled()) {
     bmpPress.recycle();
   }
 }
예제 #19
0
 public void onDestroy() {
   Log.d(TAG, "onDestroy()");
   // TODO: unbind to avoid memory leak...
   super.onDestroy();
   iv.destroyDrawingCache();
   if (yourSelectedImage != null) yourSelectedImage.recycle();
   if (imageWithBubbles != null) imageWithBubbles.recycle();
 }
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK) return;

    switch (requestCode) {
      case PHOTO_PICKED_WITH_DATA: // 从本地选择图片
        if (bitMap != null && !bitMap.isRecycled()) {
          bitMap.recycle();
        }
        Uri selectedImageUri = data.getData();
        if (selectedImageUri != null) {
          try {
            bitMap =
                BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImageUri));

            bitMap = ImageUtil.zoomBitmap(bitMap, 80, 80);
          } catch (FileNotFoundException e) {
            e.printStackTrace();
          }

          // 下面这两句是对图片按照一定的比例缩放,这样就可以完美地显示出来。有关图片的处理将重新写文章来介绍。
          //				int scale = ImageThumbnail.reckonThumbnail(bitMap.getWidth(),
          //						bitMap.getHeight(), 500, 600);
          //
          //				bitMap = ImageThumbnail.PicZoom(bitMap,
          //						(int) (bitMap.getWidth() / scale),
          //						(int) (bitMap.getHeight() / scale));

          //				photoView.setImageBitmap(bitMap);
          BitmapDrawable bd = new BitmapDrawable(bitMap);
          photoView.setBackgroundDrawable(bd);

          hasImage = true;
        }
        break;
      case CAMERA_WITH_DATA: // 拍照
        Bundle bundle = data.getExtras();
        bitMap = (Bitmap) bundle.get("data");
        if (bitMap != null) bitMap.recycle();

        bitMap = (Bitmap) data.getExtras().get("data");
        //			int scale = ImageThumbnail.reckonThumbnail(bitMap.getWidth(),
        //					bitMap.getHeight(), 500, 600);
        //
        //			bitMap = ImageThumbnail.PicZoom(bitMap,
        //					(int) (bitMap.getWidth() / scale),
        //					(int) (bitMap.getHeight() / scale));

        //			photoView.setImageBitmap(null);
        //			photoView.setImageBitmap(bitMap);
        BitmapDrawable bd = new BitmapDrawable(bitMap);
        photoView.setBackgroundDrawable(bd);
        hasImage = true;

        break;
    }
  }
  @Override
  protected String doInBackground(String... urls) {

    Bitmap bitmap =
        Bitmap.createBitmap(
            PHOTO_SIZE * TILE_COUNT, PHOTO_SIZE * TILE_COUNT, Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);

    int index = 0;
    for (int column = 0; column < TILE_COUNT; column++) {
      for (int row = 0; row < TILE_COUNT; row++) {
        File imageFile = ImageLoader.getInstance().getDiscCache().get(urls[index]);
        try {
          Bitmap tileBitmap = BitmapFactory.decodeStream(new FileInputStream(imageFile));
          canvas.drawBitmap(tileBitmap, column * PHOTO_SIZE, row * PHOTO_SIZE, null);
          tileBitmap.recycle();
        } catch (FileNotFoundException e) {
          L.error("File " + imageFile.getAbsolutePath() + " not found on disk", e);
        }
        index++;
        if (index >= urls.length) index = 0;
      }
    }

    FileOutputStream out = null;
    try {

      File collageFile1 = File.createTempFile("collage", "jpg", contextWrapper.getCacheDir());
      collageFile1.deleteOnExit();

      out = new FileOutputStream(collageFile1);

      bitmap.compress(Bitmap.CompressFormat.JPEG, 95, out);

      return collageFile1.getAbsolutePath();

    } catch (IOException e) {

      L.error("Error creating collage", e);

    } finally {

      bitmap.recycle();

      if (out != null)
        try {
          out.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
    }

    return null;
  }
 public void destory() {
   mPhotoUri = null;
   if (mOrignalBitmap != null) {
     mOrignalBitmap.recycle();
     mOrignalBitmap = null;
   }
   if (mAffectEffctBitmap != null) {
     mAffectEffctBitmap.recycle();
     mAffectEffctBitmap = null;
   }
 }
예제 #23
0
 /**
  * 원본 비트맵 위에 다른 비트맵을 겹침 두 비트맵의 크기가 같거나 비슷할 것을 권장 두 비트맵의 크기가 다를 경우 원본 비트맵에 크기가 맞춰짐
  *
  * @param bmp1 원본 비트맵
  * @param bmp2 원본 위에 겹쳐질 비트맵
  * @return 겹쳐진 비트맵
  */
 public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
   Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
   Bitmap bmOrign = Bitmap.createScaledBitmap(bmp2, bmp1.getWidth(), bmp1.getHeight(), true);
   Canvas canvas = new Canvas(bmOverlay);
   canvas.drawBitmap(bmp1, 0, 0, null);
   canvas.drawBitmap(bmOrign, 0, 0, null);
   bmp1.recycle();
   bmp2.recycle();
   bmOrign.recycle();
   return bmOverlay;
 }
예제 #24
0
  private static Bitmap transform(
      Matrix scaler, Bitmap source, int targetWidth, int targetHeight, int options) {
    boolean scaleUp = (options & OPTIONS_SCALE_UP) != 0;
    boolean recycle = (options & OPTIONS_RECYCLE_INPUT) != 0;

    int deltaX = source.getWidth() - targetWidth;
    int deltaY = source.getHeight() - targetHeight;
    if (!scaleUp && (deltaX < 0 || deltaY < 0)) {
      Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
      Canvas c = new Canvas(b2);

      int deltaXHalf = Math.max(0, deltaX / 2);
      int deltaYHalf = Math.max(0, deltaY / 2);
      Rect src =
          new Rect(
              deltaXHalf,
              deltaYHalf,
              deltaXHalf + Math.min(targetWidth, source.getWidth()),
              deltaYHalf + Math.min(targetHeight, source.getHeight()));
      int dstX = (targetWidth - src.width()) / 2;
      int dstY = (targetHeight - src.height()) / 2;
      Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY);
      c.drawBitmap(source, src, dst, null);
      if (recycle) source.recycle();
      return b2;
    }

    float bitmapWidthF = source.getWidth();
    float bitmapHeightF = source.getHeight();
    float bitmapAspect = bitmapWidthF / bitmapHeightF;
    float viewAspect = (float) targetWidth / targetHeight;

    float scale =
        bitmapAspect > viewAspect ? targetHeight / bitmapHeightF : targetWidth / bitmapWidthF;
    if (scale < .9F || scale > 1F) scaler.setScale(scale, scale);
    else scaler = null;

    Bitmap b1;
    if (scaler != null)
      b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true);
    else b1 = source;

    if (recycle && b1 != source) source.recycle();

    int dx1 = Math.max(0, b1.getWidth() - targetWidth);
    int dy1 = Math.max(0, b1.getHeight() - targetHeight);

    Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight);

    if (b2 != b1 && (recycle || b1 != source)) b1.recycle();

    return b2;
  }
예제 #25
0
 public void nullBitmaps() {
   mCanvas = null;
   if (mBitmap != null) {
     mBitmap.recycle();
   }
   mBitmap = null;
   mBackgroundCanvas = null;
   if (mBackgroundBitmap != null) {
     mBackgroundBitmap.recycle();
   }
   mBackgroundBitmap = null;
 }
예제 #26
0
  public static int loadMipMappedTexture(Bitmap bitmap) {

    GL.glGenTextures(1, singleID, 0);

    int textureID = singleID[0];
    GL.glBindTexture(GL.GL_TEXTURE_2D, textureID);

    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR);
    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);

    if (autoMipMap) {
      // To use automatic mipmap generation
      GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_GENERATE_MIPMAP, GL.GL_TRUE);
      GLUtils.texImage2D(GL.GL_TEXTURE_2D, 0, bitmap, 0);
      return textureID;
    }

    // Original code is buggy on certain devices ("HTC Desire S", "Motorola Milestone (Droid) 3
    // XT860 Android 2.3.5 Gingerbread")
    // Need conversion as a workaround
    Bitmap oldBitmap = bitmap;
    bitmap = oldBitmap.copy(Bitmap.Config.ARGB_8888, true);
    if (bitmap == null) return 0;
    oldBitmap.recycle();

    int level = 0;
    int height = bitmap.getHeight();
    int width = bitmap.getWidth();

    while (height >= 1 || width >= 1) {
      // First of all, generate the texture from our bitmap and set it to the according level
      GLUtils.texImage2D(GL.GL_TEXTURE_2D, level, bitmap, 0);

      if (height == 1 && width == 1) break;

      // Increase the mipmap level
      level++;

      height /= 2;
      if (height == 0) height = 1;
      width /= 2;
      if (width == 0) width = 1;
      Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, width, height, true);

      // Clean up
      bitmap.recycle();
      bitmap = bitmap2;
    }

    return textureID;
  }
예제 #27
0
 protected void recycle() {
   if (_thumb != null) {
     _thumb.recycle();
     _thumb = null;
     MLog.i(TAG, "PhotoChoosingActivity.recycle() _thumb was recycled");
   }
   if (_pic != null) {
     _pic.recycle();
     _pic = null;
     MLog.i(TAG, "PhotoChoosingActivity.recycle() _pic was recycled");
   }
 }
예제 #28
0
 void destroy() {
   if (mViewBitmap != null) mViewBitmap.recycle();
   if (mBitmap != null) mBitmap.recycle();
   if (mK3DRenderer != null) {
     mK3DRenderer.stop();
     mK3DRenderer = null;
   }
   try {
     System.gc();
     System.gc();
   } catch (NullPointerException e) {
   }
 }
 @Override
 protected void onDetachedFromWindow() {
   if (waveBmp != null) {
     waveBmp.recycle();
   }
   if (bigBubble != null) {
     bigBubble.recycle();
   }
   if (smallBubble != null) {
     smallBubble.recycle();
   }
   super.onDetachedFromWindow();
 }
예제 #30
0
  /** Mipmap texture */
  public static int buildMipmap(GL10 gl, Bitmap srcBitmap, boolean recycle) {
    Bitmap bitmap = srcBitmap;
    int level = 0;
    int height = bitmap.getHeight();
    int width = bitmap.getWidth();
    int textureID = genTexture(gl);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID);

    try {
      // この一文がないと、Lynxで崩れる
      ((GL11) gl).glTexParameteri(GL10.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL10.GL_TRUE);
    } catch (Exception e) {
      e.printStackTrace();
    }

    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(
        GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_LINEAR);

    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
    gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);

    while (height >= 1 && width >= 1) {
      // ---- note ----
      // First of all, generate the texture from our bitmap and set it to
      // the according level
      //  Lynx3Dではlevel 0 以外で呼び出したときにうまくいかないことがある。
      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, level, bitmap, 0);

      if (height == 1 || width == 1) {
        if (recycle || bitmap != srcBitmap) bitmap.recycle();
        break;
      }

      level++;

      height /= 2;
      width /= 2;

      Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, width, height, true);

      // Clean up
      if (recycle || bitmap != srcBitmap) bitmap.recycle();
      bitmap = bitmap2;
    }

    return textureID;
  }