给图片添加边框,就像安卓上的谷歌相片扫描应用程序一样。

huangapple go评论99阅读模式
英文:

Adding border to image like google photo scan app in android

问题

在安卓中是否可能在不向用户显示图像的情况下为图像添加边框,类似于Google Photo Scan应用中的做法... 这里添加了带有PhotoScan徽标的图像边框

英文:

Is it possible to add border to a image without showing the image to the user in android.
Like google photo scan app..Here a border is added to image with photoscan logo

答案1

得分: 1

你可以使用这个库:

implementation 'com.github.siyamed:android-shape-imageview:0.9.+@aar'

在 XML 文件中:

<com.github.siyamed.shapeimageview.BubbleImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/neo"
    app:siArrowPosition="right"
    app:siSquare="true"/>
英文:

You can use this this library

implementation &#39;com.github.siyamed:android-shape-imageview:0.9.+@aar&#39;

in Xml File

&lt;com.github.siyamed.shapeimageview.BubbleImageView
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:src=&quot;@drawable/neo&quot;
    app:siArrowPosition=&quot;right&quot;
    app:siSquare=&quot;true&quot;/&gt;

答案2

得分: 0

  • 设计了一个自定义布局,其中包含一个图片视图。
  • 将其实例化为一个视图对象。
  • 在布局中将我的图片设置到该图片视图中。
  • 将视图绘制到位图并保存。

用于实例化和绘制的代码

public Bitmap drawToBitmap(Context context, final int layoutResId,
                           final int width, final int height, String imageDescription) {
    final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bmp);
    final LayoutInflater inflater =
            (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View layout = inflater.inflate(layoutResId, null);
    ImageView img = layout.findViewById(R.id.imgAnalysedBitmap);
    img.setImageBitmap(croppedBitmap);
    layout.setDrawingCacheEnabled(true);
    layout.measure(
            View.MeasureSpec.makeMeasureSpec(canvas.getWidth(), View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(canvas.getHeight(), View.MeasureSpec.EXACTLY));
    layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
    canvas.drawBitmap(layout.getDrawingCache(), 0, 0, new Paint());
    return bmp;
}

使用:

final DisplayMetrics metrics = getResources().getDisplayMetrics();
Bitmap bmap = drawToBitmap(this, R.layout.layout_watermark, metrics.widthPixels, metrics.heightPixels, imageDescription);
英文:
  • Designed a custom layout with a image view
  • Inflated it to a view object
  • Set the my image to that image view in layout
  • Drawn the view to a bitmap and saved it

Code for inflating and drawing

    public Bitmap drawToBitmap(Context context, final int layoutResId,
                           final int width, final int height, String imageDescription) {
    final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bmp);
    final LayoutInflater inflater =
            (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View layout = inflater.inflate(layoutResId, null);
    ImageView img = layout.findViewById(R.id.imgAnalysedBitmap);
    img.setImageBitmap(croppedBitmap);
    layout.setDrawingCacheEnabled(true);
    layout.measure(
            View.MeasureSpec.makeMeasureSpec(canvas.getWidth(), View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(canvas.getHeight(), View.MeasureSpec.EXACTLY));
    layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
    canvas.drawBitmap(layout.getDrawingCache(), 0, 0, new Paint());
    return bmp;
}

Usage:

final DisplayMetrics metrics = getResources().getDisplayMetrics();
        Bitmap bmap = drawToBitmap(this, R.layout.layout_watermark, metrics.widthPixels, metrics.heightPixels, imageDescription);

huangapple
  • 本文由 发表于 2020年6月29日 15:26:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/62633168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定