Search in sources :

Example 6 with Bitmap

use of android.graphics.Bitmap in project Launcher3 by chislon.

the class WidgetPreviewLoader method getPreview.

public Bitmap getPreview(final Object o) {
    final String name = getObjectName(o);
    final String packageName = getObjectPackage(o);
    // check if the package is valid
    boolean packageValid = true;
    synchronized (sInvalidPackages) {
        packageValid = !sInvalidPackages.contains(packageName);
    }
    if (!packageValid) {
        return null;
    }
    if (packageValid) {
        synchronized (mLoadedPreviews) {
            // check if it exists in our existing cache
            if (mLoadedPreviews.containsKey(name) && mLoadedPreviews.get(name).get() != null) {
                return mLoadedPreviews.get(name).get();
            }
        }
    }
    Bitmap unusedBitmap = null;
    synchronized (mUnusedBitmaps) {
        // not in cache; we need to load it from the db
        while ((unusedBitmap == null || !unusedBitmap.isMutable() || unusedBitmap.getWidth() != mPreviewBitmapWidth || unusedBitmap.getHeight() != mPreviewBitmapHeight) && mUnusedBitmaps.size() > 0) {
            unusedBitmap = mUnusedBitmaps.remove(0).get();
        }
        if (unusedBitmap != null) {
            final Canvas c = mCachedAppWidgetPreviewCanvas.get();
            c.setBitmap(unusedBitmap);
            c.drawColor(0, PorterDuff.Mode.CLEAR);
            c.setBitmap(null);
        }
    }
    if (unusedBitmap == null) {
        unusedBitmap = Bitmap.createBitmap(mPreviewBitmapWidth, mPreviewBitmapHeight, Bitmap.Config.ARGB_8888);
    }
    Bitmap preview = null;
    if (packageValid) {
        preview = readFromDb(name, unusedBitmap);
    }
    if (preview != null) {
        synchronized (mLoadedPreviews) {
            mLoadedPreviews.put(name, new WeakReference<Bitmap>(preview));
        }
        return preview;
    } else {
        // it's not in the db... we need to generate it
        final Bitmap generatedPreview = generatePreview(o, unusedBitmap);
        preview = generatedPreview;
        if (preview != unusedBitmap) {
            throw new RuntimeException("generatePreview is not recycling the bitmap " + o);
        }
        synchronized (mLoadedPreviews) {
            mLoadedPreviews.put(name, new WeakReference<Bitmap>(preview));
        }
        // write to db on a thread pool... this can be done lazily and improves the performance
        // of the first time widget previews are loaded
        new AsyncTask<Void, Void, Void>() {

            public Void doInBackground(Void... args) {
                writeToDb(o, generatedPreview);
                return null;
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
        return preview;
    }
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas)

Example 7 with Bitmap

use of android.graphics.Bitmap in project Launcher3 by chislon.

the class SavedWallpaperImages method loadThumbnailsAndImageIdList.

public void loadThumbnailsAndImageIdList() {
    mImages = new ArrayList<SavedWallpaperTile>();
    SQLiteDatabase db = mDb.getReadableDatabase();
    Cursor result = db.query(ImageDb.TABLE_NAME, new String[] { ImageDb.COLUMN_ID, // cols to return
    ImageDb.COLUMN_IMAGE_THUMBNAIL_FILENAME }, // select query
    null, // args to select query
    null, null, null, ImageDb.COLUMN_ID + " DESC", null);
    while (result.moveToNext()) {
        String filename = result.getString(1);
        File file = new File(mContext.getFilesDir(), filename);
        Bitmap thumb = BitmapFactory.decodeFile(file.getAbsolutePath());
        if (thumb != null) {
            mImages.add(new SavedWallpaperTile(result.getInt(0), new BitmapDrawable(thumb)));
        }
    }
    result.close();
}
Also used : Bitmap(android.graphics.Bitmap) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Cursor(android.database.Cursor) File(java.io.File)

Example 8 with Bitmap

use of android.graphics.Bitmap in project TakePhoto by crazycodeboy.

the class TakePhotoImpl method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case TConstant.RC_PICK_PICTURE_FROM_GALLERY_CROP:
            if (resultCode == Activity.RESULT_OK && data != null) {
                //从相册选择照片并裁剪
                try {
                    onCrop(data.getData(), outPutUri, cropOptions);
                } catch (TException e) {
                    takeResult(TResult.of(TImage.of(outPutUri, fromType)), e.getDetailMessage());
                    e.printStackTrace();
                }
            } else {
                listener.takeCancel();
            }
            break;
        case //从相册选择照片不裁剪
        TConstant.RC_PICK_PICTURE_FROM_GALLERY_ORIGINAL:
            if (resultCode == Activity.RESULT_OK) {
                try {
                    takeResult(TResult.of(TImage.of(TUriParse.getFilePathWithUri(data.getData(), contextWrap.getActivity()), fromType)));
                } catch (TException e) {
                    takeResult(TResult.of(TImage.of(data.getData(), fromType)), e.getDetailMessage());
                    e.printStackTrace();
                }
            } else {
                listener.takeCancel();
            }
            break;
        case //从文件选择照片不裁剪
        TConstant.RC_PICK_PICTURE_FROM_DOCUMENTS_ORIGINAL:
            if (resultCode == Activity.RESULT_OK) {
                try {
                    takeResult(TResult.of(TImage.of(TUriParse.getFilePathWithDocumentsUri(data.getData(), contextWrap.getActivity()), fromType)));
                } catch (TException e) {
                    takeResult(TResult.of(TImage.of(outPutUri, fromType)), e.getDetailMessage());
                    e.printStackTrace();
                }
            } else {
                listener.takeCancel();
            }
            break;
        case //从文件选择照片,并裁剪
        TConstant.RC_PICK_PICTURE_FROM_DOCUMENTS_CROP:
            if (resultCode == Activity.RESULT_OK) {
                try {
                    onCrop(data.getData(), outPutUri, cropOptions);
                } catch (TException e) {
                    takeResult(TResult.of(TImage.of(outPutUri, fromType)), e.getDetailMessage());
                    e.printStackTrace();
                }
            } else {
                listener.takeCancel();
            }
            break;
        case //拍取照片,并裁剪
        TConstant.RC_PICK_PICTURE_FROM_CAPTURE_CROP:
            if (resultCode == Activity.RESULT_OK) {
                if (takePhotoOptions != null && takePhotoOptions.isCorrectImage())
                    ImageRotateUtil.of().correctImage(contextWrap.getActivity(), tempUri);
                try {
                    onCrop(tempUri, Uri.fromFile(new File(TUriParse.parseOwnUri(contextWrap.getActivity(), outPutUri))), cropOptions);
                } catch (TException e) {
                    takeResult(TResult.of(TImage.of(outPutUri, fromType)), e.getDetailMessage());
                    e.printStackTrace();
                }
            } else {
                listener.takeCancel();
            }
            break;
        case //拍取照片
        TConstant.RC_PICK_PICTURE_FROM_CAPTURE:
            if (resultCode == Activity.RESULT_OK) {
                if (takePhotoOptions != null && takePhotoOptions.isCorrectImage())
                    ImageRotateUtil.of().correctImage(contextWrap.getActivity(), outPutUri);
                try {
                    takeResult(TResult.of(TImage.of(TUriParse.getFilePathWithUri(outPutUri, contextWrap.getActivity()), fromType)));
                } catch (TException e) {
                    takeResult(TResult.of(TImage.of(outPutUri, fromType)), e.getDetailMessage());
                    e.printStackTrace();
                }
            } else {
                listener.takeCancel();
            }
            break;
        //裁剪照片返回结果
        case TConstant.RC_CROP:
        case //裁剪照片返回结果
        Crop.REQUEST_CROP:
            if (resultCode == Activity.RESULT_OK) {
                if (multipleCrop != null) {
                    cropContinue(true);
                } else {
                    try {
                        TImage image = TImage.of(TUriParse.getFilePathWithUri(outPutUri, contextWrap.getActivity()), fromType);
                        image.setCropped(true);
                        takeResult(TResult.of(image));
                    } catch (TException e) {
                        takeResult(TResult.of(TImage.of(outPutUri.getPath(), fromType)), e.getDetailMessage());
                        e.printStackTrace();
                    }
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                //裁剪的照片没有保存
                if (multipleCrop != null) {
                    if (data != null) {
                        //获取裁剪的结果数据
                        Bitmap bitmap = data.getParcelableExtra("data");
                        //将裁剪的结果写入到文件
                        TImageFiles.writeToFile(bitmap, outPutUri);
                        cropContinue(true);
                    } else {
                        cropContinue(false);
                    }
                } else {
                    if (data != null) {
                        //获取裁剪的结果数据
                        Bitmap bitmap = data.getParcelableExtra("data");
                        //将裁剪的结果写入到文件
                        TImageFiles.writeToFile(bitmap, outPutUri);
                        TImage image = TImage.of(outPutUri.getPath(), fromType);
                        image.setCropped(true);
                        takeResult(TResult.of(image));
                    } else {
                        listener.takeCancel();
                    }
                }
            } else {
                if (multipleCrop != null) {
                    cropContinue(false);
                } else {
                    listener.takeCancel();
                }
            }
            break;
        case //多选图片返回结果
        TConstant.RC_PICK_MULTIPLE:
            if (resultCode == Activity.RESULT_OK && data != null) {
                ArrayList<Image> images = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
                if (cropOptions != null) {
                    try {
                        onCrop(MultipleCrop.of(TUtils.convertImageToUri(contextWrap.getActivity(), images), contextWrap.getActivity(), fromType), cropOptions);
                    } catch (TException e) {
                        cropContinue(false);
                        e.printStackTrace();
                    }
                } else {
                    takeResult(TResult.of(TUtils.getTImagesWithImages(images, fromType)));
                }
            } else {
                listener.takeCancel();
            }
            break;
        default:
            break;
    }
}
Also used : TException(com.jph.takephoto.model.TException) Bitmap(android.graphics.Bitmap) TImage(com.jph.takephoto.model.TImage) TImage(com.jph.takephoto.model.TImage) CompressImage(com.jph.takephoto.compress.CompressImage) Image(com.darsh.multipleimageselect.models.Image) File(java.io.File)

Example 9 with Bitmap

use of android.graphics.Bitmap in project TakePhoto by crazycodeboy.

the class CompressImageUtil method compressImageByPixel.

/**
	 * 按比例缩小图片的像素以达到压缩的目的
	 * @author JPH
	 * @param imgPath
	 * @return 
	 * @date 2014-12-5下午11:30:59
	 */
private void compressImageByPixel(String imgPath, CompressListener listener) throws FileNotFoundException {
    if (imgPath == null) {
        sendMsg(false, imgPath, "要压缩的文件不存在", listener);
        return;
    }
    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    //只读边,不读内容
    newOpts.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imgPath, newOpts);
    newOpts.inJustDecodeBounds = false;
    int width = newOpts.outWidth;
    int height = newOpts.outHeight;
    float maxSize = config.getMaxPixel();
    int be = 1;
    if (width >= height && width > maxSize) {
        //缩放比,用高或者宽其中较大的一个数据进行计算
        be = (int) (newOpts.outWidth / maxSize);
        be++;
    } else if (width < height && height > maxSize) {
        be = (int) (newOpts.outHeight / maxSize);
        be++;
    }
    //设置采样率
    newOpts.inSampleSize = be;
    //该模式是默认的,可不设
    newOpts.inPreferredConfig = Config.ARGB_8888;
    // 同时设置才会有效
    newOpts.inPurgeable = true;
    //。当系统内存不够时候图片自动被回收
    newOpts.inInputShareable = true;
    Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
    if (config.isEnableQualityCompress()) {
        //压缩好比例大小后再进行质量压缩
        compressImageByQuality(bitmap, imgPath, listener);
    } else {
        File thumbnailFile = getThumbnailFile(new File(imgPath));
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(thumbnailFile));
        listener.onCompressSuccess(thumbnailFile.getPath());
    }
}
Also used : Bitmap(android.graphics.Bitmap) FileOutputStream(java.io.FileOutputStream) BitmapFactory(android.graphics.BitmapFactory) File(java.io.File)

