-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScannerActivity.java
164 lines (133 loc) · 5.59 KB
/
ScannerActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.murtic.adis.techyz;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.Button;
import com.google.zxing.Result;
import java.net.URI;
import java.net.URISyntaxException;
import me.dm7.barcodescanner.core.IViewFinder;
import me.dm7.barcodescanner.core.ViewFinderView;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
mScannerView = new ZXingScannerView(this) {
@Override
protected IViewFinder createViewFinderView(Context context) {
return new CustomViewFinderView(context);
}
};
setContentView(mScannerView); // Set the scanner view as the content view
}
private static class CustomViewFinderView extends ViewFinderView {
public static final String TRADE_MARK_TEXT = "Postavite kod u centar kvadrata:";
public static final int TRADE_MARK_TEXT_SIZE_SP = 15;
public final Paint PAINT = new Paint();
public CustomViewFinderView(Context context) {
super(context);
init();
}
public CustomViewFinderView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
PAINT.setColor(Color.WHITE);
PAINT.setAntiAlias(true);
float textPixelSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
TRADE_MARK_TEXT_SIZE_SP, getResources().getDisplayMetrics());
PAINT.setTextSize(textPixelSize);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawTradeMark(canvas);
}
private void drawTradeMark(Canvas canvas) {
Rect framingRect = getFramingRect();
float tradeMarkTop;
float tradeMarkLeft;
if (framingRect != null) {
tradeMarkTop = framingRect.top + PAINT.getTextSize() - 100;
tradeMarkLeft = framingRect.left + 200;
} else {
tradeMarkTop = 10;
tradeMarkLeft = canvas.getHeight() - PAINT.getTextSize() - 10;
}
canvas.drawText(TRADE_MARK_TEXT, tradeMarkLeft, tradeMarkTop, PAINT);
}
}
@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
@Override
public void handleResult(final Result rawResult) {
// Do something with the result here
final AlertDialog.Builder buildAlert = new AlertDialog.Builder(this, R.style.Base_Theme_AppCompat_Dialog_Alert);
buildAlert.setTitle("Result");
buildAlert.setMessage(rawResult.getText() + "\n" + rawResult.getBarcodeFormat().toString());
buildAlert.setNegativeButton("Zatvori", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
mScannerView.startCamera();
}
});
buildAlert.setNeutralButton("Kopiraj", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Gets a handle to the clipboard service.
ClipboardManager clipboard = (ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
// Creates a new text clip to put on the clipboard
ClipData clip = ClipData.newPlainText("scanned_text",rawResult.getText());
// Set the clipboard's primary clip.
clipboard.setPrimaryClip(clip);
dialogInterface.cancel();
mScannerView.startCamera();
}
});
buildAlert.setPositiveButton("Posjeti URL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent goToScanned = new Intent(ScannerActivity.this, MainActivity.class);
goToScanned.putExtra("scanned", rawResult.getText());
startActivity(goToScanned);
}
});
final AlertDialog alertDialog = buildAlert.create();
alertDialog.show();
// Provjeri da li je skenirani tekst zapravo URL ako jeste uljuci Posjeti URL dugmic
// Ako nije url iskljucit ce dugmic
Button posjetiURL = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
try{
new URI(rawResult.getText());
posjetiURL.setEnabled(true);
} catch (URISyntaxException e) {
e.printStackTrace();
posjetiURL.setEnabled(false);
}
}
}