private Bitmap drawTextToBitmap(String gText) { Bitmap bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888); bitmap.eraseColor(Color.TRANSPARENT); bitmap.setHasAlpha(true); // bitmap.setHasAlpha(true); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.TRANSPARENT); TextPaint textPaint = new TextPaint(); textPaint.setColor(Color.GREEN); textPaint.setTextSize(35); RectF rect = new RectF(0, 0, 300, 300); StaticLayout sl = new StaticLayout( gText, textPaint, (int) rect.width(), Layout.Alignment.ALIGN_NORMAL, 1, 1, false); canvas.save(); sl.draw(canvas); canvas.restore(); // Flip the image. Matrix m = new Matrix(); m.preScale(-1, 1); Bitmap output = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, false); output.setDensity(DisplayMetrics.DENSITY_DEFAULT); return output; }
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) @Override public synchronized Bitmap getDirty(int width, int height, Bitmap.Config config) { // Config will be null for non public config types, which can lead to transformations naively // passing in // null as the requested config here. See issue #194. final Bitmap result = strategy.get(width, height, config != null ? config : DEFAULT_CONFIG); if (result == null) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Missing bitmap=" + strategy.logBitmap(width, height, config)); } misses++; } else { hits++; currentSize -= strategy.getSize(result); tracker.remove(result); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { result.setHasAlpha(true); } } if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Get bitmap=" + strategy.logBitmap(width, height, config)); } dump(); return result; }
@Test public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlphaAndOutBitmapIsReused() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888); reset(bitmapPool); when(bitmapPool.get(eq(50), eq(50), eq(Bitmap.Config.ARGB_8888))).thenReturn(toReuse); toReuse.setHasAlpha(false); toTransform.setHasAlpha(true); Bitmap result = TransformationUtils.centerCrop( bitmapPool, toTransform, toReuse.getWidth(), toReuse.getHeight()); assertEquals(toReuse, result); assertTrue(result.hasAlpha()); }
/** Takes a screenshot of the current display and shows an animation. */ void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible) { // We need to orient the screenshot correctly (and the Surface api seems to take screenshots // only in the natural orientation of the device :!) mDisplay.getRealMetrics(mDisplayMetrics); float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels}; // float degrees = getDegreesForRotation(mDisplay.getRotation()); int value = mDisplay.getRotation(); String hwRotation = SystemProperties.get("ro.sf.hwrotation", "0"); if (hwRotation.equals("270") || hwRotation.equals("90")) { value = (value + 3) % 4; } float degrees = getDegreesForRotation(value); boolean requiresRotation = (degrees > 0); if (requiresRotation) { // Get the dimensions of the device in its native orientation mDisplayMatrix.reset(); mDisplayMatrix.preRotate(-degrees); mDisplayMatrix.mapPoints(dims); dims[0] = Math.abs(dims[0]); dims[1] = Math.abs(dims[1]); } // Take the screenshot mScreenBitmap = Surface.screenshot((int) dims[0], (int) dims[1]); if (mScreenBitmap == null) { notifyScreenshotError(mContext, mNotificationManager); finisher.run(); return; } if (requiresRotation) { // Rotate the screenshot to the current orientation Bitmap ss = Bitmap.createBitmap( mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(ss); c.translate(ss.getWidth() / 2, ss.getHeight() / 2); c.rotate(degrees); c.translate(-dims[0] / 2, -dims[1] / 2); c.drawBitmap(mScreenBitmap, 0, 0, null); c.setBitmap(null); mScreenBitmap = ss; } // Optimizations mScreenBitmap.setHasAlpha(false); mScreenBitmap.prepareToDraw(); // Start the post-screenshot animation startAnimation( finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, statusBarVisible, navBarVisible); }
@Test public void testFitCenterSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlpha() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); toTransform.setHasAlpha(false); Bitmap result = TransformationUtils.fitCenter( bitmapPool, toTransform, toTransform.getWidth() / 2, toTransform.getHeight() / 2); assertFalse(result.hasAlpha()); }
@Test public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlpha() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); toTransform.setHasAlpha(true); Bitmap result = TransformationUtils.centerCrop( bitmapPool, toTransform, toTransform.getWidth() / 2, toTransform.getHeight() / 2); assertTrue(result.hasAlpha()); }
@Override public void process(Bitmap output, Bitmap source) { output.setHasAlpha(true); if (mBorderRadius < 0.01f) { super.process(output, source); return; } Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); Canvas canvas = new Canvas(output); float radius = getRadius(source); canvas.drawRoundRect( new RectF(0, 0, source.getWidth(), source.getHeight()), radius, radius, paint); }
// Rotates the bitmap by the specified degree. // If a new bitmap is created, the original bitmap is recycled. 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 { b.setHasAlpha(true); 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; }
public JSONObject verify(ImageView mImageViewFingerprint, String prints) { JSONObject res = new JSONObject(); try { // Init Device long openError = sgfpLib.OpenDevice(0); debugMessage("Open Device Response: " + openError); // Get Device Info SGDeviceInfoParam deviceInfo = getDeviceInfo(); mImageWidth = deviceInfo.imageWidth; mImageHeight = deviceInfo.imageHeight; // Initialize Bitmap Image grayBuffer = new int[JSGFPLib.MAX_IMAGE_WIDTH_ALL_DEVICES * JSGFPLib.MAX_IMAGE_HEIGHT_ALL_DEVICES]; for (int i = 0; i < grayBuffer.length; ++i) grayBuffer[i] = android.graphics.Color.GRAY; grayBitmap = Bitmap.createBitmap( JSGFPLib.MAX_IMAGE_WIDTH_ALL_DEVICES, JSGFPLib.MAX_IMAGE_HEIGHT_ALL_DEVICES, Bitmap.Config.ARGB_8888); grayBitmap.setPixels( grayBuffer, 0, JSGFPLib.MAX_IMAGE_WIDTH_ALL_DEVICES, 0, 0, JSGFPLib.MAX_IMAGE_WIDTH_ALL_DEVICES, JSGFPLib.MAX_IMAGE_HEIGHT_ALL_DEVICES); mImageViewFingerprint.setImageBitmap(grayBitmap); if (mVerifyImage != null) { mVerifyImage = null; } // Capture Print mMaxTemplateSize = new int[1]; sgfpLib.SetTemplateFormat(SGFDxTemplateFormat.TEMPLATE_FORMAT_SG400); sgfpLib.GetMaxTemplateSize(mMaxTemplateSize); long dwTimeStart = 0, dwTimeEnd = 0, dwTimeElapsed = 0; debugMessage("Clicked REGISTER\n"); mVerifyTemplate = new byte[mMaxTemplateSize[0]]; mVerifyImage = new byte[mImageWidth * mImageHeight]; ByteBuffer byteBuf = ByteBuffer.allocate(mImageWidth * mImageHeight); dwTimeStart = System.currentTimeMillis(); long result = sgfpLib.GetImage(mVerifyImage); dwTimeEnd = System.currentTimeMillis(); dwTimeElapsed = dwTimeEnd - dwTimeStart; debugMessage("GetImage() ret: " + result + "[" + dwTimeElapsed + " ms]\n"); Bitmap b = Bitmap.createBitmap(mImageWidth, mImageHeight, Bitmap.Config.ARGB_8888); b.setHasAlpha(false); byteBuf.put(mVerifyImage); int[] intbuffer = new int[mImageWidth * mImageHeight]; for (int i = 0; i < intbuffer.length; i++) { intbuffer[i] = (int) mVerifyImage[i]; } b.setPixels(intbuffer, 0, mImageWidth, 0, 0, mImageWidth, mImageHeight); mImageViewFingerprint.setImageBitmap(this.toGrayScale(b)); // String printImage = encodeImage(mRegisterImage); String printImage = processPicture(this.toGrayScale(b)); // Get Image Quality double iq = 0.0; int img_quality[] = new int[1]; long q_res = sgfpLib.GetImageQuality(mImageWidth, mImageHeight, mVerifyImage, img_quality); if (q_res == SGFDxErrorCode.SGFDX_ERROR_NONE) { iq = img_quality[0]; // txtImageQuality.setText("" + iq); System.err.println(TAG + "Image Quality: " + iq); } else { System.err.println(TAG + "Unable To Get Image Quality"); } // Generate Template dwTimeStart = System.currentTimeMillis(); result = sgfpLib.SetTemplateFormat(SGFDxTemplateFormat.TEMPLATE_FORMAT_SG400); dwTimeEnd = System.currentTimeMillis(); dwTimeElapsed = dwTimeEnd - dwTimeStart; debugMessage("SetTemplateFormat(SG400) ret: " + result + " [" + dwTimeElapsed + " ms]\n"); SGFingerInfo fpInfo = new SGFingerInfo(); for (int i = 0; i < mVerifyTemplate.length; i++) { mVerifyTemplate[i] = 0; } dwTimeStart = System.currentTimeMillis(); result = sgfpLib.CreateTemplate(fpInfo, mVerifyImage, mVerifyTemplate); dwTimeEnd = System.currentTimeMillis(); dwTimeElapsed = dwTimeEnd - dwTimeStart; // debugMessage("Finger Print Template: " + printImage); debugMessage("CreateTemplate() ret: " + result + " [" + dwTimeElapsed + " ms]\n"); // Match Template for (int i = 0; i < mVerifyTemplate.length; ++i) mVerifyTemplate[i] = 0; dwTimeStart = System.currentTimeMillis(); result = sgfpLib.CreateTemplate(fpInfo, mVerifyImage, mVerifyTemplate); // DumpFile("verify.min", mVerifyTemplate); dwTimeEnd = System.currentTimeMillis(); dwTimeElapsed = dwTimeEnd - dwTimeStart; debugMessage("CreateTemplate() ret:" + result + " [" + dwTimeElapsed + "ms]\n"); boolean[] matched = new boolean[1]; dwTimeStart = System.currentTimeMillis(); JSONArray serverPrints = new JSONArray(prints); boolean verified = false; for (int i = 0; i < serverPrints.length(); i++) { byte[] print = android.util.Base64.decode(serverPrints.getString(i), android.util.Base64.NO_WRAP); result = sgfpLib.MatchTemplate(print, mVerifyTemplate, SGFDxSecurityLevel.SL_NORMAL, matched); if (matched[0]) { verified = true; } } dwTimeEnd = System.currentTimeMillis(); dwTimeElapsed = dwTimeEnd - dwTimeStart; debugMessage("MatchTemplate() ret:" + result + " [" + dwTimeElapsed + "ms]\n"); if (verified) { Toast.makeText(context, "Authorized", Toast.LENGTH_LONG).show(); } else { Toast.makeText(context, "Not Authorized", Toast.LENGTH_LONG).show(); } sgfpLib.CloseDevice(); res.put("response_code", "1"); res.put("printImage", printImage); // res.put("template",android.util.Base64.encodeToString(mRegisterTemplate, // android.util.Base64.NO_WRAP)); res.put("imageQuality", iq); res.put("mImageWidth", mImageWidth); res.put("mImageHeight", mImageHeight); return res; } catch (Exception e) { debugMessage("Error: " + e.getMessage()); try { res.put("error", e.getMessage()); } catch (JSONException je) { debugMessage(TAG + " " + je.getMessage()); } return res; } }