-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
270 lines (215 loc) · 9.51 KB
/
MainActivity.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package advmobdev.unipr.it.landmarkrecognition;
import android.Manifest;
import android.support.v4.content.FileProvider;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.app.Activity;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import org.json.JSONException;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class MainActivity extends Activity{
ImageView viewImage;
Button b, btn_invia;
String mCurrentPhotoPath;
float[] imageDescriptor = new float[2048];
// second image
float[] imageDescriptor2 = new float[2048];
public static final int CAMERA_PICTURE = 1;
public static final int IMAGE_FROM_GALLERY = 2;
private Bitmap bitmapImage;
private Bitmap bitmapImage2;
/*
* getter e setter Bitmap Image
*/
public Bitmap getBitmapImage() {
return bitmapImage;
}
public void setBitmapImage(Bitmap bitmapImage) {
this.bitmapImage = bitmapImage;
}
public Bitmap getBitmapImage2() {
return bitmapImage2;
}
public void setBitmapImage2(Bitmap bitmapImage) {
this.bitmapImage2 = bitmapImage;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.btnSelectPhoto);
btn_invia=(Button)findViewById(R.id.button_invia);
viewImage=(ImageView)findViewById(R.id.viewImage);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
btn_invia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
try {
imageDescriptor = detectFeatures(getBitmapImage());
} catch (JSONException e) {
e.printStackTrace();
}
ArrayList<Float> desc_1 = new ArrayList<>();
for (int i=0; i<2048; i++) {
desc_1.add(imageDescriptor[i]);
}
} catch (IOException e) {
e.printStackTrace();
}
/********* SOCKET CLIENT ***************************************/
//new SocketClient("192.168.1.101", 7001,imageDescriptor, getApplicationContext()).execute();
new SocketClient("192.168.3.155", 7001,imageDescriptor, getApplicationContext()).execute();
}
});
btn_invia.setVisibility(View.GONE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds options to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private float[] detectFeatures(Bitmap bitmap) throws IOException, JSONException {
float[] descriptor = new float[2048];
TensorflowFeaturesDetection featuresDetection = new TensorflowFeaturesDetection(getApplicationContext());
descriptor = featuresDetection.detectFeatures(bitmap);
return descriptor;
}
private void selectImage() {
final CharSequence[] options = { "Scatta Foto", "Scegli da Galleria","Annulla" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Inserisci Foto!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Scatta Foto"))
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
System.out.println("ERRORS during file creations");
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
"advmobdev.unipr.it.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, 1);
}
}
btn_invia.setVisibility(View.VISIBLE);
}
else if (options[item].equals("Scegli da Galleria"))
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
btn_invia.setVisibility(View.VISIBLE);
}
else if (options[item].equals("Annulla")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PICTURE && resultCode == RESULT_OK) {
/********** GALLERY ADD PICTURE *************/
/**********************************************/
viewImage.setDrawingCacheEnabled(true);
Bitmap b = viewImage.getDrawingCache();
MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), b,"Photo", "Prova");
/*****************************************/
/*********** SET PICTURE ***************/
// Get the dimensions of the View
int targetW = viewImage.getWidth();
int targetH = viewImage.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
viewImage.setImageBitmap(bitmap);
setBitmapImage(bitmap);
}
if (requestCode == IMAGE_FROM_GALLERY) {
System.out.println("DEBUG - Request for gallery image!");
Uri uri = data.getData();
String[] prjection ={MediaStore.Images.Media.DATA};
Cursor cursor=getContentResolver().query(uri,prjection,null,null,null);
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(prjection[0]);
String path=cursor.getString(columnIndex);
cursor.close();
Bitmap selectFile = BitmapFactory.decodeFile(path);
viewImage.setImageBitmap(selectFile);
setBitmapImage(selectFile);
}
// second image
if (requestCode == 8) {
System.out.println("DEBUG - Request for second gallery image!");
Uri uri = data.getData();
String[] prjection ={MediaStore.Images.Media.DATA};
Cursor cursor=getContentResolver().query(uri,prjection,null,null,null);
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(prjection[0]);
String path=cursor.getString(columnIndex);
cursor.close();
Bitmap selectFile = BitmapFactory.decodeFile(path);
viewImage.setImageBitmap(selectFile);
setBitmapImage2(selectFile);
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
//File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
}