Example 10 with Bitmap

use of android.graphics.Bitmap in project TakePhoto by crazycodeboy.

the class ImageRotateUtil method rotateBitmapByDegree.

/**
     * 将图片按照某个角度进行旋转
     *
     * @param bm     需要旋转的图片
     * @param degree 旋转角度
     * @return 旋转后的图片
     */
private Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
    Bitmap returnBm = null;
    // 根据旋转角度,生成旋转矩阵
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);
    try {
        // 将原始图片按照旋转矩阵进行旋转,并得到新的图片
        returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
    } catch (OutOfMemoryError e) {
    }
    if (returnBm == null) {
        returnBm = bm;
    }
    if (bm != returnBm) {
        bm.recycle();
    }
    return returnBm;
}
Also used : Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix)

Aggregations

Bitmap (android.graphics.Bitmap)3662 Canvas (android.graphics.Canvas)875 Paint (android.graphics.Paint)697 BitmapDrawable (android.graphics.drawable.BitmapDrawable)447 IOException (java.io.IOException)384 Rect (android.graphics.Rect)338 Test (org.junit.Test)255 File (java.io.File)253 Matrix (android.graphics.Matrix)250 Drawable (android.graphics.drawable.Drawable)241 BitmapFactory (android.graphics.BitmapFactory)236 View (android.view.View)220 ImageView (android.widget.ImageView)205 FileOutputStream (java.io.FileOutputStream)182 Intent (android.content.Intent)177 InputStream (java.io.InputStream)165 FileNotFoundException (java.io.FileNotFoundException)150 RectF (android.graphics.RectF)148 Point (android.graphics.Point)146 Uri (android.net.Uri)117