Skip to content

Commit

Permalink
WIP: Added image processing and POST request send.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrewTSW committed Oct 31, 2018
1 parent 15cdc3d commit b33fc8a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
*.iml
.gradle
/local.properties
/.idea/caches/build_file_checksums.ser
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea
.DS_Store
/build
/captures
Expand Down
14 changes: 12 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
buildToolsVersion '28.0.3'
dexOptions {
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.volley:volley:1.1.1'
implementation 'org.jsoup:jsoup:1.7.2'
implementation group: 'commons-io', name: 'commons-io', version: '2.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="eu.dasancti.reversee">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
Expand Down
79 changes: 79 additions & 0 deletions app/src/main/java/eu/dasancti/reversee/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
package eu.dasancti.reversee;

import android.Manifest;
import android.content.ContentProviderClient;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.Xml;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

public class MainActivity extends AppCompatActivity {

private static final String GOOGLE_REVERSE_IMAGE_SEARCH_URL = "https://www.google.com/searchbyimage/upload";
private static final String FAKE_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -29,9 +52,65 @@ protected void onCreate(Bundle savedInstanceState) {

private void handleImageSearch(Intent intent) {
Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
Toast.makeText(this, "Received image URI:"+imageUri.getPath(), Toast.LENGTH_SHORT).show();
AtomicReference<String> imageData = new AtomicReference<>();
try {
imageData.set(IOUtils.toString(Objects.requireNonNull(getContentResolver().openInputStream(imageUri))));
} catch (FileNotFoundException e) {
String err = "Couldn't open the shared image as an input stream.";
Toast.makeText(this, err, Toast.LENGTH_LONG).show();
Log.e("GET_IMAGE_FILE", err);
this.finish();
return;
} catch (IOException e) {
String err = "Couldn't read input stream of image.";
Toast.makeText(this, err, Toast.LENGTH_LONG).show();
Log.e("GET_IMAGE_FILE", err);
this.finish();
return;
}
Intent searchIntent = new Intent();
searchIntent.setAction(Intent.ACTION_VIEW);
//TODO: Implement browser intent with POST method
RequestQueue requestQueue = Volley.newRequestQueue(this);
final AtomicReference<String> browserRedirect = new AtomicReference<>();
StringRequest getGoogleRedirectUrl = new StringRequest(
Request.Method.POST,
GOOGLE_REVERSE_IMAGE_SEARCH_URL,
response -> {
Document doc = Jsoup.parse(response);
Element content = doc.getElementById("content");
Elements redirects = content.getElementsByTag("meta");
for (Element redirect : redirects) {
String redirectUrl = redirect.attr("url");
if (redirectUrl.contains("/search?tbs=sbi:")) {
browserRedirect.set(redirectUrl);
break;
}
}
if (browserRedirect.get().isEmpty()) {
Toast.makeText(this, "Failed to get redirect URL", Toast.LENGTH_LONG).show();
} else {
//TODO: Launch browser intent with the redirect
Toast.makeText(this, "Found redirect URL for reverse search.", Toast.LENGTH_SHORT).show();
Log.i("REVERSE_SEARCH_URL", browserRedirect.get());
}
},
error -> Toast.makeText(this, "POST call to reverse search failed: "+error.getMessage(), Toast.LENGTH_LONG).show()
) {
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("sch", "sch");
params.put("encoded_image", imageData.get());
return params;
}
public Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("User-Agent", FAKE_USER_AGENT);
return headers;
}
};
requestQueue.add(getGoogleRedirectUrl);
Toast.makeText(this, "Handling google reverse image search.", Toast.LENGTH_LONG).show();
this.finish();
}
Expand Down

0 comments on commit b33fc8a

Please sign in to comment.