1package com.android.rs.refocus; 2 3import android.app.Activity; 4import android.content.ContentResolver; 5import android.content.Context; 6import android.content.Intent; 7import android.graphics.Bitmap; 8import android.net.Uri; 9import android.os.AsyncTask; 10import android.os.Environment; 11import android.os.Bundle; 12import android.support.v8.renderscript.RenderScript; 13import android.util.Log; 14import android.widget.ImageView; 15import android.widget.TextView; 16 17import java.io.File; 18import java.io.IOException; 19 20public class MainActivity extends Activity { 21 private static final int RS_API = 19; 22 private static final String TAG = "MainActivity"; 23 24 ImageView mImgView; 25 26 ImageView mNewImgView; 27 TextView mCompareTextView; 28 TextView mPointerLabelTextView; 29 TextView mAllocLabelTextView; 30 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 mImgView = (ImageView) findViewById(R.id.image_view); 36 mNewImgView = (ImageView) findViewById(R.id.image_view_new); 37 mCompareTextView = (TextView) findViewById(R.id.compareTextView); 38 mPointerLabelTextView = (TextView) findViewById(R.id.orignialImageLabel); 39 mAllocLabelTextView = (TextView) findViewById(R.id.newImageLabel); 40 41 Intent intent = getIntent(); 42 if (intent != null) { 43 44 String s = intent.getType(); 45 if (s != null && s.indexOf("image/") != -1) { 46 Uri data = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); 47 if (data != null) { 48 49 try { 50 RenderScript renderScript = RenderScript.create(getApplicationContext(), RS_API); 51 renderScript.setPriority(RenderScript.Priority.NORMAL); 52 53 // Get input uri to RGBZ 54 RGBZ current_rgbz = new RGBZ(data, getContentResolver(), this); 55 DepthOfFieldOptions current_depth_options = new DepthOfFieldOptions(current_rgbz); 56 57 // Set image focus settings 58 current_depth_options.setFocusPoint(0.7f, 0.5f); 59 current_depth_options.setBokeh(2f); 60 61 RsTaskParams rsTaskParam = new RsTaskParams(renderScript, current_depth_options); 62 new RsAsyncTaskRunner().execute(rsTaskParam); 63 return; 64 } catch (IOException e) { 65 e.printStackTrace(); 66 } 67 68 } 69 } 70 } 71 72 73 try { 74 //Uri data = getLocalRef(); 75 Uri data = getResourceRef(); 76 if (data == null) { 77 return; 78 } 79 80 RenderScript renderScript = RenderScript.create(getApplicationContext(), RS_API); 81 renderScript.setPriority(RenderScript.Priority.NORMAL); 82 83 // Get input uri to RGBZ 84 RGBZ current_rgbz = new RGBZ(data, getContentResolver(), this); 85 DepthOfFieldOptions current_depth_options = new DepthOfFieldOptions(current_rgbz); 86 87 // Set image focus settings 88 current_depth_options.setFocusPoint(0.7f, 0.5f); 89 current_depth_options.setBokeh(2f); 90 91 RsTaskParams rsTaskParam = new RsTaskParams(renderScript, current_depth_options); 92 new RsAsyncTaskRunner().execute(rsTaskParam); 93 return; 94 } catch (IOException e) { 95 e.printStackTrace(); 96 } 97 } 98 99 private static class RsTaskParams { 100 RenderScript mRenderScript; 101 DepthOfFieldOptions mOptions; 102 103 RsTaskParams(RenderScript renderScript, 104 DepthOfFieldOptions options) { 105 mRenderScript = renderScript; 106 mOptions = options; 107 } 108 } 109 110 private class RsAsyncTaskRunner extends AsyncTask<RsTaskParams, Integer, Bitmap> { 111 112 Bitmap outputImage; 113 Bitmap outputImageNew; 114 ImageCompare.CompareValue result; 115 116 @Override 117 protected Bitmap doInBackground(RsTaskParams... params) { 118 119 publishProgress(0); 120 121 RenderScriptTask renderScriptTask = new RenderScriptTask(params[0].mRenderScript, RenderScriptTask.script.f32); 122 outputImage = renderScriptTask.applyRefocusFilter(params[0].mOptions); 123 publishProgress(1); 124 125 RenderScriptTask renderScriptTaskNew = new RenderScriptTask(params[0].mRenderScript, RenderScriptTask.script.d1new); 126 outputImageNew = renderScriptTaskNew.applyRefocusFilter(params[0].mOptions); 127 publishProgress(2); 128 129 result = new ImageCompare.CompareValue(); 130 ImageCompare.compareBitmap(outputImage, outputImageNew, result); 131 publishProgress(3); 132 133 return outputImage; 134 } 135 136 protected void onPostExecute(Bitmap result) { 137 138 } 139 140 protected void onProgressUpdate(Integer... progress) { 141 switch (progress[0]){ 142 case 0: 143 mAllocLabelTextView.setText("Global Allocation Version..."); 144 mPointerLabelTextView.setText("Processing..."); 145 mCompareTextView.setText("Image Difference"); 146 case 1: 147 mImgView.setImageBitmap(outputImage); 148 mImgView.invalidate(); 149 mPointerLabelTextView.setText("Pointer Result"); 150 mAllocLabelTextView.setText("Processing..."); 151 break; 152 case 2: 153 mNewImgView.setImageBitmap(outputImageNew); 154 mNewImgView.invalidate(); 155 mAllocLabelTextView.setText("Global Allocation Version"); 156 mCompareTextView.setText("Calculating..."); 157 break; 158 case 3: 159 mCompareTextView.setText("Percentage Different: " + result.diffPercent + " Average Difference: " + result.aveDiff); 160 break; 161 } 162 } 163 } 164 165 Uri getLocalRef() { 166 167 168 File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 169 170 Log.v(TAG, "DIRECTORY_DOCUMENTS = " + folder.getAbsolutePath()); 171 ; 172 File f = findJpeg(folder); 173 if (f != null) { 174 Log.v(TAG, "File = " + f); 175 return Uri.fromFile(f); 176 } 177 return null; 178 } 179 180 Uri getResourceRef() { 181 Context context = getApplicationContext(); 182 int resID = R.drawable.refocusimage; 183 Uri path = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + 184 context.getResources().getResourcePackageName(resID) + '/' + 185 context.getResources().getResourceTypeName(resID) + '/' + 186 context.getResources().getResourceEntryName(resID)); 187 return path; 188 } 189 190 private File findJpeg(File dir) { 191 192 File[] files = dir.listFiles(); 193 if (files == null) return null; 194 for (int i = 0; i < files.length; i++) { 195 if (files[i].isDirectory() && !files[i].getName().startsWith(".")) { 196 File ret = findJpeg(files[i]); 197 if (ret != null) { 198 Log.v(TAG, "returning " + ret.getAbsolutePath()); 199 return ret; 200 } 201 continue; 202 } 203 if (files[i].getName().toLowerCase().endsWith(".jpg")) { 204 Log.v(TAG, "returning " + files[i].getAbsolutePath()); 205 return files[i]; 206 207 } 208 } 209 return null; 210 } 211 212} 213