Биография:
Меня зовут Гусев Максим, родился я осенью . На данный момент учусь в университете ИТМО на 3 курсе. Мое хобби это автомобили
Навыки:
- Базовые знания программирования:
- C++
- C#
- Git
- Java
Мое тотемное животное
,`````. _________
' CiCi `, /_ ___ \
' ^_^ `. /@ \/@ \ \
` , . , ' `.. \__/\___/ /
\_\/______/
/ /\\\\\
| |\\\\\\
\ \\\\\\
\______/\\\\ -ccw-
_______ ||_||_______
(______(((_(((______(@)
Код клиентской части приложения под андроид
public class MainActivity extends AppCompatActivity {
public static TextView textView;
private TextureView cameraTextureView;
private Button readButton;
private HandlerThread backgroundThread;
private ImageView imageView;
private Spinner spinner;
public static String qrResult = "0";
Bitmap barcode_bitmap;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ||
(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED))
{
requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}
textView = findViewById(R.id.textView);
readButton = findViewById(R.id.readButton);
imageView = findViewById(R.id.imageView);
spinner = findViewById(R.id.spinner);
readButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(getApplicationContext(), ScanCodeActivity.class), 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
new Client().execute();
}
}
private static Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height)
throws WriterException {
String contentsToEncode = contents;
if (contentsToEncode == null) {
return null;
}
Map hints = null;
String encoding = guessAppropriateEncoding(contentsToEncode);
if (encoding != null) {
hints = new EnumMap(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result;
try {
result = writer.encode(contentsToEncode, format, img_width, img_height, hints);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
private static String guessAppropriateEncoding(CharSequence contents) {
for (int i = 0; i < contents.length(); i++) {
if (contents.charAt(i) > 0xFF) {
return "UTF-8";
}
}
return null;
}
public void getQRButtonClick(View view) throws WriterException {
new Client().execute();
}
class Client extends AsyncTask
{
String server = "https://secure-refuge-57247.herokuapp.com/ticket";
String code;
String answer;
@SuppressLint("WrongThread")
@Override
protected String doInBackground(String... strings) {
BufferedReader reader = null;
try {
@SuppressLint("WrongThread") URL url = new URL(server+"?a="+spinner.getSelectedItemPosition()+"&b="+qrResult);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(10000);
connection.connect();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
answer = reader.readLine();
if(qrResult == "0") {
//imageView.setImageBitmap(barcode_bitmap);
if(answer.compareTo("No more tickets") == 0){
textView.setTextColor(Color.rgb(128, 0, 128));
textView.setText(answer);
}
else{
barcode_bitmap = encodeAsBitmap(answer, BarcodeFormat.QR_CODE, 300, 300);
}
}
else {
if(answer.compareTo("correct") == 0){
textView.setTextColor(Color.GREEN);
}
else {
textView.setTextColor(Color.RED);
}
textView.setText(answer);
}
} catch (IOException | WriterException e) {
e.printStackTrace();
}
finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
imageView.setImageBitmap(barcode_bitmap);
}
}
}