List of usage examples for android.graphics Bitmap createScaledBitmap
public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight, boolean filter)
From source file:de.uulm.graphicalpasswords.openmiba.MIBALoginActivity.java
public void changeBackground(int imageindex) { int img = 0;/*from ww w. ja v a 2 s.c om*/ if (imageindex == -1) { img = R.drawable.ccp000; } else img = R.drawable.ccp001 + imageindex; Bitmap bmp = BitmapFactory.decodeResource(getResources(), img); bmp = Bitmap.createScaledBitmap(bmp, width, height, true); Drawable d = new BitmapDrawable(getResources(), bmp); bmp = null; // prevent outofmemory tableLayout.setBackgroundDrawable(d); }
From source file:com.geekandroid.sdk.pay.utils.Util.java
public static Bitmap extractThumbNail(final String path, final int height, final int width, final boolean crop) { Assert.assertTrue(path != null && !path.equals("") && height > 0 && width > 0); BitmapFactory.Options options = new BitmapFactory.Options(); try {/*from w ww . jav a2s .c om*/ options.inJustDecodeBounds = true; Bitmap tmp = BitmapFactory.decodeFile(path, options); if (tmp != null) { tmp.recycle(); tmp = null; } Log.d(TAG, "extractThumbNail: round=" + width + "x" + height + ", crop=" + crop); final double beY = options.outHeight * 1.0 / height; final double beX = options.outWidth * 1.0 / width; Log.d(TAG, "extractThumbNail: extract beX = " + beX + ", beY = " + beY); options.inSampleSize = (int) (crop ? (beY > beX ? beX : beY) : (beY < beX ? beX : beY)); if (options.inSampleSize <= 1) { options.inSampleSize = 1; } // NOTE: out of memory error while (options.outHeight * options.outWidth / options.inSampleSize > MAX_DECODE_PICTURE_SIZE) { options.inSampleSize++; } int newHeight = height; int newWidth = width; if (crop) { if (beY > beX) { newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth); } else { newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight); } } else { if (beY < beX) { newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth); } else { newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight); } } options.inJustDecodeBounds = false; Log.i(TAG, "bitmap required size=" + newWidth + "x" + newHeight + ", orig=" + options.outWidth + "x" + options.outHeight + ", sample=" + options.inSampleSize); Bitmap bm = BitmapFactory.decodeFile(path, options); if (bm == null) { Log.e(TAG, "bitmap decode failed"); return null; } Log.i(TAG, "bitmap decoded size=" + bm.getWidth() + "x" + bm.getHeight()); final Bitmap scale = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true); if (scale != null) { bm.recycle(); bm = scale; } if (crop) { final Bitmap cropped = Bitmap.createBitmap(bm, (bm.getWidth() - width) >> 1, (bm.getHeight() - height) >> 1, width, height); if (cropped == null) { return bm; } bm.recycle(); bm = cropped; Log.i(TAG, "bitmap croped size=" + bm.getWidth() + "x" + bm.getHeight()); } return bm; } catch (final OutOfMemoryError e) { Log.e(TAG, "decode bitmap failed: " + e.getMessage()); options = null; } return null; }
From source file:com.nttec.everychan.cache.BitmapCache.java
/** * ? ? /*from w ww .j av a 2 s . c o m*/ * @param hash ? ( ? ) * @param url ? URL (? ? ) * @param maxSize ? ??, , 0, ? ?? ? ? * @param chan ? * @param task ?? * @return Bitmap ? , null, ? ? */ public Bitmap download(String hash, String url, int maxSize, ChanModule chan, CancellableTask task) { if (hash == null) { Logger.e(TAG, "received null hash; url: " + url); return null; } synchronized (currentDownloads) { while (currentDownloads.contains(hash)) { try { currentDownloads.wait(); } catch (Exception e) { Logger.e(TAG, e); } } currentDownloads.add(hash); } try { Bitmap bmp = getFromCache(hash); if (bmp != null) return bmp; try { class BufOutputStream extends ByteArrayOutputStream { public BufOutputStream() { super(1024); } public InputStream toInputStream() { return new ByteArrayInputStream(buf, 0, count); } } BufOutputStream data = new BufOutputStream(); chan.downloadFile(url, data, null, task); bmp = BitmapFactory.decodeStream(data.toInputStream()); } catch (Exception e) { Logger.e(TAG, e); if (url.startsWith("data:image")) { try { byte[] data = Base64.decode(url.substring(url.indexOf("base64,") + 7), Base64.DEFAULT); bmp = BitmapFactory.decodeByteArray(data, 0, data.length); } catch (Exception e1) { Logger.e(TAG, e1); } } } if (bmp == null || (task != null && task.isCancelled())) { return null; } if (maxSize > 0) { // double scale = (double) maxSize / Math.max(bmp.getWidth(), bmp.getHeight()); if (scale < 1.0) { int width = (int) (bmp.getWidth() * scale); int height = (int) (bmp.getHeight() * scale); if (Math.min(width, height) > 0) { Bitmap scaled = Bitmap.createScaledBitmap(bmp, width, height, true); bmp.recycle(); bmp = scaled; } } } OutputStream fileStream = null; File file = null; boolean success = true; try { lru.put(hash, bmp); file = fileCache.create(FileCache.PREFIX_BITMAPS + hash); fileStream = new FileOutputStream(file); if (!bmp.compress(Bitmap.CompressFormat.PNG, 100, fileStream)) { throw new Exception(); } fileStream.close(); fileCache.put(file); fileStream = null; } catch (Exception e) { success = false; Logger.e(TAG, e); } finally { IOUtils.closeQuietly(fileStream); if (!success && file != null) fileCache.abort(file); } return bmp; } catch (OutOfMemoryError oom) { MainApplication.freeMemory(); Logger.e(TAG, oom); return null; } finally { synchronized (currentDownloads) { currentDownloads.remove(hash); currentDownloads.notifyAll(); } } }
From source file:com.javielinux.utils.Utils.java
static public String createIconForSearch(Bitmap bmp) { int count = 1; String tokenFile = "search_1"; String file = Utils.appDirectory + tokenFile + ".png"; File f = new File(file); while (f.exists()) { count++;/*from ww w . j av a2 s . c om*/ tokenFile = "search_" + count; file = Utils.appDirectory + tokenFile + ".png"; f = new File(file); } Bitmap avatarBig = Bitmap.createScaledBitmap(bmp, Utils.HEIGHT_SEARCH_ICON, Utils.HEIGHT_SEARCH_ICON, true); Bitmap avatarSmall = Bitmap.createScaledBitmap(bmp, Utils.HEIGHT_SEARCH_ICON_SMALL, Utils.HEIGHT_SEARCH_ICON_SMALL, true); FileOutputStream out; try { out = new FileOutputStream(file); avatarBig.compress(Bitmap.CompressFormat.PNG, 90, out); String fileSmall = Utils.appDirectory + tokenFile + "_small.png"; FileOutputStream outSmall = new FileOutputStream(fileSmall); avatarSmall.compress(Bitmap.CompressFormat.PNG, 90, outSmall); } catch (FileNotFoundException e) { e.printStackTrace(); } return tokenFile; }
From source file:com.slownet5.pgprootexplorer.filemanager.ui.TabsFragmentOne.java
public static Bitmap blur(Context context, Bitmap image) { int width = Math.round(image.getWidth() * BITMAP_SCALE); int height = Math.round(image.getHeight() * BITMAP_SCALE); Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false); Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap); RenderScript rs = RenderScript.create(context); ScriptIntrinsicBlur theIntrinsic = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap); Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap); theIntrinsic.setRadius(BLUR_RADIUS); theIntrinsic.setInput(tmpIn);//ww w. j ava2 s . c om theIntrinsic.forEach(tmpOut); tmpOut.copyTo(outputBitmap); } else { outputBitmap = BlurHelper.doBlur(image, (int) BLUR_RADIUS, false); } return outputBitmap; }
From source file:eu.faircode.netguard.AdapterLog.java
@Override public void bindView(final View view, final Context context, final Cursor cursor) { // Get values long time = cursor.getLong(colTime); int version = (cursor.isNull(colVersion) ? -1 : cursor.getInt(colVersion)); int protocol = (cursor.isNull(colProtocol) ? -1 : cursor.getInt(colProtocol)); String flags = cursor.getString(colFlags); String saddr = cursor.getString(colSAddr); int sport = (cursor.isNull(colSPort) ? -1 : cursor.getInt(colSPort)); String daddr = cursor.getString(colDAddr); int dport = (cursor.isNull(colDPort) ? -1 : cursor.getInt(colDPort)); String dname = (cursor.isNull(colDName) ? null : cursor.getString(colDName)); int uid = (cursor.isNull(colUid) ? -1 : cursor.getInt(colUid)); String data = cursor.getString(colData); int allowed = (cursor.isNull(colAllowed) ? -1 : cursor.getInt(colAllowed)); int connection = (cursor.isNull(colConnection) ? -1 : cursor.getInt(colConnection)); int interactive = (cursor.isNull(colInteractive) ? -1 : cursor.getInt(colInteractive)); // Get views//w w w . jav a 2 s . c o m TextView tvTime = view.findViewById(R.id.tvTime); TextView tvProtocol = view.findViewById(R.id.tvProtocol); TextView tvFlags = view.findViewById(R.id.tvFlags); TextView tvSAddr = view.findViewById(R.id.tvSAddr); TextView tvSPort = view.findViewById(R.id.tvSPort); final TextView tvDaddr = view.findViewById(R.id.tvDAddr); TextView tvDPort = view.findViewById(R.id.tvDPort); final TextView tvOrganization = view.findViewById(R.id.tvOrganization); final ImageView ivIcon = view.findViewById(R.id.ivIcon); TextView tvUid = view.findViewById(R.id.tvUid); TextView tvData = view.findViewById(R.id.tvData); ImageView ivConnection = view.findViewById(R.id.ivConnection); ImageView ivInteractive = view.findViewById(R.id.ivInteractive); // Show time tvTime.setText(new SimpleDateFormat("HH:mm:ss").format(time)); // Show connection type if (connection <= 0) ivConnection.setImageResource(allowed > 0 ? R.drawable.host_allowed : R.drawable.host_blocked); else { if (allowed > 0) ivConnection.setImageResource(connection == 1 ? R.drawable.wifi_on : R.drawable.other_on); else ivConnection.setImageResource(connection == 1 ? R.drawable.wifi_off : R.drawable.other_off); } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(ivConnection.getDrawable()); DrawableCompat.setTint(wrap, allowed > 0 ? colorOn : colorOff); } // Show if screen on if (interactive <= 0) ivInteractive.setImageDrawable(null); else { ivInteractive.setImageResource(R.drawable.screen_on); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(ivInteractive.getDrawable()); DrawableCompat.setTint(wrap, colorOn); } } // Show protocol name tvProtocol.setText(Util.getProtocolName(protocol, version, false)); // SHow TCP flags tvFlags.setText(flags); tvFlags.setVisibility(TextUtils.isEmpty(flags) ? View.GONE : View.VISIBLE); // Show source and destination port if (protocol == 6 || protocol == 17) { tvSPort.setText(sport < 0 ? "" : getKnownPort(sport)); tvDPort.setText(dport < 0 ? "" : getKnownPort(dport)); } else { tvSPort.setText(sport < 0 ? "" : Integer.toString(sport)); tvDPort.setText(dport < 0 ? "" : Integer.toString(dport)); } // Application icon ApplicationInfo info = null; PackageManager pm = context.getPackageManager(); String[] pkg = pm.getPackagesForUid(uid); if (pkg != null && pkg.length > 0) try { info = pm.getApplicationInfo(pkg[0], 0); } catch (PackageManager.NameNotFoundException ignored) { } if (info == null) ivIcon.setImageDrawable(null); else { if (info.icon <= 0) ivIcon.setImageResource(android.R.drawable.sym_def_app_icon); else { ivIcon.setHasTransientState(true); final ApplicationInfo finalInfo = info; executor.submit(new Runnable() { @Override public void run() { try { Drawable drawable = context.getPackageManager() .getApplicationIcon(finalInfo.packageName); final Drawable scaledDrawable; if (drawable instanceof BitmapDrawable) { Bitmap original = ((BitmapDrawable) drawable).getBitmap(); Bitmap scaled = Bitmap.createScaledBitmap(original, iconSize, iconSize, false); scaledDrawable = new BitmapDrawable(context.getResources(), scaled); } else scaledDrawable = drawable; new Handler(context.getMainLooper()).post(new Runnable() { @Override public void run() { ivIcon.setImageDrawable(scaledDrawable); ivIcon.setHasTransientState(false); } }); } catch (Throwable ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); new Handler(context.getMainLooper()).post(new Runnable() { @Override public void run() { ivIcon.setImageDrawable(null); ivIcon.setHasTransientState(false); } }); } } }); } } boolean we = (android.os.Process.myUid() == uid); // https://android.googlesource.com/platform/system/core/+/master/include/private/android_filesystem_config.h uid = uid % 100000; // strip off user ID if (uid == -1) tvUid.setText(""); else if (uid == 0) tvUid.setText(context.getString(R.string.title_root)); else if (uid == 9999) tvUid.setText("-"); // nobody else tvUid.setText(Integer.toString(uid)); // Show source address tvSAddr.setText(getKnownAddress(saddr)); // Show destination address if (!we && resolve && !isKnownAddress(daddr)) if (dname == null) { tvDaddr.setText(daddr); new AsyncTask<String, Object, String>() { @Override protected void onPreExecute() { ViewCompat.setHasTransientState(tvDaddr, true); } @Override protected String doInBackground(String... args) { try { return InetAddress.getByName(args[0]).getHostName(); } catch (UnknownHostException ignored) { return args[0]; } } @Override protected void onPostExecute(String name) { tvDaddr.setText(">" + name); ViewCompat.setHasTransientState(tvDaddr, false); } }.execute(daddr); } else tvDaddr.setText(dname); else tvDaddr.setText(getKnownAddress(daddr)); // Show organization tvOrganization.setVisibility(View.GONE); if (!we && organization) { if (!isKnownAddress(daddr)) new AsyncTask<String, Object, String>() { @Override protected void onPreExecute() { ViewCompat.setHasTransientState(tvOrganization, true); } @Override protected String doInBackground(String... args) { try { return Util.getOrganization(args[0]); } catch (Throwable ex) { Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); return null; } } @Override protected void onPostExecute(String organization) { if (organization != null) { tvOrganization.setText(organization); tvOrganization.setVisibility(View.VISIBLE); } ViewCompat.setHasTransientState(tvOrganization, false); } }.execute(daddr); } // Show extra data if (TextUtils.isEmpty(data)) { tvData.setText(""); tvData.setVisibility(View.GONE); } else { tvData.setText(data); tvData.setVisibility(View.VISIBLE); } }
From source file:com.nostra13.universalimageloader.core.decode.ContentImageDecoder.java
private static Bitmap scaleBitmap(Bitmap origBitmap, int width, int height) { float scale = Math.min(((float) width) / ((float) origBitmap.getWidth()), ((float) height) / ((float) origBitmap.getHeight())); return Bitmap.createScaledBitmap(origBitmap, (int) (((float) origBitmap.getWidth()) * scale), (int) (((float) origBitmap.getHeight()) * scale), false); }
From source file:net.pmarks.chromadoze.ChromaDoze.java
private Drawable getScaledImage(int resource, int size) { Bitmap b = ((BitmapDrawable) ContextCompat.getDrawable(this, resource)).getBitmap(); Bitmap bitmapResized = Bitmap.createScaledBitmap(b, size, size, true); return new BitmapDrawable(getResources(), bitmapResized); }
From source file:com.lcl6.cn.imagepickerl.AndroidImagePicker.java
public static Bitmap makeCropBitmap(Bitmap bitmap, Rect rectBox, RectF imageMatrixRect, int expectSize) { Bitmap bmp = bitmap;//ww w . j a v a 2s .c om RectF localRectF = imageMatrixRect; float f = localRectF.width() / bmp.getWidth(); int left = (int) ((rectBox.left - localRectF.left) / f); int top = (int) ((rectBox.top - localRectF.top) / f); int width = (int) (rectBox.width() / f); int height = (int) (rectBox.height() / f); if (left < 0) { left = 0; } if (top < 0) { top = 0; } if (left + width > bmp.getWidth()) { width = bmp.getWidth() - left; } if (top + height > bmp.getHeight()) { height = bmp.getHeight() - top; } int k = width; if (width < expectSize) { k = expectSize; } if (width > expectSize) { k = expectSize; } try { bmp = Bitmap.createBitmap(bmp, left, top, width, height); if (k != width && k != height) {//don't do this if equals bmp = Bitmap.createScaledBitmap(bmp, k, k, true);//scale the bitmap } } catch (OutOfMemoryError localOutOfMemoryError1) { Log.v(TAG, "OOM when create bitmap"); } return bmp; }