public String saveImage(Uri image, int width, int height) { String imagePath = null; try { byte[] data = readFileThroughUri(image); BitmapFactory.Options options = new BitmapFactory.Options(); options.outWidth = height; options.outHeight = width; options.inSampleSize = 2; Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options); Bitmap scaledBmp = Bitmap.createScaledBitmap(bmp, width, height, false); bmp.recycle(); bmp = null; ByteArrayOutputStream bytes = new ByteArrayOutputStream(); scaledBmp.compress( Bitmap.CompressFormat.JPEG, (int) (100 * ImageFileManipulation.IAMGE_COMPRESSION_RATIO), bytes); imagePath = ImageFileManipulation.EXTERNAL_MEMORY + getFileName() + ".jpeg"; boolean isFileCreate = createFileInData(imagePath, bytes.toByteArray()); if (!isFileCreate) // if file is not created return null return null; } catch (Exception e) { e.printStackTrace(); } return imagePath; }
/** * 按指定图片大小返回压缩参数 * * @param pathName * @param maxWidth * @param maxHeight * @return */ public static BitmapFactory.Options createOptions(String pathName, int maxWidth, int maxHeight) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; // 当opts不为null时,但decodeFile返回空,不为图片分配内存,只获取图片的大小,并保存在opts的outWidth和outHeight BitmapFactory.decodeFile(pathName, opts); int srcWidth = opts.outWidth; int srcHeight = opts.outHeight; int destWidth = 0; int destHeight = 0; // 缩放的比例 double ratio = 0.0; // 按比例计算缩放后的图片大小,maxLength是长或宽允许的最大长度 if (srcWidth > srcHeight) { ratio = srcWidth / maxWidth; destWidth = maxWidth; destHeight = (int) (srcHeight / ratio); } else { ratio = srcHeight / maxHeight; destHeight = maxHeight; destWidth = (int) (srcWidth / ratio); } // 对图片进行压缩,是在读取的过程中进行压缩,而不是把图片读进了内存再进行压缩 BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 缩放的比例,缩放是很难按准备的比例进行缩放的,目前我只发现只能通过inSampleSize来进行缩放,其值表明缩放的倍数,SDK中建议其值是2的指数值 newOpts.inSampleSize = (int) ratio + 1; // inJustDecodeBounds设为false表示把图片读进内存中 newOpts.inJustDecodeBounds = false; // 设置大小,这个一般是不准确的,是以inSampleSize的为准,但是如果不设置却不能缩放 newOpts.outHeight = destHeight; newOpts.outWidth = destWidth; return newOpts; }
/** * コンストラクタ * * @param context */ public OverlayFigureView(Context context, String seqDir, int fgCount) { super(context); // ビットマップ展開オプションを設定 mOpt.inJustDecodeBounds = false; mOpt.inPurgeable = true; mOpt.inInputShareable = true; mOpt.outHeight = STAMP_HEIGHT; mOpt.outWidth = STAMP_WIDTH; mOpt.inSampleSize = 1; // OpenCV設定用 mCvGaussianSize = new Size(1, 1); setFocusable(true); // gestureを作成(設定はview内) mGListener = new MyGestureListener(this); mGesture = new GestureDetector(mGListener); mScaleListener = new MyScaleGestureListener(); scaleGesture = new ScaleGestureDetector(context, mScaleListener); // フォルダ設定 mDirectory = seqDir; // フィギュア枚数 mFgCount = fgCount; mContext = context; // ビットマップ作成 createFigureBitmap(); }
public static Bitmap getBitmapByStream(InputStream stream) { try { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; // 这里是整个方法的关键,inJustDecodeBounds设为true时将不为图片分配内存。 BitmapFactory.decodeStream(stream, null, opts); int srcWidth = opts.outWidth; // 获取图片的原始宽度 int srcHeight = opts.outHeight; // 获取图片原始高度 // 缩放的比例 // float ratio = srcWidth / srcHeight; float width, height; if (srcWidth > srcHeight) { width = 480; height = srcHeight * 480 / srcWidth; } else { height = 480; width = srcWidth * 480 / srcHeight; } BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 缩放的比例,缩放是很难按准备的比例进行缩放的,目前我只发现只能通过inSampleSize来进行缩放,其值表明缩放的倍数,SDK中建议其值是2的指数值 if (srcWidth <= 480) { newOpts.inSampleSize = 1; } if (srcWidth > 480 && srcWidth <= 960) { newOpts.inSampleSize = 2; } if (srcWidth > 960 && srcWidth <= 1920) { newOpts.inSampleSize = 4; } if (srcWidth > 1920) { newOpts.inSampleSize = 8; } // inJustDecodeBounds设为false表示把图片读进内存中 newOpts.inJustDecodeBounds = false; // 设置大小,这个一般是不准确的,是以inSampleSize的为准,但是如果不设置却不能缩放 newOpts.outHeight = (int) height; newOpts.outWidth = (int) width; return BitmapFactory.decodeStream(stream, null, newOpts); // 获取缩放后图片 /* * Bitmap srcBitmap= BitmapFactory.decodeFile(path); int * srcWidth=srcBitmap.getWidth(); QLLog.e("my_tag", * "width是:---->"+srcWidth); int srcHeight=srcBitmap.getHeight(); * QLLog.e("my_tag", "height是:---->"+srcHeight); float * ratio=srcWidth/srcHeight; float width=0,height=0; * if(srcWidth>srcHeight){ width=480; height=srcHeight*480/srcWidth; * }else{ height=480; width=srcWidth*480/srcHeight; } * QLLog.e("my_tag", "width是:---->"+width+"height====>"+height); * //Bitmap tarBitmap=Bitmap.createScaledBitmap(srcBitmap, * (int)width,(int) height, false); QLLog.e("my_tag", * tarBitmap==null?"是空的":"非空啦"); return tarBitmap; */ } catch (Exception e) { return BitmapFactory.decodeStream(stream); } }
public Bitmap decodeThumbFile(String path) { Bitmap bmp = null; Log.v("File Path", path); File f = new File(path); try { // Decode image size BitmapFactory.Options options = new BitmapFactory.Options(); options.inTempStorage = new byte[16 * 1024]; options.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(f), null, options); int IMAGE_MAX_SIZE = 640; int scale = 1; if (options.outHeight > IMAGE_MAX_SIZE || options.outWidth > IMAGE_MAX_SIZE) { scale = (int) Math.pow( 2, (int) Math.round( Math.log( IMAGE_MAX_SIZE / (double) Math.max(options.outHeight, options.outWidth)) / Math.log(0.5))); } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; o2.inPurgeable = true; o2.outHeight = 480; o2.outWidth = 640; Bitmap temp = BitmapFactory.decodeStream(new FileInputStream(f), null, o2); ByteArrayOutputStream baos = new ByteArrayOutputStream(); temp.compress( Bitmap.CompressFormat.PNG, (int) (100 * ImageFileManipulation.IAMGE_COMPRESSION_RATIO), baos); temp.recycle(); temp = null; options = new BitmapFactory.Options(); options.inDither = true; options.inPurgeable = true; options.inInputShareable = true; options.inSampleSize = scale; options.inTempStorage = new byte[32 * 1024]; bmp = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size(), options); } catch (Exception e) { e.printStackTrace(); } return bmp; }
@Override public void run() { int len = mFiles.size(); for (int i = 0; i < len; i++) { if (mStop) { mStop = false; mFiles = null; return; } final File file = new File(mDir + "/" + mFiles.get(i)); if (isImageFile(file.getName())) { long len_kb = file.length() / 1024; BitmapFactory.Options options = new BitmapFactory.Options(); options.outWidth = mWidth; options.outHeight = mHeight; if (len_kb > 1000 && len_kb < 5000) { options.inSampleSize = 32; options.inPurgeable = true; mThumb = new SoftReference<Bitmap>(BitmapFactory.decodeFile(file.getPath(), options)); } else if (len_kb >= 5000) { options.inSampleSize = 32; options.inPurgeable = true; mThumb = new SoftReference<Bitmap>(BitmapFactory.decodeFile(file.getPath(), options)); } else if (len_kb <= 1000) { options.inPurgeable = true; mThumb = new SoftReference<Bitmap>( Bitmap.createScaledBitmap( BitmapFactory.decodeFile(file.getPath()), mWidth, mHeight, false)); } mCacheMap.put(file.getPath(), mThumb.get()); mHandler.post( new Runnable() { @Override public void run() { Message msg = mHandler.obtainMessage(); msg.obj = (Bitmap) mThumb.get(); msg.sendToTarget(); } }); } } }
public static Bitmap getBitmaps(Context context, String fileName) { Log.i("DORIS", "getBitmaps"); final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(getPhotoPath(context) + fileName, options); if (options != null) { // scale image options.outWidth = 200; options.outHeight = 150; Log.i("DORIS", "check"); return PhotoUtils.scaleBitmap(options, getPhotoPath(context) + fileName); } return null; }
public Bitmap createBitmapFromUri(Uri uri) { Bitmap imageBitmap = null; try { ContentResolver contentResolver = context.getContentResolver(); InputStream inputStream = contentResolver.openInputStream(uri); BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inTempStorage = new byte[16 * 1024]; opt.inSampleSize = 1; opt.outWidth = 50; opt.outHeight = 40; imageBitmap = BitmapFactory.decodeStream(inputStream, new Rect(), null); } catch (FileNotFoundException e) { e.printStackTrace(); } return imageBitmap; }
public Bitmap createBitmap(String path, int w, int h) { try { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; // 这里是整个方法的关键,inJustDecodeBounds设为true时将不为图片分配内存。 BitmapFactory.decodeFile(path, opts); int srcWidth = opts.outWidth; // 获取图片的原始宽度 int srcHeight = opts.outHeight; // 获取图片原始高度 int destWidth = 0; int destHeight = 0; // 缩放的比例 double ratio = 0.0; if (srcWidth < w || srcHeight < h) { ratio = 0.0; destWidth = srcWidth; destHeight = srcHeight; } else if (srcWidth > srcHeight) { // 按比例计算缩放后的图片大小,maxLength是长或宽允许的最大长度 ratio = (double) srcWidth / w; destWidth = w; destHeight = (int) (srcHeight / ratio); } else { ratio = (double) srcHeight / h; destHeight = h; destWidth = (int) (srcWidth / ratio); } BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 缩放的比例,缩放是很难按准备的比例进行缩放的,目前我只发现只能通过inSampleSize来进行缩放,其值表明缩放的倍数,SDK中建议其值是2的指数值 newOpts.inSampleSize = (int) ratio + 1; // inJustDecodeBounds设为false表示把图片读进内存中 newOpts.inJustDecodeBounds = false; // 设置大小,这个一般是不准确的,是以inSampleSize的为准,但是如果不设置却不能缩放 newOpts.outHeight = destHeight; newOpts.outWidth = destWidth; // 获取缩放后图片 return BitmapFactory.decodeFile(path, newOpts); } catch (Exception e) { // TODO: handle exception return null; } }
@SuppressWarnings("NewApi") @Override public void run() { if (src_image == null) { if (uri == null) { ui_handler.post( new Runnable() { @Override public void run() { if (isFinishing()) return; Toast.makeText( SetWallpaperActivity.this, "missing uri in arguments.", Toast.LENGTH_SHORT) .show(); } }); return; } log.d("loading image.."); // Options for image size check BitmapFactory.Options check_option = new BitmapFactory.Options(); check_option.inJustDecodeBounds = true; check_option.inDensity = 0; check_option.inTargetDensity = 0; check_option.inDensity = 0; check_option.inScaled = false; // Option for image load BitmapFactory.Options load_option = new BitmapFactory.Options(); load_option.inPurgeable = true; load_option.inDensity = 0; load_option.inTargetDensity = 0; load_option.inDensity = 0; load_option.inScaled = false; // Color at the time of load depth int pixel_bytes; check_option.inPreferredConfig = Bitmap.Config.ARGB_8888; load_option.inPreferredConfig = Bitmap.Config.ARGB_8888; pixel_bytes = 4; ContentResolver cr = getContentResolver(); InputStream is; // Examine the image size try { is = cr.openInputStream(uri); try { check_option.outHeight = 0; check_option.outWidth = 0; BitmapFactory.decodeStream(is, null, check_option); } finally { is.close(); } } catch (Throwable ex) { // IOException // SecurityException: reading com.android.providers.telephony.MmsProvider ex.printStackTrace(); } if (check_option.outWidth < 1 || check_option.outHeight < 1) { log.e("load failed."); ui_handler.post( new Runnable() { @Override public void run() { if (isFinishing()) return; Toast.makeText(SetWallpaperActivity.this, "load failed.", Toast.LENGTH_SHORT) .show(); } }); return; } // To change the sample size, if necessary by examining the amount of data int data_size = check_option.outWidth * check_option.outHeight * pixel_bytes; // 面積と色深度 int limit_size = 1024 * 1024 * 10; int samplesize = 1; while (data_size / (float) (samplesize * samplesize) >= limit_size) { samplesize++; } load_option.inSampleSize = samplesize; // load bitmap try { is = cr.openInputStream(uri); try { src_image = BitmapFactory.decodeStream(is, null, load_option); } finally { is.close(); } } catch (IOException ex) { ex.printStackTrace(); } if (src_image == null) { log.e("load failed."); ui_handler.post( new Runnable() { @Override public void run() { if (isFinishing()) return; Toast.makeText(SetWallpaperActivity.this, "load failed.", Toast.LENGTH_SHORT) .show(); } }); return; } int row_bytes = src_image.getRowBytes(); int pixel_bytes2 = row_bytes / src_image.getWidth(); originalImageWidth = src_image.getWidth(); originalImageHeight = src_image.getHeight(); Point size = new Point(); WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); wm.getDefaultDisplay().getRealSize(size); float ratioOriginal = originalImageWidth / originalImageHeight; float ratioScreen = (float) (size.x) / (float) (size.y); imageDimensionCategory = originalImageWidth > originalImageHeight ? IMAGE_DIMENSION_HORIZONTAL : IMAGE_DIMENSION_VERTICAL; if (ratioOriginal > ratioScreen && ratioOriginal < 2 * ratioScreen) { imageDimensionCategory = IMAGE_DIMENSION_NOT_STANDARD; } Log.e("DIMENSION", imageDimensionCategory + ""); float temp = size.y; wall_real_w = opt_display_width; float scaleTemp = (temp / originalImageHeight); wall_real_h = (int) temp; opt_display_width = size.x; opt_display_height = size.y; // Log.e("COST", "" + temp + " " + scaleTemp + " " + wall_real_w + " " + // wall_real_h); Log.e( "COST", temp + " ORIGINAL " + originalImageWidth + " " + originalImageHeight + " REAL " + wall_real_w + " " + wall_real_h + " SCREEN " + opt_display_width + " " + opt_display_height); log.d( "wallpaper=%dx%d, display=%dx%d, overriding %dx%d", wall_real_w, wall_real_h, opt_display_width, opt_display_height, opt_output_width, opt_output_height); if (wall_real_w <= 0) wall_real_w = opt_display_width; if (wall_real_h <= 0) wall_real_h = opt_display_height; if (opt_output_width > 0) wall_real_w = opt_output_width; if (opt_output_height > 0) wall_real_h = opt_output_height; if (wall_real_w <= 0) wall_real_w = 1; if (wall_real_h <= 0) wall_real_h = 1; wall_w = wall_real_w; wall_h = wall_real_h; if (wall_w < 1) wall_w = 1; if (wall_h < 1) wall_h = 1; wall_image_aspect = (float) wall_w / (float) wall_h; log.d("wall_image=%dx%d", wall_w, wall_h); log.d( "original size=%dx%dx%d(%.2fMB), factor=%s,resized=%dx%dx%d(%.2fMB)", check_option.outWidth, check_option.outHeight, pixel_bytes, data_size / (float) (1024 * 1024), samplesize, src_image.getWidth(), src_image.getHeight(), pixel_bytes2, (src_image.getHeight() * row_bytes) / (float) (1024 * 1024)); } // レイアウトが完了するのを待つ while (!bCancelled) { if (flOuter.getWidth() > 0) break; waitEx(100); } if (bCancelled) return; // The size of the display frame int frame_w = flOuter.getWidth(); int frame_h = flOuter.getHeight(); float frame_aspect = frame_w / (float) frame_h; // The size of the data int src_w = src_image.getWidth(); int src_h = src_image.getHeight(); float src_aspect = src_w / (float) src_h; src_rect = new Rect(0, 0, src_w, src_h); // The size of the display image int shown_w; int shown_h; if (src_w <= frame_w && src_h <= frame_h) { // Scaling unnecessary shown_w = src_w; shown_h = src_h; } else if (src_aspect >= frame_aspect) { shown_w = frame_w; shown_h = (int) (0.5f + (src_h * frame_w) / (float) src_w); } else { // Image I fit in portrait. Vertical base than the display frame shown_h = frame_h; shown_w = (int) (0.5f + (src_w * frame_h) / (float) src_h); } // Position of the display image (display frame criteria) int x, y; x = (frame_w - shown_w) / 2; y = (frame_h - shown_h) / 2; shown_image_rect = new RectF(x, y, x + shown_w, y + shown_h); // Generate bitmap for display shown_image = Bitmap.createBitmap( frame_w, frame_h, MyApp.getBitmapConfig(src_image, Bitmap.Config.ARGB_8888)); paint = new Paint(); paint.setFilterBitmap(true); c = new Canvas(shown_image); cFrame = new Canvas(shown_image); c.drawBitmap(src_image, src_rect, shown_image_rect, paint); // Width and height of the selected range int selection_w; int selection_h; if (src_aspect <= wall_image_aspect) { // Image portrait than the ratio of wallpaper. It fits in right and left base selection_w = (int) shown_image_rect.width(); selection_h = (int) (0.5 + selection_w / wall_image_aspect); } else { // Image is long in the transverse than wallpaper. It fits in the vertical base selection_h = (int) shown_image_rect.height(); selection_w = (int) (0.5 + selection_h * wall_image_aspect); } x = (frame_w - selection_w) / 2; y = (frame_h - selection_h) / 2; prev_selection.set(x, y, x + selection_w, y + selection_h); ui_handler.post( new Runnable() { @Override public void run() { if (bCancelled) return; // 画像を表示 ivImage.setImageDrawable(new BitmapDrawable(getResources(), shown_image)); // Enable button // tbOverall.setEnabled(true); btnOk.setEnabled(true); bLoading = false; // Set selection setSelection( prev_selection.left, prev_selection.top, prev_selection.width(), prev_selection.height()); } }); }