List of usage examples for android.graphics BitmapFactory decodeFile
public static Bitmap decodeFile(String pathName, Options opts)
From source file:com.dastardlylabs.ti.ocrdroid.OcrdroidModule.java
@Kroll.method @SuppressWarnings("rawtypes") public String ocr(HashMap _config) { assert (_config != null); String dataParentPath = getTessDataDirectory().getAbsolutePath(), //= (String) _config.get("dataParent"), imagePath = StorageHelper.stripFileUri((String) _config.get("image")), language = (String) _config.get("lang"); try {//w w w .j av a 2 s . c o m if (!tessDataExists()) unpackTessData(); } catch (Exception e) { // catch failure and bubble up failure message and options // else continue. } Log.d(LCAT, "ocr called"); Log.d(LCAT, "Setting parent directory for tessdata as DATAPATH".replace("DATAPATH", dataParentPath /*DATA_PATH*/)); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; options.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options); //bitmap.getConfig() try { ExifInterface exif = new ExifInterface(imagePath); int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); Log.v(LCAT, "Orient: " + exifOrientation); int rotate = 0; switch (exifOrientation) { case ExifInterface.ORIENTATION_ROTATE_90: rotate = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: rotate = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: rotate = 270; break; } Log.v(LCAT, "Rotation: " + rotate); if (rotate != 0) { // Getting width & height of the given image. int w = bitmap.getWidth(); int h = bitmap.getHeight(); // Setting pre rotate Matrix mtx = new Matrix(); mtx.preRotate(rotate); // Rotating Bitmap bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false); // tesseract req. ARGB_8888 bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); } } catch (IOException e) { Log.e(LCAT, "Rotate or coversion failed: " + e.toString()); } // ImageView iv = (ImageView) findViewById(R.id.image); // iv.setImageBitmap(bitmap); // iv.setVisibility(View.VISIBLE); Log.v(LCAT, "Before baseApi"); TessBaseAPI baseApi = new TessBaseAPI(); baseApi.setDebug(true); baseApi.init(dataParentPath /*DATA_PATH*/, language); baseApi.setImage(bitmap); //baseApi.get String recognizedText = baseApi.getUTF8Text(); baseApi.end(); Log.v(LCAT, "OCR Result: " + recognizedText); // clean up and show if (language.equalsIgnoreCase("eng")) { recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " "); } return recognizedText.trim(); }
From source file:com.diona.videoplugin.CameraUtil.java
/** * Get the bitmap returned via the camera utility. * //from w w w. jav a2 s .c om * @param activity * context used to access the file system. * @param data * Intent that contains the returned data from the camera utility * @return Bitmap the bitmap that is returned */ public static Bitmap getBitmap(final Activity activity, final Intent data) { try { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = SAMPLE_QUALITY; final File file = FileCacheUtil.getOutputMediaFile(); final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options); final int nh = (int) (bitmap.getHeight() * (SCALED_HEIGHT / bitmap.getWidth())); final Bitmap scaled = Bitmap.createScaledBitmap(bitmap, (int) SCALED_HEIGHT, nh, true); // Delete the file once we have the bitmap FileUtils.deleteQuietly(file); return scaled; } catch (final NullPointerException e) { LogUtil.error(TAG, e); return null; } }
From source file:android.support.asy.image.ImageWorker.java
private void calculateImageViewHeight(String url, ImageView imageView) { String fileName = ((HotPotApplication) ((Activity) mContext).getApplication()) .getCacheImageAbsolutePath(url); final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;/* w ww . ja v a 2 s .c om*/ BitmapFactory.decodeFile(fileName, options); int width = options.outWidth; int height = options.outHeight; if (width != 0) { final int imageViewHeight = mScreenWidth * height / width; if (imageViewHeight != 0) { LayoutParams lp = imageView.getLayoutParams(); if (lp == null) { lp = new LayoutParams(mScreenWidth, imageViewHeight); imageView.setLayoutParams(lp); } else { lp.width = mScreenWidth; lp.height = imageViewHeight; } imageView.invalidate(); } } }
From source file:com.manning.androidhacks.hack040.util.ImageResizer.java
/** * Decode and sample down a bitmap from a file to the requested width and * height./* www. j a va2 s . c om*/ * * @param filename * The full path of the file to decode * @param reqWidth * The requested width of the resulting bitmap * @param reqHeight * The requested height of the resulting bitmap * @return A bitmap sampled down from the original with the same aspect ratio * and dimensions that are equal to or greater than the requested * width and height */ public static synchronized Bitmap decodeSampledBitmapFromFile(String filename, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filename, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filename, options); }
From source file:com.lovejoy777sarootool.rootool.preview.IconPreview.java
private static Bitmap getPreview(File file) { final boolean isImage = MimeTypes.isPicture(file); final boolean isVideo = MimeTypes.isVideo(file); final boolean isApk = file.getName().endsWith(".apk"); Bitmap mBitmap = null;// ww w. j av a 2s. c o m String path = file.getAbsolutePath(); if (isImage) { BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, o); o.inJustDecodeBounds = false; if (o.outWidth != -1 && o.outHeight != -1) { final int originalSize = (o.outHeight > o.outWidth) ? o.outWidth : o.outHeight; o.inSampleSize = originalSize / mWidth; } mBitmap = BitmapFactory.decodeFile(path, o); cache.put(path, mBitmap); return mBitmap; } else if (isVideo) { mBitmap = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MICRO_KIND); cache.put(path, mBitmap); return mBitmap; } else if (isApk) { final PackageInfo packageInfo = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES); if (packageInfo != null) { final ApplicationInfo appInfo = packageInfo.applicationInfo; if (appInfo != null) { appInfo.sourceDir = path; appInfo.publicSourceDir = path; final Drawable icon = appInfo.loadIcon(pm); if (icon != null) { mBitmap = ((BitmapDrawable) icon).getBitmap(); } } } else { // load apk icon from /res/drawable/.. mBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.type_apk); } cache.put(path, mBitmap); return mBitmap; } return null; }
From source file:com.haru.ui.image.workers.MediaProcessorThread.java
private String compressAndSaveImage(String fileImage, int scale) throws Exception { try {//from w w w. j av a 2s .c o m ExifInterface exif = new ExifInterface(fileImage); String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH); String length = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); int rotate = 0; if (/* TODO: DEBUG */ true) { Log.i(TAG, "Before: " + width + "x" + length); } switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_270: rotate = -90; break; case ExifInterface.ORIENTATION_ROTATE_180: rotate = 180; break; case ExifInterface.ORIENTATION_ROTATE_90: rotate = 90; break; } int w = Integer.parseInt(width); int l = Integer.parseInt(length); int what = w > l ? w : l; Options options = new Options(); if (what > 1500) { options.inSampleSize = scale * 4; } else if (what > 1000 && what <= 1500) { options.inSampleSize = scale * 3; } else if (what > 400 && what <= 1000) { options.inSampleSize = scale * 2; } else { options.inSampleSize = scale; } if (/* TODO: DEBUG */ true) { Log.i(TAG, "Scale: " + (what / options.inSampleSize)); Log.i(TAG, "Rotate: " + rotate); } Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options); File original = new File(fileImage); File file = new File((original.getParent() + File.separator + original.getName().replace(".", "_fact_" + scale + "."))); FileOutputStream stream = new FileOutputStream(file); if (rotate != 0) { Matrix matrix = new Matrix(); matrix.setRotate(rotate); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); } bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); if (/* TODO: DEBUG */ true) { ExifInterface exifAfter = new ExifInterface(file.getAbsolutePath()); String widthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_WIDTH); String lengthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_LENGTH); if (/* TODO: DEBUG */ true) { Log.i(TAG, "After: " + widthAfter + "x" + lengthAfter); } } stream.flush(); stream.close(); return file.getAbsolutePath(); } catch (IOException e) { e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw new Exception("Corrupt or deleted file???"); } }
From source file:com.wallerlab.compcellscope.Image_Gallery.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image__gallery); counter = 0;/*from w w w.j a v a2 s . co m*/ final ImageView currPic = new ImageView(this); DisplayMetrics metrics = this.getResources().getDisplayMetrics(); final int screen_width = metrics.widthPixels; SharedPreferences settings = getSharedPreferences(Folder_Chooser.PREFS_NAME, 0); //SharedPreferences.Editor edit = settings.edit(); String path = settings.getString(Folder_Chooser.location_name, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()); //Log.d(LOG, " | " + path + " | "); TextView text1 = (TextView) findViewById(R.id.text1); final TextView text2 = (TextView) findViewById(R.id.text2); text1.setText(path); File directory = new File(path); // get all the files from a directory File[] dump_files = directory.listFiles(); Log.d(LOG, dump_files.length + " "); final File[] fList = removeElements(dump_files, "info.json"); Log.d(LOG, "Filtered Length: " + fList.length); Arrays.sort(fList, new Comparator<File>() { @Override public int compare(File file, File file2) { String one = file.toString(); String two = file2.toString(); //Log.d(LOG, "one: " + one); //Log.d(LOG, "two: " + two); int num_one = Integer.parseInt(one.substring(one.lastIndexOf("(") + 1, one.lastIndexOf(")"))); int num_two = Integer.parseInt(two.substring(two.lastIndexOf("(") + 1, two.lastIndexOf(")"))); return num_one - num_two; } }); try { writeJsonFile(fList); } catch (JSONException e) { Log.d(LOG, "JSON WRITE FAILED"); } //List names programattically LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.linear_table); final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, screen_width + 4); myLinearLayout.setOrientation(LinearLayout.VERTICAL); if (fList != null) { File cur_pic = fList[0]; BitmapFactory.Options opts = new BitmapFactory.Options(); Log.d(LOG, "\n File Location: " + cur_pic.getAbsolutePath() + " \n" + " Parent: " + cur_pic.getParent().toString() + "\n"); Bitmap myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts); currPic.setLayoutParams(params); currPic.setImageBitmap(myImage); currPic.setId(View.generateViewId()); text2.setText(cur_pic.getName()); } myLinearLayout.addView(currPic); //Seekbar seekBar = (SeekBar) findViewById(R.id.seekBar); seekBar.setEnabled(true); seekBar.setMax(fList.length - 1); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { int progress = 0; int length = fList.length; @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { setCounter(i); if (getCounter() <= fList.length) { File cur_pic = fList[getCounter()]; BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; Bitmap myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts); int image_width = opts.outWidth; int image_height = opts.outHeight; int sampleSize = image_width / screen_width; opts.inSampleSize = sampleSize; text2.setText(cur_pic.getName()); opts.inJustDecodeBounds = false; myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts); currPic.setImageBitmap(myImage); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); //Make Button Layout LinearLayout buttonLayout = new LinearLayout(this); buttonLayout.setOrientation(LinearLayout.HORIZONTAL); LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f); buttonLayout.setLayoutParams(LLParams); buttonLayout.setId(View.generateViewId()); //Button Layout Params LinearLayout.LayoutParams param_button = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f); //Prev Pic Button prevPic = new Button(this); prevPic.setText("Previous"); prevPic.setId(View.generateViewId()); prevPic.setLayoutParams(param_button); prevPic.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (fList != null) { decrementCounter(); if (getCounter() >= 0) { File cur_pic = fList[getCounter()]; BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; Bitmap myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts); int image_width = opts.outWidth; int image_height = opts.outHeight; int sampleSize = image_width / screen_width; opts.inSampleSize = sampleSize; text2.setText(cur_pic.getName()); opts.inJustDecodeBounds = false; myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts); //Log.d(LOG, getCounter() + ""); seekBar.setProgress(getCounter()); currPic.setImageBitmap(myImage); } else { setCounter(0); } } else { Toast.makeText(Image_Gallery.this, "There are no pictures in this folder", Toast.LENGTH_SHORT); setCounter(getCounter() - 1); } } }); buttonLayout.addView(prevPic); // Next Picture Button Button nextPic = new Button(this); nextPic.setText("Next"); nextPic.setId(View.generateViewId()); nextPic.setLayoutParams(param_button); buttonLayout.addView(nextPic); nextPic.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (fList != null) { incrementCounter(); if (getCounter() < fList.length) { File cur_pic = fList[getCounter()]; BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; Bitmap myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts); int image_width = opts.outWidth; int image_height = opts.outHeight; int sampleSize = image_width / screen_width; opts.inSampleSize = sampleSize; text2.setText(cur_pic.getName()); opts.inJustDecodeBounds = false; myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts); //Log.d(LOG, getCounter() + ""); seekBar.setProgress(getCounter()); currPic.setImageBitmap(myImage); } else { setCounter(getCounter() - 1); } } else { Toast.makeText(Image_Gallery.this, "There are no pictures in this folder", Toast.LENGTH_SHORT); setCounter(getCounter() - 1); } } }); myLinearLayout.addView(buttonLayout); }
From source file:com.cars.manager.utils.imageChooser.threads.MediaProcessorThread.java
private String compressAndSaveImage(String fileImage, int scale) throws Exception { try {//from w w w. ja va 2s. c om ExifInterface exif = new ExifInterface(fileImage); String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH); String length = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); int rotate = 0; if (Config.DEBUG) { Log.i(TAG, "Before: " + width + "x" + length); } switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_270: rotate = -90; break; case ExifInterface.ORIENTATION_ROTATE_180: rotate = 180; break; case ExifInterface.ORIENTATION_ROTATE_90: rotate = 90; break; } int w = Integer.parseInt(width); int l = Integer.parseInt(length); int what = w > l ? w : l; Options options = new Options(); if (what > 1500) { options.inSampleSize = scale * 4; } else if (what > 1000 && what <= 1500) { options.inSampleSize = scale * 3; } else if (what > 400 && what <= 1000) { options.inSampleSize = scale * 2; } else { options.inSampleSize = scale; } if (Config.DEBUG) { Log.i(TAG, "Scale: " + (what / options.inSampleSize)); Log.i(TAG, "Rotate: " + rotate); } Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options); File original = new File(fileImage); File file = new File((original.getParent() + File.separator + original.getName().replace(".", "_fact_" + scale + "."))); FileOutputStream stream = new FileOutputStream(file); if (rotate != 0) { Matrix matrix = new Matrix(); matrix.setRotate(rotate); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); } bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); if (Config.DEBUG) { ExifInterface exifAfter = new ExifInterface(file.getAbsolutePath()); String widthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_WIDTH); String lengthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_LENGTH); if (Config.DEBUG) { Log.i(TAG, "After: " + widthAfter + "x" + lengthAfter); } } stream.flush(); stream.close(); return file.getAbsolutePath(); } catch (IOException e) { e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw new Exception("Corrupt or deleted file???"); } }
From source file:com.whamads.nativecamera.NativeCameraLauncher.java
public void onActivityResult(int requestCode, int resultCode, Intent intent) { // If image available if (resultCode == Activity.RESULT_OK) { int rotate = 0; try {//from w w w . j ava 2s . c o m // Check if the image was written to BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(this.imageUri.getPath(), options); if (options.outWidth == -1 || options.outHeight == -1) { this.failPicture("Error decoding image."); return; } ExifHelper exif = new ExifHelper(); exif.createInFile(this.imageUri.getPath()); exif.readExifData(); rotate = exif.getOrientation(); Log.i(LOG_TAG, "Uncompressed image rotation value: " + rotate); exif.resetOrientation(); exif.createOutFile(this.imageUri.getPath()); exif.writeExifData(); JSONObject returnObject = new JSONObject(); returnObject.put("url", this.imageUri.toString()); returnObject.put("rotation", rotate); Log.i(LOG_TAG, "Return data: " + returnObject.toString()); PluginResult result = new PluginResult(PluginResult.Status.OK, returnObject); // Log.i(LOG_TAG, "Final Exif orientation value: " + exif.getOrientation()); // Send Uri back to JavaScript for viewing image this.callbackContext.sendPluginResult(result); } catch (IOException e) { e.printStackTrace(); this.failPicture("Error capturing image."); } catch (JSONException e) { e.printStackTrace(); this.failPicture("Error capturing image."); } } // If browse gallery clicked else if (resultCode == CameraActivity.BROWSE_GALLERY) { this.failPicture("BROWSE_GALLERY"); } // If cancelled else if (resultCode == Activity.RESULT_CANCELED) { this.failPicture("Camera cancelled."); } // If something else else { this.failPicture("Did not complete!"); } }
From source file:com.doplgangr.secrecy.FileSystem.storage.java
public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, int reqHeight) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;//w w w . j av a 2 s . c o m BitmapFactory.decodeFile(path, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(path, options); }