Last active
November 30, 2020 22:07
-
-
Save null-product/d84dd0d2e268f356f51083c1e87c0a9c to your computer and use it in GitHub Desktop.
CanvasでBitmap画像に図形を重ねて描画
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:padding="0dip" | |
tools:context="com.nullproduct.blogsample.MainActivity"> | |
<com.nullproduct.blogsample.CanvasView | |
android:id="@+id/CanvasView" | |
android:layout_height="match_parent" | |
android:layout_width="match_parent" /> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CanvasView extends View { | |
private Bitmap mBitmap; | |
List<List<Point>> mPoints; | |
public CanvasView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
mBitmap = null; | |
mPoints = null; | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
canvasDraw(canvas); | |
} | |
private void canvasDraw(Canvas canvas) { | |
Paint paint = new Paint(); | |
paint.setStrokeWidth(100); | |
/* ⑤:背景色を設定 */ | |
canvas.drawColor(Color.BLACK); | |
/* ⑥:原画像(Bitmap)を描画 */ | |
if(mBitmap != null) { | |
float scale = (float)getWidth() / mBitmap.getWidth(); | |
canvas.scale(scale, scale); | |
canvas.drawBitmap(mBitmap, 0, 0, paint); | |
} | |
/* ⑦:輪郭を描画 */ | |
if(mPoints != null) { | |
Path path = new Path(); | |
paint.setAntiAlias(true); | |
paint.setStyle(Paint.Style.STROKE); | |
paint.setColor(Color.argb(128, 255, 0, 0)); | |
for(int i = 0; i < mPoints.size(); i++) { | |
List<Point> point = mPoints.get(i); | |
path.moveTo((float)point.get(0).x, (float)point.get(0).y); | |
for(int j = 0; j < point.size(); j++) { | |
path.lineTo((float)point.get(j).x, (float)point.get(j).y); | |
} | |
path.lineTo((float)point.get(0).x, (float)point.get(0).y); | |
for(int j = 0; j < point.size(); j++) { | |
path.lineTo((float)point.get(j).x, (float)point.get(j).y); | |
} | |
} | |
canvas.drawPath(path, paint); | |
} | |
} | |
public void setmBitmap(Bitmap bmp) { | |
mBitmap = bmp; | |
} | |
public void setmPoints(List<List<Point>> points) { | |
mPoints = points; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
static { | |
System.loadLibrary("opencv_java3"); | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
onImageScan(); | |
} | |
private void onImageScan() { | |
/* 画像読み込み */ | |
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.src); | |
Mat mat = new Mat(); | |
Utils.bitmapToMat(bmp, mat, true); | |
/* 画像を二値化 */ | |
mat = getThreshold(mat); | |
/* 輪郭の座標を取得 */ | |
List<MatOfPoint> contours = getContour(mat); | |
List<List<Point>> points = contour2point(contours); | |
/* ①:描画先のVIEWを取得 */ | |
CanvasView canvasView = (CanvasView)findViewById(R.id.CanvasView); | |
/* ②:VIEWに原画像(Bitmap)を登録 */ | |
canvasView.setmBitmap(bmp); | |
/* ③:VIEWに抽出した輪郭座標を登録 */ | |
canvasView.setmPoints(points); | |
/* ④:VIEWの描画更新要求 */ | |
canvasView.invalidate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment