Recently, I have been working on an Android app using OpenCV for visible light positioning. While identifying contours, I accidentally used a QR code image from my phone as a sample for testing and found that the lines were very clear, just like the image below:
Isn't this a line draft? Maybe I can create an app that can convert images into line drafts with just one click. It might come in handy in the future and would be convenient for drawing enthusiasts as well.
So, I made a copy of my project for visible light positioning, changed the package name, logo, and background image, and then redesigned the UI. I also added a feature to save the image by long-pressing it. After various tests and debugging, I successfully created a stable version in just two days. Finally, I released the app and the project on my Gitee repository.
Key parts of the code:#
Save image:#
// Save image by long-pressing
public static void saveBitmap(ImageView view, String filePath) {
Drawable drawable = view.getDrawable();
if (drawable == null) {
return;
}
FileOutputStream outStream = null;
File file = new File(filePath);
if (file.isDirectory()) {
return;
}
try {
outStream = new FileOutputStream(file);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (outStream != null) {
outStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Refresh media gallery:#
// Refresh media gallery
private void updateGallery(String filename) {
MediaScannerConnection.scanFile(this,
new String[] { filename }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
Generate directory and random file name:#
// Generate random file name
private String generateFileName() {
String fileList = getExternalStorageDirectory().getAbsolutePath() + File.separator + "LineDraft" + File.separator;
File mkdir = new File(fileList);
if(!mkdir.exists()) mkdir.mkdir();
@SuppressLint("SimpleDateFormat") DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String formatDate = format.format(new Date());
int random = new Random().nextInt(10000);
return (fileList + formatDate + random + ".png");
}
Preview:
Welcome screen | Main screen | Conversion and save |
Since I only used the ARM architecture of the OpenCV library, it runs smoothly on both Redmi Note 1 and Xiaomi 6X. Therefore, I believe it supports almost all Android smartphones, but compatibility with Android tablets is still unknown.