I have the following code that runs in a service where it captures screenshots from Android. What technology could I use to send a remote command to this service to execute the task? I don't want it to run from a MainActivity button. Firebase cloud messaging ? Websocket ?
ScreenCaptureService.java
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.view.Display;
import android.view.OrientationEventListener;
import android.view.WindowManager;
import androidx.annotation.RequiresApi;
import androidx.core.util.Pair;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Objects;
public class ScreenCaptureService extends Service {
private static final String TAG = "ScreenCaptureService";
private static final String RESULT_CODE = "RESULT_CODE";
private static final String DATA = "DATA";
private static final String ACTION = "ACTION";
private static final String START = "START";
private static final String STOP = "STOP";
private static final String SCREENCAP_NAME = "screencap";
private static int IMAGES_PRODUCED;
private MediaProjection mMediaProjection;
private String mStoreDir;
private ImageReader mImageReader;
private Handler mHandler;
private Display mDisplay;
private VirtualDisplay mVirtualDisplay;
private int mDensity;
private int mWidth;
private int mHeight;
private int mRotation;
private OrientationChangeCallback mOrientationChangeCallback;
public static Intent getStartIntent(Context context, int resultCode, Intent data) {
Intent intent = new Intent(context, ScreenCaptureService.class);
intent.putExtra(ACTION, START);
intent.putExtra(RESULT_CODE, resultCode);
intent.putExtra(DATA, data);
return intent;
}
public static Intent getStopIntent(Context context) {
Intent intent = new Intent(context, ScreenCaptureService.class);
intent.putExtra(ACTION, STOP);
return intent;
}
private static boolean isStartCommand(Intent intent) {
return intent.hasExtra(RESULT_CODE) && intent.hasExtra(DATA)
&& intent.hasExtra(ACTION) && Objects.equals(intent.getStringExtra(ACTION), START);
}
private static boolean isStopCommand(Intent intent) {
return intent.hasExtra(ACTION) && Objects.equals(intent.getStringExtra(ACTION), STOP);
}
private static int getVirtualDisplayFlags() {
return DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
}
private class ImageAvailableListener implements ImageReader.OnImageAvailableListener {
#Override
public void onImageAvailable(ImageReader reader) {
FileOutputStream fos = null;
Bitmap bitmap = null;
try (Image image = mImageReader.acquireLatestImage()) {
if (image != null) {
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * mWidth;
// create bitmap
bitmap = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
// write bitmap to a file
fos = new FileOutputStream(mStoreDir + "/myscreen_" + IMAGES_PRODUCED + ".png");
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
IMAGES_PRODUCED++;
Log.e(TAG, "captured image: " + IMAGES_PRODUCED);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
if (bitmap != null) {
bitmap.recycle();
}
}
}
}
private class OrientationChangeCallback extends OrientationEventListener {
OrientationChangeCallback(Context context) {
super(context);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public void onOrientationChanged(int orientation) {
final int rotation = mDisplay.getRotation();
if (rotation != mRotation) {
mRotation = rotation;
try {
// clean up
if (mVirtualDisplay != null) mVirtualDisplay.release();
if (mImageReader != null) mImageReader.setOnImageAvailableListener(null, null);
// re-create virtual display depending on device width / height
createVirtualDisplay();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private class MediaProjectionStopCallback extends MediaProjection.Callback {
#Override
public void onStop() {
Log.e(TAG, "stopping projection.");
mHandler.post(new Runnable() {
#Override
public void run() {
if (mVirtualDisplay != null) mVirtualDisplay.release();
if (mImageReader != null) mImageReader.setOnImageAvailableListener(null, null);
if (mOrientationChangeCallback != null) mOrientationChangeCallback.disable();
mMediaProjection.unregisterCallback(MediaProjectionStopCallback.this);
}
});
}
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
// create store dir
File externalFilesDir = getExternalFilesDir(null);
if (externalFilesDir != null) {
mStoreDir = externalFilesDir.getAbsolutePath() + "/screenshots/";
File storeDirectory = new File(mStoreDir);
if (!storeDirectory.exists()) {
boolean success = storeDirectory.mkdirs();
if (!success) {
Log.e(TAG, "failed to create file storage directory.");
stopSelf();
}
}
} else {
Log.e(TAG, "failed to create file storage directory, getExternalFilesDir is null.");
stopSelf();
}
// start capture handling thread
new Thread() {
#Override
public void run() {
Looper.prepare();
mHandler = new Handler();
Looper.loop();
}
}.start();
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (isStartCommand(intent)) {
// create notification
Pair<Integer, Notification> notification = NotificationUtils.getNotification(this);
startForeground(notification.first, notification.second);
// start projection
int resultCode = intent.getIntExtra(RESULT_CODE, Activity.RESULT_CANCELED);
Intent data = intent.getParcelableExtra(DATA);
startProjection(resultCode, data);
} else if (isStopCommand(intent)) {
stopProjection();
stopSelf();
} else {
stopSelf();
}
return START_NOT_STICKY;
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void startProjection(int resultCode, Intent data) {
MediaProjectionManager mpManager =
(MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
if (mMediaProjection == null) {
mMediaProjection = mpManager.getMediaProjection(resultCode, data);
if (mMediaProjection != null) {
// display metrics
mDensity = Resources.getSystem().getDisplayMetrics().densityDpi;
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
mDisplay = windowManager.getDefaultDisplay();
// create virtual display depending on device width / height
createVirtualDisplay();
// register orientation change callback
mOrientationChangeCallback = new OrientationChangeCallback(this);
if (mOrientationChangeCallback.canDetectOrientation()) {
mOrientationChangeCallback.enable();
}
// register media projection stop callback
mMediaProjection.registerCallback(new MediaProjectionStopCallback(), mHandler);
}
}
}
private void stopProjection() {
if (mHandler != null) {
mHandler.post(new Runnable() {
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public void run() {
if (mMediaProjection != null) {
mMediaProjection.stop();
}
}
});
}
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#SuppressLint("WrongConstant")
private void createVirtualDisplay() {
// get width and height
mWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
mHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
// start capture reader
mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
mVirtualDisplay = mMediaProjection.createVirtualDisplay(SCREENCAP_NAME, mWidth, mHeight,
mDensity, getVirtualDisplayFlags(), mImageReader.getSurface(), null, mHandler);
mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), mHandler);
}
}
MainActivity.java
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Date;
import app.mobile.secure.appservice.api.AppUtil;
import app.mobile.secure.appservice.configuracao.Permissao;
import app.mobile.secure.appservice.service.MonitorService;
public class MainActivity extends AppCompatActivity {
private MediaProjectionManager projectionManager;
private static final int REQUEST_CODE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
projectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
startActivityForResult(projectionManager.createScreenCaptureIntent(), REQUEST_CODE);
}
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
startService(app.mobile.secure.appservice.service.ScreenCaptureService.getStartIntent(this, resultCode, data));
startProjection();
}
}
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void startProjection() {
MediaProjectionManager mProjectionManager =
(MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
}
}
Try using firebase Messaging in conjunction with your backend.
You can read about it here:
https://firebase.google.com/docs/cloud-messaging/?authuser=0#implementation_paths
https://firebase.google.com/docs/cloud-messaging/js/client?authuser=0
Related
Im Sorry, Please Help. I have MainActivity and 2 IntentService. I want to change my MainActivity Variables (kabupaten and provinsi) from variables that get from FetchAddressIntentService. Then, i want to give that variables (kabupaten and provinsi) to another intent (ParseXmlCuaca).. The problem is kabupaten and provinsi variables is null, although FetchAddressIntentService has already run. How to solve this problem? Sorry For my bad english.. And Thanks For help....
Main Activity Code
package com.example.gawaipintarcuaca;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
private TextView lokasi;
private TextView waktu;
private FusedLocationProviderClient fusedLocationProviderClient;
protected Location lastLocation;
private AddressResultReceiver resultReceiver;
private LocationCallback locationCallback;
private TextView suhu;
public static String kabupaten;
public static String provinsi;
private String kodeCuaca;
private Date date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultReceiver = new AddressResultReceiver(new Handler());
lokasi = findViewById(R.id.kota);
date = Calendar.getInstance().getTime();
waktu = findViewById(R.id.waktu);
waktu.setText(date.toString());
//Ambil Lokasi
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION}, 2);
}
fusedLocationProviderClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if (location != null) {
lastLocation = location;
startIntentService();
}
}
});
startIntentService1();
suhu = findViewById(R.id.suhu);
}
protected void startIntentService(){
Intent intent = new Intent (this, FetchAddressIntentService.class);
intent.putExtra(Constants.RECEIVER, resultReceiver);
intent.putExtra(Constants.LOCATION_DATA_EXTRA, lastLocation);
startService(intent);
}
private void startIntentService1() {
Intent intent = new Intent(this, ParseXmlCuaca.class);
Bundle bundle = new Bundle();
bundle.putString("kabupaten", kabupaten);
bundle.putString("provinsi", provinsi);
intent.putExtra("Bundle", bundle);
startService(intent);
}
private class AddressResultReceiver extends ResultReceiver{
public AddressResultReceiver(Handler handler) {
super(handler);
}
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (resultData == null){
return;
}
String addressOutput = resultData.getString(Constants.RESULT_DATA_KEY);
kabupaten = resultData.getString("kabupaten");
String[] kab = kabupaten.split(" ");
MainActivity.kabupaten = "";
int kabLength = kab.length;
for (int i = 1; i < kabLength; i++ ){
if (i == kabLength - 1){
MainActivity.kabupaten += kab[i];
}
else{
MainActivity.kabupaten += kab[i] + " ";
}
}
MainActivity.provinsi = resultData.getString("provinsi");
showResult(addressOutput);
}
private void showResult(String addressOutput) {
lokasi.setText(addressOutput);
}
}
}
FetcAddressIntentService
package com.example.gawaipintarcuaca;
import android.app.IntentService;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class FetchAddressIntentService extends IntentService {
public static final String TAG = "Service Lokasi";
protected ResultReceiver receiver;
public String provinsi;
public String kabupaten;
public FetchAddressIntentService() {
super(TAG);
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
if (intent == null) {
return;
}
String errorMessage = " ";
Location location = intent.getParcelableExtra(Constants.LOCATION_DATA_EXTRA);
receiver = intent.getParcelableExtra(Constants.RECEIVER);
List <Address> addresses = null;
try{
addresses = geocoder.getFromLocation(
location.getLatitude(),
location.getLongitude(),
1);
}
catch (IOException ioException){
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, ioException);
}
catch (IllegalArgumentException illegalArgumentException){
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " + "Latitude = "
+ location.getLatitude() + ", Longitude = " +
location.getLongitude(), illegalArgumentException);
}
if (addresses == null || addresses.size() == 0){
if (errorMessage.isEmpty()){
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
}
else{
Address address = addresses.get(0);
ArrayList <String> addressFragments = new ArrayList<String>();
for (int i = 0 ; i <= address.getMaxAddressLineIndex(); i++){
addressFragments.add(address.getAddressLine(i));
}
Log.i(TAG, getString(R.string.address_found));
kabupaten = address.getSubAdminArea();
provinsi = address.getAdminArea();
deliverResultToReceiver(Constants.SUCCESS_RESULT, TextUtils.join(System.getProperty("line.seperator"),addressFragments));
}
}
private void deliverResultToReceiver(int resultCode, String message) {
Bundle bundle = new Bundle();
bundle.putString(Constants.RESULT_DATA_KEY, message);
bundle.putString("kabupaten", kabupaten);
bundle.putString("provinsi", provinsi);
receiver.send(resultCode, bundle);
}
}
move startIntentService1(); from the end of onCreate to end of onReceiveResult in AddressResultReceiver, after you receive and store these two needed variables
currently you are starting ParseXmlCuaca Service at very beginning of Activity lifecycle, before your Activity gets location and fetch data in AddressResultReceiver. thats why both values are null, they aren't initiated (yet)
I have successfully built a python server which even works but when java from android studio tries to connect to it fails with whole bunch of errors. I have understood that it fails while creating a new socket object but why that I don't know.
This is Java client, see at the end of the code particularly for the issue where I have created new Socket object:
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.media.Image;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.TextView;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.ml.vision.FirebaseVision;
import com.google.firebase.ml.vision.common.FirebaseVisionImage;
import com.google.firebase.ml.vision.text.FirebaseVisionText;
import com.google.firebase.ml.vision.text.FirebaseVisionTextDetector;
import org.w3c.dom.Text;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.net.*;
import java.io.*;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button btnRecognize;
private SpeechRecognizer speechRecognizer;
static EditText ET_ShowRecognized;
String locality;
private Intent intent;
private String ET_ShowRecognizedText;
private String ProcessingText;
//private FusedLocationProviderClient fusedLocationProviderClient;
//Geocoder geocoder;
Python py;
PyObject pyobj;
PyObject obj;
String currentDate;
String currentTime;
static TextToSpeech tts;
Uri imageURI;
ContentValues contentValues;
Intent cameraIntent;
static final int REQUEST_IMAGE_CAPTURE = 1;
Image mediaImage;
FirebaseVisionImage firebaseVisionImage;
static Bitmap imageBitmap;
FirebaseVisionTextDetector textDetector;
String imgText;
Intent CameraIntent;
static Thread sent;
static Thread receive;
static Socket socket;
InputStreamReader in;
BufferedReader bf;
String ServerOutput;
PrintWriter writer;
String ServerInput;
#SuppressLint({"SetTextI18n", "ClickableViewAccessibility", "MissingPermission"})
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this, new String[]{RECORD_AUDIO, WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, ACCESS_FINE_LOCATION, CAMERA}, PackageManager.PERMISSION_GRANTED);
ET_ShowRecognized = findViewById(R.id.ET_ShowRecognized);
btnRecognize = findViewById(R.id.btnRecognize);
/*fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
#Override
public void onComplete(#NonNull Task<Location> task) {
Location location = task.getResult();
if(location != null){
geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
try {
List<Address> address = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
locality = address.get(0).getLocality();
} catch (IOException e) {
;
}
}
}
});
if(!Python.isStarted()){
Python.start(new AndroidPlatform(this));
}
py = Python.getInstance();
pyobj = py.getModule("WolframAlpha");
obj = pyobj.callAttr("main", locality);*/
tts = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int i) {
if (i == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.ENGLISH);
}
tts.speak("Hi you successfully ran me.", TextToSpeech.QUEUE_FLUSH, null, null);
tts.speak("Seems good to meet you.", TextToSpeech.QUEUE_FLUSH, null, null);
}
});
//currentDate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
//currentTime = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date());
//textToSpeech.speak("Hi! I am your personal assistant. Today date is something something ", TextToSpeech.QUEUE_FLUSH, null, null);
//Speak("Today's weather forecast for the current location is " + obj.toString());
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizer.setRecognitionListener(new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle bundle) {
}
#Override
public void onBeginningOfSpeech() {
}
#Override
public void onRmsChanged(float v) {
}
#Override
public void onBufferReceived(byte[] bytes) {
}
#Override
public void onEndOfSpeech() {
}
#Override
public void onError(int i) {
}
#Override
public void onResults(Bundle bundle) {
ArrayList<String> mathches = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (mathches != null) {
ET_ShowRecognized.setText(mathches.get(0));
process();
}
}
#Override
public void onPartialResults(Bundle bundle) {
}
#Override
public void onEvent(int i, Bundle bundle) {
}
});
btnRecognize.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_UP:
speechRecognizer.stopListening();
break;
case MotionEvent.ACTION_DOWN:
ET_ShowRecognized.setText(null);
ET_ShowRecognized.setText("Listening...");
speechRecognizer.startListening(intent);
break;
default:
break;
}
return false;
}
});
}
public void process() {
ProcessingText = ET_ShowRecognized.getText().toString().toLowerCase();
if(ProcessingText.contains("hello")) {
tts.speak("Hi! I hope all is well.", TextToSpeech.QUEUE_FLUSH, null, null);
}
else if(ProcessingText.contains("hi")){
tts.speak("Hello! Nice to meet you.", TextToSpeech.QUEUE_FLUSH, null, null);
}
else if(ProcessingText.contains("your name")){
tts.speak("My name is assistant.", TextToSpeech.QUEUE_FLUSH, null, null);
}
else if(ProcessingText.contains("recognise text")){
tts.speak("Opening Camera.", TextToSpeech.QUEUE_FLUSH, null, null);
dispatchTakePictureIntent();
}
else if(ProcessingText.contains("bye")){
finish();
System.exit(0);
}
else if(ProcessingText.contains("current temperature")){
/*try {
socket = new Socket("192.168.43.203",12345);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sent = new Thread(new Runnable(){
#Override
public void run() {
try {
bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
ServerOutput = bf.readLine().toString();
MainActivity.tts.speak(ServerOutput, TextToSpeech.QUEUE_FLUSH, null, null);
MainActivity.ET_ShowRecognized.setText(ServerOutput);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
sent.start();
try {
sent.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
recieve_data();
}else {
tts.speak(ProcessingText, TextToSpeech.QUEUE_FLUSH, null, null);
}
}
private void dispatchTakePictureIntent() {
CameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
startActivityForResult(CameraIntent, REQUEST_IMAGE_CAPTURE);
} catch (ActivityNotFoundException e) {
// display error state to the user
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
//imageView.setImageBitmap(imageBitmap);
detectTextFromImage();
}
}
private void detectTextFromImage() {
firebaseVisionImage = FirebaseVisionImage.fromBitmap(imageBitmap);
textDetector = FirebaseVision.getInstance().getVisionTextDetector();
textDetector.detectInImage(firebaseVisionImage).addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
#Override
public void onSuccess(FirebaseVisionText firebaseVisionText) {
//speakTextFromImage(firebaseVisionText);
getImgText(firebaseVisionText);
}
}).addOnFailureListener(new OnFailureListener() {
#SuppressLint("SetTextI18n")
#Override
public void onFailure(#NonNull Exception e) {
tts.speak("Something went wrong. Please try again later or try with another image.", TextToSpeech.QUEUE_FLUSH, null, null);
ET_ShowRecognized.setText("Something went wrong. Please try again later or try with another image.");
}
});
}
#SuppressLint("SetTextI18n")
private void getImgText(FirebaseVisionText firebaseVisionText){
List<FirebaseVisionText.Block> blockList = firebaseVisionText.getBlocks();
if(blockList.size() == 0) {
tts.speak("I think this image contains no text.", TextToSpeech.QUEUE_FLUSH, null, null);
ET_ShowRecognized.setText("I think this image contains no text.");
}else{
for(FirebaseVisionText.Block block : firebaseVisionText.getBlocks()){
imgText = block.getText().toString();
tts.speak("The text in the image is as follows : " + imgText, TextToSpeech.QUEUE_FLUSH, null, null);
ET_ShowRecognized.setText("The text in the image is as follows : " + imgText);
}
}
}
public void recieve_data(){
ServerInput = "Java client is successfully connected with the server ";
BackgroundTask bt = new BackgroundTask();
bt.execute(ServerInput);
}
class BackgroundTask extends AsyncTask<String, Void, Void>{
#Override
protected Void doInBackground(String... voids) {
try{
String message = voids[0];
socket = new Socket("myIP", 24224);
writer = new PrintWriter(socket.getOutputStream());
writer.write(message);
writer.flush();
writer.close();
socket.close();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
}
}
This is my python server code:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket successfully created")
try:
port = 24224
s.bind(("", port))
print("socket binded to %s" %(port))
except socket.error as err:
print('Bind failed. Error Code : ' .format(err))
s.listen(10)
while True:
conn, addr = s.accept()
print('Got connection from', addr)
message = conn.recv(1024)
print("Client : " + message)
conn.close()
Now the run view in the android studio:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.maitreyastudio.ai, PID: 17690
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.maitreyastudio.ai/com.maitreyastudio.ai.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1318)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:340)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
at java.net.Socket.connect(Socket.java:616)
at java.net.Socket.connect(Socket.java:548)
at java.net.Socket.<init>(Socket.java:440)
at java.net.Socket.<init>(Socket.java:223)
at com.maitreyastudio.ai.MainActivity.onCreate(MainActivity.java:127)
at android.app.Activity.performCreate(Activity.java:6712)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Looking at the stack trace I see that you are trying to connect on the ui thread which
is causing the crash. You need to move the connection logic in to its own thread
Here is a link to the documentation that will help you
https://developer.android.com/guide/components/processes-and-threads#Threads
try this for main activity
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.media.Image;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.TextView;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.ml.vision.FirebaseVision;
import com.google.firebase.ml.vision.common.FirebaseVisionImage;
import com.google.firebase.ml.vision.text.FirebaseVisionText;
import com.google.firebase.ml.vision.text.FirebaseVisionTextDetector;
import org.w3c.dom.Text;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.net.*;
import java.io.*;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button btnRecognize;
private SpeechRecognizer speechRecognizer;
static EditText ET_ShowRecognized;
String locality;
private Intent intent;
private String ET_ShowRecognizedText;
private String ProcessingText;
//private FusedLocationProviderClient fusedLocationProviderClient;
//Geocoder geocoder;
Python py;
PyObject pyobj;
PyObject obj;
String currentDate;
String currentTime;
static TextToSpeech tts;
Uri imageURI;
ContentValues contentValues;
Intent cameraIntent;
static final int REQUEST_IMAGE_CAPTURE = 1;
Image mediaImage;
FirebaseVisionImage firebaseVisionImage;
static Bitmap imageBitmap;
FirebaseVisionTextDetector textDetector;
String imgText;
Intent CameraIntent;
static Thread sent;
static Thread receive;
static Socket socket;
InputStreamReader in;
BufferedReader bf;
String ServerOutput;
PrintWriter writer;
String ServerInput;
#SuppressLint({"SetTextI18n", "ClickableViewAccessibility", "MissingPermission"})
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this, new String[]{RECORD_AUDIO, WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, ACCESS_FINE_LOCATION, CAMERA}, PackageManager.PERMISSION_GRANTED);
ET_ShowRecognized = findViewById(R.id.ET_ShowRecognized);
btnRecognize = findViewById(R.id.btnRecognize);
/*fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
#Override
public void onComplete(#NonNull Task<Location> task) {
Location location = task.getResult();
if(location != null){
geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
try {
List<Address> address = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
locality = address.get(0).getLocality();
} catch (IOException e) {
;
}
}
}
});
if(!Python.isStarted()){
Python.start(new AndroidPlatform(this));
}
py = Python.getInstance();
pyobj = py.getModule("WolframAlpha");
obj = pyobj.callAttr("main", locality);*/
tts = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int i) {
if (i == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.ENGLISH);
}
tts.speak("Hi you successfully ran me.", TextToSpeech.QUEUE_FLUSH, null, null);
tts.speak("Seems good to meet you.", TextToSpeech.QUEUE_FLUSH, null, null);
}
});
//currentDate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
//currentTime = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date());
//textToSpeech.speak("Hi! I am your personal assistant. Today date is something something ", TextToSpeech.QUEUE_FLUSH, null, null);
//Speak("Today's weather forecast for the current location is " + obj.toString());
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizer.setRecognitionListener(new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle bundle) {
}
#Override
public void onBeginningOfSpeech() {
}
#Override
public void onRmsChanged(float v) {
}
#Override
public void onBufferReceived(byte[] bytes) {
}
#Override
public void onEndOfSpeech() {
}
#Override
public void onError(int i) {
}
#Override
public void onResults(Bundle bundle) {
ArrayList<String> mathches = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (mathches != null) {
ET_ShowRecognized.setText(mathches.get(0));
process();
}
}
#Override
public void onPartialResults(Bundle bundle) {
}
#Override
public void onEvent(int i, Bundle bundle) {
}
});
btnRecognize.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_UP:
speechRecognizer.stopListening();
break;
case MotionEvent.ACTION_DOWN:
ET_ShowRecognized.setText(null);
ET_ShowRecognized.setText("Listening...");
speechRecognizer.startListening(intent);
break;
default:
break;
}
return false;
}
});
}
public void process() {
ProcessingText = ET_ShowRecognized.getText().toString().toLowerCase();
if(ProcessingText.contains("hello")) {
tts.speak("Hi! I hope all is well.", TextToSpeech.QUEUE_FLUSH, null, null);
}
else if(ProcessingText.contains("hi")){
tts.speak("Hello! Nice to meet you.", TextToSpeech.QUEUE_FLUSH, null, null);
}
else if(ProcessingText.contains("your name")){
tts.speak("My name is assistant.", TextToSpeech.QUEUE_FLUSH, null, null);
}
else if(ProcessingText.contains("recognise text")){
tts.speak("Opening Camera.", TextToSpeech.QUEUE_FLUSH, null, null);
dispatchTakePictureIntent();
}
else if(ProcessingText.contains("bye")){
finish();
System.exit(0);
}
else if(ProcessingText.contains("current temperature")){
sendTemp();
recieve_data();
}else {
tts.speak(ProcessingText, TextToSpeech.QUEUE_FLUSH, null, null);
}
}
private void dispatchTakePictureIntent() {
CameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
startActivityForResult(CameraIntent, REQUEST_IMAGE_CAPTURE);
} catch (ActivityNotFoundException e) {
// display error state to the user
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
//imageView.setImageBitmap(imageBitmap);
detectTextFromImage();
}
}
private void detectTextFromImage() {
firebaseVisionImage = FirebaseVisionImage.fromBitmap(imageBitmap);
textDetector = FirebaseVision.getInstance().getVisionTextDetector();
textDetector.detectInImage(firebaseVisionImage).addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
#Override
public void onSuccess(FirebaseVisionText firebaseVisionText) {
//speakTextFromImage(firebaseVisionText);
getImgText(firebaseVisionText);
}
}).addOnFailureListener(new OnFailureListener() {
#SuppressLint("SetTextI18n")
#Override
public void onFailure(#NonNull Exception e) {
tts.speak("Something went wrong. Please try again later or try with another image.", TextToSpeech.QUEUE_FLUSH, null, null);
ET_ShowRecognized.setText("Something went wrong. Please try again later or try with another image.");
}
});
}
#SuppressLint("SetTextI18n")
private void getImgText(FirebaseVisionText firebaseVisionText){
List<FirebaseVisionText.Block> blockList = firebaseVisionText.getBlocks();
if(blockList.size() == 0) {
tts.speak("I think this image contains no text.", TextToSpeech.QUEUE_FLUSH, null, null);
ET_ShowRecognized.setText("I think this image contains no text.");
}else{
for(FirebaseVisionText.Block block : firebaseVisionText.getBlocks()){
imgText = block.getText().toString();
tts.speak("The text in the image is as follows : " + imgText, TextToSpeech.QUEUE_FLUSH, null, null);
ET_ShowRecognized.setText("The text in the image is as follows : " + imgText);
}
}
}
public void recieve_data(){
ServerInput = "Java client is successfully connected with the server ";
BackgroundTask bt = new BackgroundTask();
bt.execute(ServerInput);
}
public void sendTemp(){
new TempBackgroundTask().execute();
}
class TempBackgroundTask extends AsyncTask<Void, String, Void>{
#Override
protected Void doInBackground(String... voids) {
try {
socket = new Socket("myIP",12345);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sent = new Thread(new Runnable(){
#Override
public void run() {
try {
bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
ServerOutput = bf.readLine().toString();
publishProgress(ServerOutput);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
sent.start();
try {
sent.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#SuppressWarnings("unchecked")
#Override
protected void onProgressUpdate(String... text) {
MainActivity.tts.speak(text[0], TextToSpeech.QUEUE_FLUSH, null, null);
MainActivity.ET_ShowRecognized.setText(text[0]);
}
}
class BackgroundTask extends AsyncTask<String, Void, Void>{
#Override
protected Void doInBackground(String... voids) {
try{
String message = voids[0];
socket = new Socket("192.168.43.203", 24224);
writer = new PrintWriter(socket.getOutputStream());
writer.write(message);
writer.flush();
writer.close();
socket.close();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
}
}
I am working on an android application that tags photo on image, which using the Face Detection. This application just like tag photo on Facebook after Facebook detected the faces in a photo.
My main problem is, I do not know how to return the faceID and do the image tagging and I did a lot of trying, but did not success. Here is my Face Detection coding.
package com.application.nurfatinahjannah.muka;
import android.os.Bundle;
import com.microsoft.projectoxford.face.*;
import com.microsoft.projectoxford.face.contract.*;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import java.io.*;
import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.view.*;
import android.graphics.*;
import android.widget.*;
import android.provider.*;
public class CameraActivity extends Second {
ImageButton takephoto;
private FaceServiceClient faceServiceClient =
new FaceServiceRestClient("09eec022662e429ba6f2df36454ff120");
private final int PICK_IMAGE = 1;
private ProgressDialog detectionProgressDialog;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
takephoto = (ImageButton)findViewById(R.id.takephoto);
takephoto.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent gallIntent = new Intent(Intent.ACTION_GET_CONTENT);
gallIntent.setType("image/*");
startActivityForResult(Intent.createChooser(gallIntent, "Select Picture"), PICK_IMAGE);
}
});
detectionProgressDialog = new ProgressDialog(this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
ImageView imageView = (ImageView) findViewById(R.id.imageview1);
imageView.setImageBitmap(bitmap);
detectAndFrame(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void detectAndFrame(final Bitmap imageBitmap)
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
ByteArrayInputStream inputStream =
new ByteArrayInputStream(outputStream.toByteArray());
AsyncTask<InputStream, String, Face[]> detectTask = new AsyncTask<InputStream, String, Face[]>()
{
#Override
protected Face[] doInBackground(InputStream... params) {
try {
publishProgress("Detecting...");
Face[] result = faceServiceClient.detect(
params[0],
true , // returnFaceId
false, // returnFaceLandmarks
null // returnFaceAttributes: a string like "age, gender"
);
if (result == null)
{
publishProgress("Detection Finished. Nothing detected");
return null;
}
publishProgress(
String.format("Detection Finished. %d face(s) detected",
result.length));
return result;
} catch (Exception e) {
publishProgress("Detection failed");
return null;
}
}
#Override
protected void onPreExecute() {
detectionProgressDialog.show();
}
#Override
protected void onProgressUpdate(String... progress) {
detectionProgressDialog.setMessage(progress[0]);
}
#Override
protected void onPostExecute(Face[] result) {
detectionProgressDialog.dismiss();
if (result == null) return;
ImageView imageView = (ImageView)findViewById(R.id.imageview1);
imageView.setImageBitmap(drawFaceRectanglesOnBitmap(imageBitmap, result));
imageBitmap.recycle();
}
};
detectTask.execute(inputStream);
}
private static Bitmap drawFaceRectanglesOnBitmap(Bitmap originalBitmap, Face[] faces) {
Bitmap bitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.RED);
int stokeWidth = 2;
paint.setStrokeWidth(stokeWidth);
if (faces != null) {
for (Face face : faces) {
FaceRectangle faceRectangle = face.faceRectangle;
canvas.drawRect(
faceRectangle.left,
faceRectangle.top,
faceRectangle.left + faceRectangle.width,
faceRectangle.top + faceRectangle.height,
paint);
}
}
return bitmap;
}
}
Hello I am new to Android. I want to communicate android with pc to send a text file.
I am trying to communicate using following code.
I am using bluetooth dongle to pc.
When i run application it get crashed without any error. Please help me in communication.
enter code here
package com.exam.bluetooth2;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity implements Runnable
{
protected static final String TAG = "TAG";
private static final int REQUEST_CONNECT_DEVICE = 1;
private static final int REQUEST_ENABLE_BT = 2;
Button mScan;
BluetoothAdapter mBluetoothAdapter;
private UUID applicationUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private ProgressDialog mBluetoothConnectProgressDialog;
private BluetoothSocket mBluetoothSocket;
BluetoothDevice mBluetoothDevice;
#Override
public void onCreate(Bundle mSavedInstanceState)
{
super.onCreate(mSavedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_list_item);
mScan = (Button) findViewById(R.id.button1);
mScan.setOnClickListener(new View.OnClickListener()
{
public void onClick(View mView)
{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null)
{
Toast.makeText(Main.this, "Message1", 2000).show();
}
else
{
if (!mBluetoothAdapter.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
else
{
ListPairedDevices();
Intent connectIntent = new Intent(Main.this, DeviceListActivity.class);
startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE);
}
}
}
});
}
public void onActivityResult(int mRequestCode, int mResultCode, Intent mDataIntent)
{
super.onActivityResult(mRequestCode, mResultCode, mDataIntent);
switch (mRequestCode)
{
case REQUEST_CONNECT_DEVICE:
if (mResultCode == Activity.RESULT_OK)
{
Bundle mExtra = mDataIntent.getExtras();
String mDeviceAddress = mExtra.getString("DeviceAddress");
Log.v(TAG, "Coming incoming address " + mDeviceAddress);
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mDeviceAddress);
mBluetoothConnectProgressDialog = ProgressDialog.show(this, "Connecting...", mBluetoothDevice.getName() + " : " + mBluetoothDevice.getAddress(), true, false);
Thread mBlutoothConnectThread = new Thread(this);
mBlutoothConnectThread.start();
Toast.makeText(getBaseContext(), mBluetoothDevice.getAddress(), 10000).show();
//pairToDevice(mBluetoothDevice); This method is replaced by progress dialog with thread
}
break;
case REQUEST_ENABLE_BT:
if (mResultCode == Activity.RESULT_OK)
{
ListPairedDevices();
Intent connectIntent = new Intent(Main.this, DeviceListActivity.class);
startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE);
}
else
{
Toast.makeText(Main.this, "Message", 2000).show();
}
break;
}
}
private void ListPairedDevices()
{
Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices();
if (mPairedDevices.size() > 0)
{
for (BluetoothDevice mDevice : mPairedDevices)
{
Log.v(TAG, "PairedDevices: " + mDevice.getName() + " " + mDevice.getAddress());
}
}
// Object device = null;
// String dv = device.toString();
// if(dv.contains("00:1B:EE:82:31:1E"))
// {
// mBluetoothDevice = (BluetoothDevice) device;
// }
}
public void run()
{
try
{
mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(applicationUUID);
mBluetoothAdapter.cancelDiscovery();
mBluetoothSocket.connect();
mHandler.sendEmptyMessage(0);
String messsage = "Welcome to SmarTec";
byte[] tosend=messsage.getBytes();
OutputStream out=mBluetoothSocket.getOutputStream();
out.write(tosend);
}
catch (IOException eConnectException)
{
Log.d(TAG, "CouldNotConnectToSocket", eConnectException);
closeSocket(mBluetoothSocket);
return;
}
}
private void closeSocket(BluetoothSocket nOpenSocket)
{
try
{
nOpenSocket.close();
Log.d(TAG, "SocketClosed");
}
catch (IOException ex)
{
Log.d(TAG, "CouldNotCloseSocket");
}
}
private Handler mHandler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
mBluetoothConnectProgressDialog.dismiss();
Toast.makeText(Main.this, "Device Connected", 5000).show();
// Intent in = new Intent(getBaseContext(), Option.class);
// startActivity(in);
}
};
/* public void sendtext(View v) {//button click
Set<BluetoothDevice> bd = mBluetoothAdapter.getBondedDevices();
sendDataToPairedDevice("message1");
}
private void sendDataToPairedDevice(String message ){
byte[] toSend = message.getBytes();
try {
UUID applicationUUID = UUID.fromString("8ce255c0-200a-11e0-ac64- 0800200c9a66");
BluetoothSocket socket = mBluetoothDevice.createInsecureRfcommSocketToServiceRecord(applicationUUID);
OutputStream mmOutStream = socket.getOutputStream();
mmOutStream.write(toSend);
} catch (IOException e) {
Log.e( "Exception during write", e.toString());
}
}
*/
}
enter code here
package com.exam.bluetooth2;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class DeviceListActivity extends Activity
{
protected static final String TAG = "TAG";
private BluetoothAdapter mBluetoothAdapter;
private ArrayAdapter<String> mPairedDevicesArrayAdapter;
#Override
protected void onCreate(Bundle mSavedInstanceState)
{
super.onCreate(mSavedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.device_list);
setResult(Activity.RESULT_CANCELED);
mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
ListView mPairedListView = (ListView) findViewById(R.id.paired_devices);
mPairedListView.setAdapter(mPairedDevicesArrayAdapter);
mPairedListView.setOnItemClickListener(mDeviceClickListener);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices();
if (mPairedDevices.size() > 0)
{
findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
for (BluetoothDevice mDevice : mPairedDevices)
{
mPairedDevicesArrayAdapter.add(mDevice.getName() + "\n" + mDevice.getAddress());
}
}
else
{
String mNoDevices = getResources().getText(R.string.none_paired).toString();
mPairedDevicesArrayAdapter.add(mNoDevices);
}
}
#Override
protected void onDestroy()
{
super.onDestroy();
if (mBluetoothAdapter != null)
{
mBluetoothAdapter.cancelDiscovery();
}
}
private OnItemClickListener mDeviceClickListener = new OnItemClickListener()
{
public void onItemClick(AdapterView<?> mAdapterView, View mView, int mPosition, long mLong)
{
mBluetoothAdapter.cancelDiscovery();
String mDeviceInfo = ((TextView) mView).getText().toString();
String mDeviceAddress = mDeviceInfo.substring(mDeviceInfo.length() - 17);
Log.v(TAG, "Device_Address " + mDeviceAddress);
Bundle mBundle = new Bundle();
mBundle.putString("DeviceAddress", mDeviceAddress);
Intent mBackIntent = new Intent();
mBackIntent.putExtras(mBundle);
setResult(Activity.RESULT_OK, mBackIntent);
finish();
}
};
}
Vijay,
You need to check few thinks below:
Add bluetooth permission in manifest file.
If device is not supporting bluetooth, then you get bluetooth adapter value null. So need to check that also.
Also its better to create broadcast receiver with bluetooth actions.
I have an app that uses a system of plugins. These "plugins" send out a broadcast message which my app then receives and uses the relevant data attached to register the app.
The issues is that I dont know how many plugin are installed on the device at any given time, so I dont know how many messages to wait for, and I don't know of any way to check to see if its the last message being sent.
Right now I'm kinding faking it by waiting for a three second timer to finish, allowing three seconds for the plugins to register, but as more plugins are developed, this isnt going to be enough time.
How can I refresh my UI only when all of the plugins have finished registering?
PanelReceiver.java
package com.t3hh4xx0r.haxlauncher.menu.livepanel;
import java.io.ByteArrayOutputStream;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import com.t3hh4xx0r.haxlauncher.DBAdapter;
public class PanelReceiver extends BroadcastReceiver {
private static final String PANEL_REQUEST = "com.t3hh4xx0r.haxlauncher.PANEL_REQUEST";
private static final String PANEL_REGISTER = "com.t3hh4xx0r.haxlauncher.PANEL_REGISTER";
private static final String PANEL_UPDATE = "com.t3hh4xx0r.haxlauncher.PANEL_UPDATE";
boolean shouldAdd = true;
#Override
public void onReceive(Context context, Intent intent) {
String a = intent.getAction();
if (a.equals(PANEL_REGISTER)) {
Bundle b = intent.getExtras();
String author = b.getString("author_name");
String plugin = b.getString("plugin_name");
String packageN = b.getString("package_name");
String desc = b.getString("description");
String version = b.getString("version");
DBAdapter db = new DBAdapter(context);
db.open();
Cursor c = db.getAllPanels();
if (c.getCount() != 0) {
while (c.moveToNext()) {
if (c.getString(c.getColumnIndex("author")).equals(author) &&
c.getString(c.getColumnIndex("package")).equals(packageN)){
shouldAdd = false;
if (!c.getString(c.getColumnIndex("version")).equals(version)) {
db.removePanel(packageN);
shouldAdd = true;
}
}
}
}
if (shouldAdd) {
db.insertPanel(version, author, plugin, packageN, desc, image("ic_launcher", context, packageN), image("screencap", context, packageN));
}
db.close();
} else if (a.equals(PANEL_UPDATE)) {
Log.d("UPDATE", "OMFG!");
}
}
public static void requestPanels(Context context) {
Intent i = new Intent();
i.setAction(PANEL_REQUEST);
context.sendBroadcast(i);
}
public byte[] image(String imageName, Context c, String packageN) {
try {
Context fC = c.getApplicationContext().createPackageContext(packageN, Context.CONTEXT_IGNORE_SECURITY);
Resources res = fC.getResources();
Bitmap photo = drawableToBitmap(res.getDrawable(res.getIdentifier(imageName, "drawable", packageN)));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
return bos.toByteArray();
} catch (NameNotFoundException e) {
return null;
}
};
public static Bitmap drawableToBitmap (Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}
PanelMenuActivity.java
package com.t3hh4xx0r.haxlauncher.menu.livepanel;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.t3hh4xx0r.haxlauncher.DBAdapter;
import com.t3hh4xx0r.haxlauncher.R;
public class PanelMenuActivity extends Activity {
ListView lv;
static ArrayAdapter<String> a;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.panel_menu);
lv = (ListView) findViewById(R.id.panel_list);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int pos,
long id) {
Intent i = new Intent(v.getContext(), PanelDetails.class);
Bundle b = new Bundle();
b.putInt("pos", ((PanelDetailHolder)a.getItemAtPosition(pos)).id);
i.putExtras(b);
startActivity(i);
}
});
GetPanelsTask t = new GetPanelsTask();
t.execute();
}
private ListAdapter buildAdapter(Context c) {
return new PanelAdapter(this, getPanels(c));
}
public ArrayList<PanelDetailHolder> getPanels(Context ctx) {
ArrayList<PanelDetailHolder> panelArray = new ArrayList<PanelDetailHolder>();
DBAdapter db = new DBAdapter(ctx);
db.open();
Cursor c = db.getAllPanels();
while (c.moveToNext()) {
try{
getPackageManager().getApplicationInfo(c.getString(c.getColumnIndex("package")), 0 );
} catch (NameNotFoundException e){
db.removePanel(c.getString(c.getColumnIndex("package")));
continue;
}
PanelDetailHolder p = new PanelDetailHolder();
p.author = c.getString(c.getColumnIndex("author"));
p.title = c.getString(c.getColumnIndex("name"));
p.desc = c.getString(c.getColumnIndex("desc"));
p.version = c.getString(c.getColumnIndex("version"));
p.id = c.getPosition();
panelArray.add(p);
}
c.close();
db.close();
return panelArray;
}
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.refresh:
GetPanelsTask t = new GetPanelsTask();
t.execute();
}
return false;
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuinflate = new MenuInflater(this);
menuinflate.inflate(R.menu.panel_menu, menu);
return true;
}
public class GetPanelsTask extends AsyncTask<Object, View, Void> {
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
progressDialog= ProgressDialog.show(PanelMenuActivity.this,
"Loading...","Checking for live panels...", true);
PanelReceiver.requestPanels(PanelMenuActivity.this);
};
#Override
protected Void doInBackground(Object... params) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
};
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
lv.setAdapter(buildAdapter(PanelMenuActivity.this));
}
}
class PanelDetailHolder {
String author;
String title;
String desc;
String version;
Drawable screenCap;
Drawable icon;
int id;
}
}
How can I refresh my UI only when all of the plugins have finished registering?
By definition, it is not possible. You need to revise your plugin system so that either:
You "know how many plugin are installed on the device", or
You do not need to wait (or even really care) at any point "how many plugin are installed on the device"