'Printing multiple circles in a bitmap using canvas in android studio
I am trying to make a seven segment display OCR that detects its number Im going to connect this app to esp32cam i have a webview where I am trying to get the picture from the esp32cam I then get a screenshot from the webview and process that screenshot to become black and white I am now trying to draw circles in this screenshot to know where it will land in the image but I am failing. Im trying to draw many cicles by creating an arraylist of points then using the canvas.drawCircle method in a for loop as it can be seen in the code. I am a begineer so im putting everything in the MainActivity.java.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!OpenCVLoader.initDebug())
Log.e("OpenCV", "Unable to load OpenCV!");
else
Log.d("OpenCV", "OpenCV loaded Successfully!");
//ask for permissions
permission();
//pasting customer details from wifi
pasteCustomerDetails();
//Checking if location is turned on
locationStatusCheck();
//project image
projectImage();
}
//===========Permission asking===================================================================
private void permission() {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION},
PackageManager.PERMISSION_GRANTED);
}
//===========================GPS turning on thing==============================
public void locationStatusCheck() {
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", (dialog, id) -> startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)))
.setNegativeButton("No", (dialog, id) -> dialog.cancel());
final AlertDialog alert = builder.create();
alert.show();
}
public void locationSetting(View v) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
//===========================Selecting Wifi Camera==============================
public void selectWiCam(View v) {
startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
}
//=========================Projecting Captured image of WiCam==================
private void projectImage() {
WebView v = findViewById(R.id.webView);
v.setWebViewClient(new WebViewClient());
v.loadUrl("https://romanelectrichome.com/wp-content/uploads/2020/02/Roman_2-14-20.jpg");
}
public void edit(View s){
WebView v = findViewById(R.id.webView);
v.setWebViewClient(new WebViewClient());
v.loadUrl("https://theedinburghreporter.co.uk/wp-content/uploads/2019/08/smart-meter-emf-safety-network.jpg");
Canvas canvas;
}
//========================Pasting of Customer Details of WiCam==================
private void pasteCustomerDetails() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo;
wifiInfo = wifiManager.getConnectionInfo();
if(wifiInfo.getSupplicantState()== SupplicantState.COMPLETED){
TextView customerS = findViewById(R.id.customer);
customerS.setText(FixTxt(wifiInfo.getSSID()));
}
}
private static String FixTxt(String str)
{
// Removing first and last character
// of a string using substring() method
str = str.substring(1, str.length() - 1);
// Return the modified string
return str;
}
//===================image to Text==========================
public void process(View v) {
//Pasting the customer details to the readings
TextView customerR= (findViewById(R.id.customerReading));
TextView customerS = findViewById(R.id.customer);
customerR.setText(customerS.getText());
//testing the bitmap things
ImageView imageView= (findViewById(R.id.imageView));
Bitmap bitmap = getScreenShotFromView(findViewById(R.id.webView));
imageView.setImageBitmap(newest(bitmap));
}
private void ImagetoTxt (){
}
//method for generating the bitmap incase I have a lot of methods
private Bitmap newest(Bitmap bitmap){
//Bitmap Graybitmap= toGrayscale(bitmap);
return toBWImg(bitmap, Points);
}
// Creating the screenshot of the webview
private Bitmap getScreenShotFromView(View v){
Bitmap screenshot;
//screenshot = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.RGB_565);
v.setDrawingCacheEnabled(true);
screenshot= Bitmap.createBitmap(v.getDrawingCache());
return screenshot;
}
// making the screenshot into black and white and pasting the circles
private Bitmap toBWImg(Bitmap bmpOriginal, ArrayList <Point> y)
{
//creating of the points
ArrayList<Point> p= new ArrayList<>( digitPointers(ybtwnpnts,xbtwnpnts,xbtwnDgts,NofDgts,starting,y));
//Grayscaling the bitmap
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
Mat src= new Mat(bmpGrayscale.getHeight(),bmpGrayscale.getWidth(),CvType.CV_8UC4);
Mat lst= new Mat(bmpGrayscale.getHeight(),bmpGrayscale.getWidth(),CvType.CV_8UC4);
Mat dst= new Mat(bmpGrayscale.getHeight(),bmpGrayscale.getWidth(),CvType.CV_8UC4);
Utils.bitmapToMat(bmpGrayscale,src);
Imgproc.cvtColor(src,lst,Imgproc.COLOR_BGR2GRAY);
//Using threshold to make it a pure black and white
Imgproc.threshold(lst, dst, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
//THis is suppose to make the image clearer
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(1,5));
Mat morphed= new Mat();
Imgproc.morphologyEx(dst,morphed, Imgproc.MORPH_OPEN, kernel);
Bitmap output= Bitmap.createBitmap(morphed.cols(), morphed.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(morphed,output);
//Using the method that prints the circle in the image
Bitmap output1= circled(y,output);
return output1;
}
//Drawing the circles in the image
private Bitmap circled (ArrayList<Point> p, Bitmap bitmap){
Paint paint= new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(5);
Canvas c= new Canvas(bitmap);
for (int i=0;i<p.size();i++){
c.drawCircle((float)p.get(i).x,(float)p.get(i).y,20,paint);
}
return bitmap;
}
//====================Making the Array of points===================================//
//declaration of distances among points
int ybtwnpnts=10;
int xbtwnpnts=10;
int xbtwnDgts=50;
int NofDgts=5;
Point starting= new Point (38,64);
ArrayList <Point> Points= new ArrayList<>();
//Creating array of points
private ArrayList<Point> digitPointers(int ybtwnpnt, int xbtwnpnt, int xbtwnDgt, int NofDgts, Point start, ArrayList<Point> p){
Point now= start;
Point Begining= now;
for (int i=0;i<NofDgts;i++){
/*
2
1 3
4
5 7
6
*/
p.add(now); //1
now.x= now.x+xbtwnpnt; now.y= now.y+ ybtwnpnt; p.add(now); //2
now.x= now.x+xbtwnpnt; now.y= now.y- ybtwnpnt; p.add(now); //3
now.x= now.x-xbtwnpnt; now.y= now.y- ybtwnpnt; p.add(now); //4
now.x= now.x-xbtwnpnt; now.y= now.y- ybtwnpnt; p.add(now); //5
now.x= now.x+xbtwnpnt; now.y= now.y- ybtwnpnt; p.add(now); //6
now.x= now.x+xbtwnpnt; now.y= now.y+ ybtwnpnt; p.add(now); //7
now= Begining; //going back to starting point
now.x= now.x+ xbtwnDgt; //going to the next Dgt
}
return p;
};
//getting the color of points to int
}
and my xml file is
{<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="400dp"
android:layout_height="142dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/scanBtn"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:onClick="selectWiCam"
android:text="@string/scanText"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="22dp"
android:minHeight="48dp"
android:onClick="locationSetting"
android:text="@string/turnOnLocation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/scanBtn"
tools:ignore="TouchTargetSizeCheck" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="49dp"
android:text="@string/selectWiCam"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="396dp"
android:layout_height="276dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout2">
<TextView
android:id="@+id/previewTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:autofillHints="0"
android:backgroundTint="#03A9F4"
android:backgroundTintMode="src_in"
android:clickable="false"
android:ems="10"
android:fontFamily="sans-serif-black"
android:gravity="center|top"
android:hint="@string/testName"
android:labelFor="@string/previewTitleText"
android:minHeight="48dp"
android:text="@string/previewTitleText"
android:textAlignment="center"
android:textColor="#039BE5"
android:textSize="14sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/costumerTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/costumerHomeText"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/previewTitle" />
<Button
android:id="@+id/editBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:onClick="edit"
android:text="@string/editBtnText"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/processBtn" />
<Button
android:id="@+id/processBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:backgroundTint="#FF9800"
android:onClick="process"
android:text="@string/processBtnText"
android:textColor="#424242"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/rateTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/rateHomeText"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/customer" />
<EditText
android:id="@+id/rate"
android:layout_width="141dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:autofillHints="rate"
android:ems="10"
android:enabled="true"
android:hint="@string/rateDefault"
android:inputType="numberDecimal"
android:minHeight="48dp"
android:text="@string/rateDefault"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rateTitle"
tools:ignore="TextContrastCheck" />
<EditText
android:id="@+id/customer"
android:layout_width="141dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:autofillHints="name"
android:ems="10"
android:enabled="false"
android:inputType="textPersonName"
android:minHeight="48dp"
android:text="@string/testName"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/costumerTitle"
tools:ignore="DuplicateSpeakableTextCheck,DuplicateSpeakableTextCheck" />
<WebView
android:id="@+id/webView"
android:layout_width="231dp"
android:layout_height="127dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/previewTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="393dp"
android:layout_height="270dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout">
<TextView
android:id="@+id/readingTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:autofillHints="0"
android:backgroundTint="#03A9F4"
android:backgroundTintMode="src_in"
android:ems="10"
android:fontFamily="sans-serif-black"
android:gravity="center|top"
android:labelFor="@string/readingTitleText"
android:minHeight="48dp"
android:text="@string/readingTitleText"
android:textAlignment="center"
android:textColor="#039BE5"
android:textSize="14sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="@string/costumerHomeText"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/customerReading"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/readingTitle" />
<TextView
android:id="@+id/name3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:text="@string/meterReadingHomeText"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/meterReading"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/energy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:text="@string/energyUsedHomeText"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/energyUsedReading"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/bill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:text="@string/billHomeText"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/billReading"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:autofillHints="name"
android:id="@+id/customerReading"
android:layout_width="120dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:enabled="false"
android:inputType="textPersonName"
android:minHeight="48dp"
android:text="@string/testName"
android:textAlignment="textStart"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/name3"
app:layout_constraintTop_toBottomOf="@+id/readingTitle" />
<EditText
android:id="@+id/meterReading"
android:layout_width="123dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:autofillHints="meterReading"
android:ems="10"
android:enabled="false"
android:inputType="numberDecimal"
android:minHeight="48dp"
android:text="@string/Zero"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/name3"
app:layout_constraintTop_toBottomOf="@+id/customerReading"
tools:ignore="DuplicateSpeakableTextCheck" />
<EditText
android:autofillHints="energyUsedReading"
android:id="@+id/energyUsedReading"
android:layout_width="123dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:enabled="false"
android:inputType="numberDecimal"
android:minHeight="48dp"
android:text="@string/Zero"
android:textSize="12sp"
android:textStyle="italic"
app:layout_constraintStart_toEndOf="@+id/name3"
app:layout_constraintTop_toBottomOf="@+id/meterReading" />
<EditText
android:id="@+id/billReading"
android:layout_width="123dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:autofillHints="BillReading"
android:ems="10"
android:enabled="false"
android:inputType="numberDecimal"
android:minHeight="48dp"
android:text="@string/Zero"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/name3"
app:layout_constraintTop_toBottomOf="@+id/energyUsedReading"
tools:ignore="TouchTargetSizeCheck" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="128dp"
android:layout_marginEnd="26dp"
android:layout_marginBottom="19dp"
app:layout_constraintBottom_toTopOf="@+id/billReading"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="ImageContrastCheck"
tools:srcCompat="@tools:sample/avatars"
android:contentDescription="TODO" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout> }
How do i print the circles properly in the image? as of now it all looks like this this is the current output
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
