Exemplo n.º 1
0
 protected internal override void onResize(float w, float h)
 {
     base.onResize(w, h);
     android.graphics.RectF r = rect();
     mPath.reset();
     if (mOuterRadii != null)
     {
         mPath.addRoundRect(r, mOuterRadii, android.graphics.Path.Direction.CW);
     }
     else
     {
         mPath.addRect(r, android.graphics.Path.Direction.CW);
     }
     if (mInnerRect != null)
     {
         mInnerRect.set(r.left + mInset.left, r.top + mInset.top, r.right - mInset.right,
                        r.bottom - mInset.bottom);
         if (mInnerRect.width() < w && mInnerRect.height() < h)
         {
             if (mInnerRadii != null)
             {
                 mPath.addRoundRect(mInnerRect, mInnerRadii, android.graphics.Path.Direction.CCW);
             }
             else
             {
                 mPath.addRect(mInnerRect, android.graphics.Path.Direction.CCW);
             }
         }
     }
 }
Exemplo n.º 2
0
		/// <summary>
		/// Returns an immutable bitmap from subset of the source bitmap,
		/// transformed by the optional matrix.
		/// </summary>
		/// <remarks>
		/// Returns an immutable bitmap from subset of the source bitmap,
		/// transformed by the optional matrix.  It is
		/// initialized with the same density as the original bitmap.
		/// </remarks>
		/// <param name="source">The bitmap we are subsetting</param>
		/// <param name="x">The x coordinate of the first pixel in source</param>
		/// <param name="y">The y coordinate of the first pixel in source</param>
		/// <param name="width">The number of pixels in each row</param>
		/// <param name="height">The number of rows</param>
		/// <param name="m">Optional matrix to be applied to the pixels</param>
		/// <param name="filter">
		/// true if the source should be filtered.
		/// Only applies if the matrix contains more than just
		/// translation.
		/// </param>
		/// <returns>A bitmap that represents the specified subset of source</returns>
		/// <exception cref="System.ArgumentException">
		/// if the x, y, width, height values are
		/// outside of the dimensions of the source bitmap.
		/// </exception>
		public static android.graphics.Bitmap createBitmap(android.graphics.Bitmap source
			, int x, int y, int width, int height, android.graphics.Matrix m, bool filter)
		{
			checkXYSign(x, y);
			checkWidthHeight(width, height);
			if (x + width > source.getWidth())
			{
				throw new System.ArgumentException("x + width must be <= bitmap.width()");
			}
			if (y + height > source.getHeight())
			{
				throw new System.ArgumentException("y + height must be <= bitmap.height()");
			}
			// check if we can just return our argument unchanged
			if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() && height
				 == source.getHeight() && (m == null || m.isIdentity()))
			{
				return source;
			}
			int neww = width;
			int newh = height;
			android.graphics.Canvas canvas = new android.graphics.Canvas();
			android.graphics.Bitmap bitmap;
			android.graphics.Paint paint;
			android.graphics.Rect srcR = new android.graphics.Rect(x, y, x + width, y + height
				);
			android.graphics.RectF dstR = new android.graphics.RectF(0, 0, width, height);
			android.graphics.Bitmap.Config newConfig = android.graphics.Bitmap.Config.ARGB_8888;
			android.graphics.Bitmap.Config config = source.getConfig();
			// GIF files generate null configs, assume ARGB_8888
			if (config != null)
			{
				switch (config)
				{
					case android.graphics.Bitmap.Config.RGB_565:
					{
						newConfig = android.graphics.Bitmap.Config.RGB_565;
						break;
					}

					case android.graphics.Bitmap.Config.ALPHA_8:
					{
						newConfig = android.graphics.Bitmap.Config.ALPHA_8;
						break;
					}

					case android.graphics.Bitmap.Config.ARGB_4444:
					case android.graphics.Bitmap.Config.ARGB_8888:
					default:
					{
						//noinspection deprecation
						newConfig = android.graphics.Bitmap.Config.ARGB_8888;
						break;
					}
				}
			}
			if (m == null || m.isIdentity())
			{
				bitmap = createBitmap(neww, newh, newConfig, source.hasAlpha());
				paint = null;
			}
			else
			{
				// not needed
				bool transformed = !m.rectStaysRect();
				android.graphics.RectF deviceR = new android.graphics.RectF();
				m.mapRect(deviceR, dstR);
				neww = Sharpen.Util.Round(deviceR.width());
				newh = Sharpen.Util.Round(deviceR.height());
				bitmap = createBitmap(neww, newh, transformed ? android.graphics.Bitmap.Config.ARGB_8888
					 : newConfig, transformed || source.hasAlpha());
				canvas.translate(-deviceR.left, -deviceR.top);
				canvas.concat(m);
				paint = new android.graphics.Paint();
				paint.setFilterBitmap(filter);
				if (transformed)
				{
					paint.setAntiAlias(true);
				}
			}
			// The new bitmap was created from a known bitmap source so assume that
			// they use the same density
			bitmap.mDensity = source.mDensity;
			canvas.setBitmap(bitmap);
			canvas.drawBitmap(source, srcR, dstR, paint);
			canvas.setBitmap(null);
			return bitmap;
		}
Exemplo n.º 3
0
        protected internal override void onDraw(Canvas canvas)
        {
            if (mFaces != null && mZoomRect != null)
            {
                // Prepare matrix
                mMatrix.reset();
                mAspectRatio.reset();
                mActualRect.set(0, 0, mPreviewSize.Width, mPreviewSize.Height);

                // First apply zoom (crop) rect.
                // Unlike the documentation, many device does not report final crop region.
                // So, here we calculate final crop region which takes account aspect ratio between crop region and preview size.
                {
                    mRevisionZoomRect.set(mZoomRect);
                    float left = mRevisionZoomRect.left;
                    float top  = mRevisionZoomRect.top;

                    mRevisionZoomRect.offsetTo(0, 0);

                    mAspectRatio.setRectToRect(mActualRect, mRevisionZoomRect, Matrix.ScaleToFit.CENTER);

                    mAspectRatio.mapRect(mActualRect);
                    mActualRect.offset(left, top);
                }

                mMatrix.postTranslate(-mActualRect.centerX(), -mActualRect.centerY());

                // compensate mirror
                mMatrix.postScale(mFacing == SCameraCharacteristics.LENS_FACING_FRONT ? -1 : 1, 1);

                // Then rotate and scale to UI size
                mMatrix.postRotate(mRotation);
                if (mOrientation == Configuration.ORIENTATION_LANDSCAPE)
                {
                    mMatrix.postScale((float)Width / mActualRect.width(), (float)Height / mActualRect.height());
                }
                else
                {
                    mMatrix.postScale((float)Height / mActualRect.width(), (float)Width / mActualRect.height());
                }
                mMatrix.postTranslate((float)Width / 2, (float)Height / 2);

                foreach (Face face in mFaces)
                {
                    mBoundRect.set(face.Bounds);
                    mMatrix.mapRect(mBoundRect);

                    mPaint.Color       = Color.BLUE;
                    mPaint.StrokeWidth = 3;
                    canvas.drawRect(mBoundRect, mPaint);

                    {                     // Additional features may not supported.
                        float[] point = new float[2];
                        mPaint.Color       = Color.RED;
                        mPaint.StrokeWidth = 10;

                        if (face.LeftEyePosition != null)
                        {
                            mMatrix.mapPoints(point, new float[] { face.LeftEyePosition.x, face.LeftEyePosition.y });
                            canvas.drawPoint(point[0], point[1], mPaint);
                        }

                        if (face.RightEyePosition != null)
                        {
                            mMatrix.mapPoints(point, new float[] { face.RightEyePosition.x, face.RightEyePosition.y });
                            canvas.drawPoint(point[0], point[1], mPaint);
                        }

                        if (face.MouthPosition != null)
                        {
                            mMatrix.mapPoints(point, new float[] { face.MouthPosition.x, face.MouthPosition.y });
                            canvas.drawPoint(point[0], point[1], mPaint);
                        }

                        mPaint.Color       = Color.YELLOW;
                        mPaint.StrokeWidth = 3;
                        mPaint.TextSize    = 30;
                        if (face.Id != Face.ID_UNSUPPORTED)
                        {
                            canvas.drawText(string.Format("ID:{0:D}, Score:{1:D}", face.Id, face.Score), mBoundRect.left, mBoundRect.top, mPaint);
                        }
                        else
                        {
                            canvas.drawText(string.Format("Score:{0:D}", face.Score), mBoundRect.left, mBoundRect.top, mPaint);
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
		internal android.graphics.Path buildRing(android.graphics.drawable.GradientDrawable
			.GradientState st)
		{
			if (mRingPath != null && (!st.mUseLevelForShape || !mPathIsDirty))
			{
				return mRingPath;
			}
			mPathIsDirty = false;
			float sweep = st.mUseLevelForShape ? (360.0f * getLevel() / 10000.0f) : 360f;
			android.graphics.RectF bounds = new android.graphics.RectF(mRect);
			float x = bounds.width() / 2.0f;
			float y = bounds.height() / 2.0f;
			float thickness = st.mThickness != -1 ? st.mThickness : bounds.width() / st.mThicknessRatio;
			// inner radius
			float radius = st.mInnerRadius != -1 ? st.mInnerRadius : bounds.width() / st.mInnerRadiusRatio;
			android.graphics.RectF innerBounds = new android.graphics.RectF(bounds);
			innerBounds.inset(x - radius, y - radius);
			bounds = new android.graphics.RectF(innerBounds);
			bounds.inset(-thickness, -thickness);
			if (mRingPath == null)
			{
				mRingPath = new android.graphics.Path();
			}
			else
			{
				mRingPath.reset();
			}
			android.graphics.Path ringPath = mRingPath;
			// arcTo treats the sweep angle mod 360, so check for that, since we
			// think 360 means draw the entire oval
			if (sweep < 360 && sweep > -360)
			{
				ringPath.setFillType(android.graphics.Path.FillType.EVEN_ODD);
				// inner top
				ringPath.moveTo(x + radius, y);
				// outer top
				ringPath.lineTo(x + radius + thickness, y);
				// outer arc
				ringPath.arcTo(bounds, 0.0f, sweep, false);
				// inner arc
				ringPath.arcTo(innerBounds, sweep, -sweep, false);
				ringPath.close();
			}
			else
			{
				// add the entire ovals
				ringPath.addOval(bounds, android.graphics.Path.Direction.CW);
				ringPath.addOval(innerBounds, android.graphics.Path.Direction.CCW);
			}
			return ringPath;
		}