List of usage examples for android.graphics Matrix Matrix
public Matrix()
From source file:com.almalence.googsharing.Thumbnail.java
private static Bitmap rotateImage(Bitmap bitmap, int orientation) { if (orientation != 0) { // We only rotate the thumbnail once even if we get OOM. Matrix m = new Matrix(); m.setRotate(orientation, bitmap.getWidth() * 0.5f, bitmap.getHeight() * 0.5f); try {/*from w w w.jav a 2 s . c o m*/ Bitmap rotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); // If the rotated bitmap is the original bitmap, then it // should not be recycled. if (rotated != bitmap) bitmap.recycle(); return rotated; } catch (Exception t) { Log.w(TAG, "Failed to rotate thumbnail", t); } } return bitmap; }
From source file:com.bluetech.gallery5.ui.ImageDetailFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /*// www. ja v a 2 s . c o m // Inflate and locate the main ImageView final View v = inflater.inflate(R.layout.image_detail_fragment, container, false); mImageView = (ImageView) v.findViewById(R.id.imageRecycView); return v; */ final View v = inflater.inflate(R.layout.image_detail_fragment, container, false); mImageView = (ImageView) v.findViewById(R.id.imageRecycView); //ImageView img = new ImageView(getActivity()); mImageView.setPadding(6, 6, 6, 6); // mImageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)) ; //ImageViewHelperURL.setUrlDrawable((ImageView) img, url, R.drawable.no_image) ; mImageView.setOnTouchListener(new View.OnTouchListener() { private static final String TAG = "SlideImageView"; // These matrices will be used to move and zoom image Matrix matrix = new Matrix(); Matrix savedMatrix = new Matrix(); // We can be in one of these 3 states static final int NONE = 0; static final int DRAG = 1; static final int ZOOM = 2; int mode = NONE; // Remember some things for zooming PointF start = new PointF(); PointF mid = new PointF(); float oldDist = 1f; @Override public boolean onTouch(View v, MotionEvent event) { ImageView view = (ImageView) v; // Dump touch event to log dumpEvent(event); // Handle touch events here... switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: savedMatrix.set(matrix); start.set(event.getX(), event.getY()); Log.d(TAG, "mode=DRAG"); mode = DRAG; break; case MotionEvent.ACTION_POINTER_DOWN: oldDist = spacing(event); Log.d(TAG, "oldDist=" + oldDist); if (oldDist > 10f) { savedMatrix.set(matrix); midPoint(mid, event); mode = ZOOM; Log.d(TAG, "mode=ZOOM"); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: mode = NONE; Log.d(TAG, "mode=NONE"); break; case MotionEvent.ACTION_MOVE: if (mode == DRAG) { // ... matrix.set(savedMatrix); matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); } else if (mode == ZOOM) { float newDist = spacing(event); Log.d(TAG, "newDist=" + newDist); if (newDist > 10f) { matrix.set(savedMatrix); float scale = newDist / oldDist; Log.d(TAG, "ZOOOOOOOM: " + scale); matrix.postScale(scale, scale, mid.x, mid.y); } } break; } view.setImageMatrix(matrix); return true; // indicate event was handled } /** Show an event in the LogCat view, for debugging */ private void dumpEvent(MotionEvent event) { String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" }; StringBuilder sb = new StringBuilder(); int action = event.getAction(); int actionCode = action & MotionEvent.ACTION_MASK; sb.append("event ACTION_").append(names[actionCode]); if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP) { sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT); sb.append(")"); } sb.append("["); for (int i = 0; i < event.getPointerCount(); i++) { sb.append("#").append(i); sb.append("(pid ").append(event.getPointerId(i)); sb.append(")=").append((int) event.getX(i)); sb.append(",").append((int) event.getY(i)); if (i + 1 < event.getPointerCount()) sb.append(";"); } sb.append("]"); Log.d(TAG, sb.toString()); } /** Determine the space between the first two fingers */ private float spacing(MotionEvent event) { float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); return (float) Math.sqrt(x * x + y * y); } /** Calculate the mid point of the first two fingers */ private void midPoint(PointF point, MotionEvent event) { float x = event.getX(0) + event.getX(1); float y = event.getY(0) + event.getY(1); point.set(x / 2, y / 2); } }); return v; }
From source file:com.exzogeni.dk.graphics.Bitmaps.java
@NonNull private static Matrix getExifMatrix(int orientation) { final Matrix matrix = new Matrix(); if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { matrix.setRotate(180);// w ww.ja v a 2 s . c om } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { matrix.setRotate(90); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { matrix.setRotate(-90); } return matrix; }
From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.PictureObj.java
public static DbObject from(Context context, Uri imageUri) throws IOException { // Query gallery for camera picture via // Android ContentResolver interface ContentResolver cr = context.getContentResolver(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;/* ww w. j ava2 s . c o m*/ BitmapFactory.decodeStream(cr.openInputStream(imageUri), null, options); int targetSize = 200; int xScale = (options.outWidth + targetSize - 1) / targetSize; int yScale = (options.outHeight + targetSize - 1) / targetSize; int scale = xScale < yScale ? xScale : yScale; //uncomment this to get faster power of two scaling //for(int i = 0; i < 32; ++i) { // int mushed = scale & ~(1 << i); // if(mushed != 0) // scale = mushed; //} options.inJustDecodeBounds = false; options.inSampleSize = scale; Bitmap sourceBitmap = BitmapFactory.decodeStream(cr.openInputStream(imageUri), null, options); int width = sourceBitmap.getWidth(); int height = sourceBitmap.getHeight(); int cropSize = Math.min(width, height); float scaleSize = ((float) targetSize) / cropSize; Matrix matrix = new Matrix(); matrix.postScale(scaleSize, scaleSize); float rotation = PhotoTaker.rotationForImage(context, imageUri); if (rotation != 0f) { matrix.preRotate(rotation); } Bitmap resizedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, baos); byte[] data = baos.toByteArray(); sourceBitmap.recycle(); sourceBitmap = null; resizedBitmap.recycle(); resizedBitmap = null; System.gc(); // TODO: gross. JSONObject base = new JSONObject(); if (ContentCorral.CONTENT_CORRAL_ENABLED) { try { String type = cr.getType(imageUri); if (type == null) { type = "image/jpeg"; } base.put(CorralClient.OBJ_LOCAL_URI, imageUri.toString()); base.put(CorralClient.OBJ_MIME_TYPE, type); String localIp = ContentCorral.getLocalIpAddress(); if (localIp != null) { base.put(Contact.ATTR_LAN_IP, localIp); } } catch (JSONException e) { Log.e(TAG, "impossible json error possible!"); } } return from(base, data); }
From source file:com.wishlist.Utility.java
public static byte[] scaleImage(Context context, Uri photoUri) throws IOException { InputStream is = context.getContentResolver().openInputStream(photoUri); BitmapFactory.Options dbo = new BitmapFactory.Options(); dbo.inJustDecodeBounds = true;//from ww w . j a v a 2 s . c o m BitmapFactory.decodeStream(is, null, dbo); is.close(); int rotatedWidth, rotatedHeight; int orientation = getOrientation(context, photoUri); if (orientation == 90 || orientation == 270) { rotatedWidth = dbo.outHeight; rotatedHeight = dbo.outWidth; } else { rotatedWidth = dbo.outWidth; rotatedHeight = dbo.outHeight; } Bitmap srcBitmap; is = context.getContentResolver().openInputStream(photoUri); if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) { float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION); float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION); float maxRatio = Math.max(widthRatio, heightRatio); // Create the bitmap from file BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = (int) maxRatio; srcBitmap = BitmapFactory.decodeStream(is, null, options); } else { srcBitmap = BitmapFactory.decodeStream(is); } is.close(); /* if the orientation is not 0 (or -1, which means we don't know), we * have to do a rotation. */ if (orientation > 0) { Matrix matrix = new Matrix(); matrix.postRotate(orientation); srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, true); } String type = context.getContentResolver().getType(photoUri); ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (type.equals("image/png")) { srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); } else if (type.equals("image/jpg") || type.equals("image/jpeg")) { srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); } byte[] bMapArray = baos.toByteArray(); baos.close(); return bMapArray; }
From source file:com.example.facebook_photo.Utility.java
public static byte[] scaleImage(Context context, Uri photoUri) throws IOException { InputStream is = context.getContentResolver().openInputStream(photoUri); BitmapFactory.Options dbo = new BitmapFactory.Options(); dbo.inJustDecodeBounds = true;/* w w w .ja va 2 s. c o m*/ BitmapFactory.decodeStream(is, null, dbo); is.close(); int rotatedWidth, rotatedHeight; int orientation = getOrientation(context, photoUri); if (orientation == 90 || orientation == 270) { rotatedWidth = dbo.outHeight; rotatedHeight = dbo.outWidth; } else { rotatedWidth = dbo.outWidth; rotatedHeight = dbo.outHeight; } Bitmap srcBitmap; is = context.getContentResolver().openInputStream(photoUri); if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) { float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION); float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION); float maxRatio = Math.max(widthRatio, heightRatio); // Create the bitmap from file BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = (int) maxRatio; srcBitmap = BitmapFactory.decodeStream(is, null, options); } else { srcBitmap = BitmapFactory.decodeStream(is); } is.close(); /* * if the orientation is not 0 (or -1, which means we don't know), we * have to do a rotation. */ if (orientation > 0) { Matrix matrix = new Matrix(); matrix.postRotate(orientation); srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, true); } String type = context.getContentResolver().getType(photoUri); ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (type.equals("image/png")) { srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); } else if (type.equals("image/jpg") || type.equals("image/jpeg")) { srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); } byte[] bMapArray = baos.toByteArray(); baos.close(); return bMapArray; }
From source file:com.wareninja.opensource.gravatar4android.common.Utils.java
public static Bitmap getBitmapWithReflection(Bitmap originalImage) { //The gap we want between the reflection and the original image final int reflectionGap = 4; int width = originalImage.getWidth(); int height = originalImage.getHeight(); //This will not scale but will flip on the Y axis Matrix matrix = new Matrix(); matrix.preScale(1, -1);//from w w w . j a v a 2 s . c o m //Create a Bitmap with the flip matrix applied to it. //We only want the bottom half of the image Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false); //Create a new bitmap with same width but taller to fit reflection Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888); //Create a new Canvas with the bitmap that's big enough for //the image plus gap plus reflection Canvas canvas = new Canvas(bitmapWithReflection); //Draw in the original image canvas.drawBitmap(originalImage, 0, 0, null); //Draw in the gap Paint deafaultPaint = new Paint(); canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint); //Draw in the reflection canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); //Create a shader that is a linear gradient that covers the reflection Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); //Set the paint to use this shader (linear gradient) paint.setShader(shader); //Set the Transfer mode to be porter duff and destination in paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); //Draw a rectangle using the paint with our linear gradient canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; }
From source file:Main.java
public static Bitmap rotate(Bitmap b, int degrees) { if (degrees != 0 && b != null) { Matrix m = new Matrix(); m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2); try {/*from ww w . j a v a2s. c o m*/ Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true); if (b != b2) { b.recycle(); b = b2; } } catch (OutOfMemoryError ex) { // We have no memory to rotate. Return the original bitmap. } } return b; }
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 .java 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:Main.java
/** * Creates a centered bitmap of the desired size. * * @param source original bitmap source//www. java 2 s .c o m * @param width targeted width * @param height targeted height * @param options options used during thumbnail extraction */ public static Bitmap extractThumbnail(Bitmap source, int width, int height, int options) { if (source == null) { return null; } float scale; if (source.getWidth() < source.getHeight()) { scale = width / (float) source.getWidth(); } else { scale = height / (float) source.getHeight(); } Matrix matrix = new Matrix(); matrix.setScale(scale, scale); Bitmap thumbnail = transform(matrix, source, width, height, OPTIONS_SCALE_UP | options); return thumbnail; }