Posts

Showing posts from January, 2018

Java/Android class to create blur on image

package com.example.saka.moviepreview; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.os.Build; import android.renderscript.Allocation; import android.renderscript.Element; import android.renderscript.RenderScript; import android.renderscript.ScriptIntrinsicBlur; import android.util.Log; import android.view.View; /** * Created by saka on 11/1/18. */ public class BlurEffect { //-------- blur effect -------- // Bitmap bm_default_background = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.default_background); //// int width = Math.round(bm_default_background.getWidth() * 0.2f); int height = Math.round(bm_default_background.getHeight() * 0.2f); //// bm_default_background = Bitmap.createScaledBitmap(bm_default_background, width, height, false); // Bitmap ...

Android Code to create a thread run network connection get bitmap from url

new Thread(new Runnable() { @Override public void run() { // Do network action in this function try { String baseUrl = "http://image.tmdb.org/t/p/w300"; String backdropPathPath = null; try { backdropPathPath = movie.getString("poster_path"); } catch (JSONException e) { e.printStackTrace(); } final String fullImgBackDropUrl = baseUrl + backdropPathPath; URL url2 = new URL(fullImgBackDropUrl); HttpURLConnection connection = (HttpURLConnection) url2.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap myBitmap = Bi...

Android/Java code to calculate the average color of an image

private int calculateAverageColor (android.graphics.Bitmap bitmap1, int pixelSpacing) { int R = 0; int G = 0; int B = 0; int n = 0; int res = 1; if (bitmap1 != null) { int height = bitmap1.getHeight(); int width = bitmap1.getWidth(); int[] pixels = new int[width * height]; bitmap1.getPixels(pixels, 0, width, 0, 0, width, height); for (int i = 0; i < pixels.length; i += pixelSpacing) { int color = pixels[i]; R += Color.red(color); G += Color.green(color); B += Color.blue(color); n++; } res = Color.rgb(R / n, G / n, B / n); } return res; }