我搜索了所有论坛和解决方案,但没有一个技巧。我有一个扫描文本并收到扫描文本并显示的应用程序。这是我获得扫描结果的文件:
@Override
public void extractData(RomaniaIdFrontRecognizer.Result result) {
super.extractData(result);
savePreferences("selected", result.getLastName());
add(R.string.PPLastName, result.getLastName());
add(R.string.PPFirstName, result.getFirstName());
add(R.string.PPIdentityCardNumber, result.getCardNumber());
add(R.string.PPSeries, result.getIdSeries());
add(R.string.PPCNP, result.getCnp());
add(R.string.PPNationality, result.getNonMRZNationality());
add(R.string.PPPlaceOfBirth, result.getPlaceOfBirth());
add(R.string.PPAddress, result.getAddress());
add(R.string.PPIssuingAuthority, result.getIssuedBy());
add(R.string.PPSex, result.getNonMRZSex());
add(R.string.PPValidFrom, result.getValidFrom());
add(R.string.PPValidUntil, result.getValidUntil());
}
private void savePreferences(String key, String value){
SharedPreferences sharedPreferences = mContext.getSharedPreferences("weightSetting", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
该部分被执行,因为如果我放吐司,消息显示。这是我要接收带有共享preference的变量的文件:
public class ResultActivity extends FragmentActivity implements
ResultFragment.IResultFragmentActivity,
FieldByFieldResultFragment.IFieldByFieldResultFragmentActivity {
String a;
String TempName, TempEmail ;
String e;
Bitmap bitmap;
ByteArrayOutputStream baos;
byte[] imageInByte;
private LayoutInflater mInflater;
Toast toast;
LoginDataBaseAdapter loginDataBaseAdapter;
EditText nume;
EditText prenume;
EditText nr;
EditText serie;
EditText cnp;
EditText nationalitate;
EditText loc;
EditText adresa;
EditText emis;
EditText sex;
EditText start;
EditText stop;
String usern;
public static final String EXTRAS_RESULT_TYPE = "EXTRAS_RESULT_TYPE";
public enum ResultType {
RECOGNIZER_BUNDLE,
FIELD_BY_FIELD_BUNDLE
}
protected ViewPager mPager;
protected RecognizerBundle mRecognizerBundle;
protected FieldByFieldBundle mFieldByFieldBundle;
protected ResultType mResultType;
private ArrayList<Recognizer> mRecognizersWithResult;
@SuppressLint("InlinedApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setActivityContentView();
// loginDataBaseAdapter=new LoginDataBaseAdapter(getApplicationContext());
// loginDataBaseAdapter=loginDataBaseAdapter.open();
// String aa=loginDataBaseAdapter.getMagazin();
String s = loadPreferences("selected");
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_LONG).show();
Intent intent = getIntent();
mResultType = (ResultType) intent.getSerializableExtra(EXTRAS_RESULT_TYPE);
mRecognizerBundle = new RecognizerBundle();
mFieldByFieldBundle = new FieldByFieldBundle();
if (mResultType == null) {
if (mRecognizerBundle.existsInIntent(intent)) {
mResultType = ResultType.RECOGNIZER_BUNDLE;
} else if (mFieldByFieldBundle.existsInIntent(intent)) {
mResultType = ResultType.FIELD_BY_FIELD_BUNDLE;
}
}
if (mResultType == null) {
throw new IllegalStateException("Results must be passed to ResultActivity!");
}
mPager = findViewById(R.id.resultPager);
switch (mResultType) {
case RECOGNIZER_BUNDLE:
mRecognizersWithResult = new ArrayList<>();
mRecognizerBundle.loadFromIntent(intent);
for ( Recognizer< Recognizer, Recognizer.Result > r : mRecognizerBundle.getRecognizers() ) {
if ( r.getResult().getResultState() != Recognizer.Result.State.Empty ) {
mRecognizersWithResult.add( r );
// String text = mRecognizersWithResult.get(0).toString();
// Toast.makeText(getApplicationContext(), text,Toast.LENGTH_LONG).show();
}
}
mPager.setAdapter(new RecognizerListFragmentAdapter(getSupportFragmentManager()));
break;
case FIELD_BY_FIELD_BUNDLE:
// mFieldByFieldBundle.loadFromIntent(intent);
// mPager.setAdapter(new FieldByFieldBundleFragmentAdapter(getSupportFragmentManager()));
// break;
}
TabPageIndicator indicator = findViewById(R.id.resultIndicator);
indicator.setViewPager(mPager);
indicator.setClipChildren(false);
}
@Override
protected void onResume() {
super.onResume();
// clear saved state to be sure that data is cleared from cache and from file when
// intent optimisation is used
mRecognizerBundle.clearSavedState();
mFieldByFieldBundle.clearSavedState();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mResultType == ResultType.RECOGNIZER_BUNDLE) {
mRecognizerBundle.saveState();
} else if (mResultType == ResultType.FIELD_BY_FIELD_BUNDLE) {
mFieldByFieldBundle.saveState();
}
}
public void setActivityContentView() {
setContentView(R.layout.result_menu);
}
private String loadPreferences(String key){
SharedPreferences sharedPreferences =getApplicationContext().getSharedPreferences("weightSetting", MODE_PRIVATE);
String load = sharedPreferences.getString(key, "");
return load;
}
@Override
public Recognizer< Recognizer, Recognizer.Result > getRecognizerAtPosition(int resultPosition) {
if (resultPosition < 0 || resultPosition >= mRecognizersWithResult.size()) {
throw new IllegalStateException("Recognizer with non empty result on requested position"
+ " does not exist. Possible cause is that recognizer bundle state has been lost"
+ " in intent transactions.");
}
//noinspection unchecked
return mRecognizersWithResult.get(resultPosition);
}
@Override
public FieldByFieldBundle getFieldByFieldBundle() {
return mFieldByFieldBundle;
}
private class RecognizerListFragmentAdapter extends FragmentPagerAdapter {
RecognizerListFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return ResultFragment.newInstance(position);
}
@Override
public int getCount() {
return mRecognizersWithResult.size();
}
@Override
public CharSequence getPageTitle(int position) {
return ResultUtils.getRecognizerSimpleName(mRecognizersWithResult.get(position));
}
}
private class FieldByFieldBundleFragmentAdapter extends FragmentPagerAdapter {
FieldByFieldBundleFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return FieldByFieldResultFragment.newInstance();
}
@Override
public int getCount() {
return 1;
}
@Override
public CharSequence getPageTitle(int position) {
return ResultActivity.this.getString(R.string.title_field_by_field_results);
}
}
public void footerButtonClickHandler(View view) {
// String sessionId= getIntent().getStringExtra("test");
// Toast.makeText(getApplicationContext(), "da", Toast.LENGTH_LONG).show();
// new RegisterAsyntaskNew().execute();
ImageView img=(ImageView) findViewById(R.id.resultValueee);
// img.buildDrawingCache();
// Bitmap bitmap = img.getDrawingCache();
BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
Bitmap bitmap = drawable.getBitmap();
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageInByte = baos.toByteArray();
// RomanianIDFrontSideRecognitionResultExtractor myActivity2 = new RomanianIDFrontSideRecognitionResultExtractor();
// myActivity2.InsertData();
Img(imageInByte,e);
// InsertData(nume.getText().toString(), prenume.getText().toString(), nr.getText().toString(), serie.getText().toString(),cnp.getText().toString(),nationalitate.getText().toString(), loc.getText().toString(),adresa.getText().toString(),emis.getText().toString(),sex.getText().toString(),start.getText().toString(),stop.getText().toString(),usern);
finish();
}
public void InsertData(final String nume, final String prenume, final String nr, final String serie, final String cnp, final String nationalitate
, final String locn, final String adresa, final String emis, final String sex, final String start, final String sfarsit,final String username){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String name = nume ;
String pren = prenume ;
String nr1 = nr;
String serie1 = serie ;
String cnp1 = cnp ;
String nat = nationalitate ;
String locn1 = locn ;
String adresa1 = adresa ;
String emis1 = emis ;
String sex1 = sex ;
String start1 = start ;
String sfarsit1 = sfarsit ;
String user1 = username ;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("email", pren));
nameValuePairs.add(new BasicNameValuePair("nr", nr1));
nameValuePairs.add(new BasicNameValuePair("serie", serie1));
nameValuePairs.add(new BasicNameValuePair("cnp", cnp1));
nameValuePairs.add(new BasicNameValuePair("nat", nat));
nameValuePairs.add(new BasicNameValuePair("locn", locn1));
nameValuePairs.add(new BasicNameValuePair("adresa", adresa1));
nameValuePairs.add(new BasicNameValuePair("emis", emis1));
nameValuePairs.add(new BasicNameValuePair("sex", sex1));
nameValuePairs.add(new BasicNameValuePair("start", start1));
nameValuePairs.add(new BasicNameValuePair("sfarsit", sfarsit1));
nameValuePairs.add(new BasicNameValuePair("username", user1));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("url");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
a = EntityUtils.toString(httpResponse.getEntity());
// return EntityUtils.toString(httpEntity).trim();
ResultActivity.this.runOnUiThread(new Runnable() {
public void run() {
// Toast.makeText(ResultActivity.this, a, Toast.LENGTH_LONG).show();
toast = Toast.makeText(ResultActivity.this, a, Toast.LENGTH_SHORT);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
v.setTextColor(Color.parseColor("#66ff33"));
int toastDuration = 5000;
CountDownTimer countDownTimer;
countDownTimer = new CountDownTimer(toastDuration, 1000) {
public void onTick(long millisUntilFinished) {
toast.show();
}
public void onFinish() {
toast.cancel();
}
};
toast.show();
countDownTimer.start();
}
});
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "Data Inserted Successfully";
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(nume, prenume,nr,serie,cnp,nationalitate,locn,adresa,emis,sex,start,sfarsit,username);
}
public void Img(final byte[] data,final String cnpimg){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String cnp1 = cnpimg ;
Bitmap bitmapOrg = BitmapFactory.decodeByteArray(data, 0, data.length);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
String ba1 = Base64.encodeBytes(ba);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image", ba1));
nameValuePairs.add(new BasicNameValuePair("cnp", cnp1));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://promotii.grupsapte.ro/php/scanid/img.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// a = EntityUtils.toString(httpResponse.getEntity());
// return EntityUtils.toString(httpEntity).trim();
// ResultActivity.this.runOnUiThread(new Runnable() {
// public void run() {
// Toast.makeText(ResultActivity.this, a, Toast.LENGTH_LONG).show();
// }
// });
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "Data Inserted Successfully";
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute();
}
}
带有共享首选项的发送接收变量可行,但是现在,我不知道原因,第一次获得空变量,如果我再次尝试,那么我先收到我先扫描的内容。因此,该变量仅在第一次使用值扫描的第二次显示。即使我尝试放置一个简单的文本并发送它,它也只会显示我第二次采取行动。但是我重复一遍,文件将被执行,因为如果我放烤面包,它将显示。我认为首先完成了共享首选项变量的接收,然后执行第一个文件中的代码。我试图将代码放入ongreate,on Resume中,但没有任何技巧。任何帮助将不胜感激。
我的解决方案是在接收索格(Thorugh)共享偏好的过程中延迟1秒钟。请参阅下面:
@Override
protected void onResume() {
super.onResume();
// clear saved state to be sure that data is cleared from cache and from file when
// intent optimisation is used
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// Your logic here...
// When you need to modify a UI element, do so on the UI thread.
// 'getActivity()' is required as this is being ran from a Fragment.
ResultActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// This code will always run on the UI thread, therefore is safe to modify UI elements.
String a = loadPreferences("nume");
String b= loadPreferences("prenume");
String c= loadPreferences("nr");
String d= loadPreferences("serie");
e= loadPreferences("cnp");
String f= loadPreferences("nationalitate");
String g= loadPreferences("locn");
String h= loadPreferences("adresa");
String i= loadPreferences("emis");
String j= loadPreferences("sex");
String k= loadPreferences("start");
String l= loadPreferences("sfarsit");
usern= "";
String s = Normalizer.normalize(f, Normalizer.Form.NFD);
String bla = s.replaceAll("[\p{InCombiningDiacriticalMarks}]", "");
String t = Normalizer.normalize(h, Normalizer.Form.NFD);
String bla1 = t.replaceAll("[\p{InCombiningDiacriticalMarks}]", "");
String a1 = Normalizer.normalize(a, Normalizer.Form.NFD);
String bla2 = a1.replaceAll("[\p{InCombiningDiacriticalMarks}]", "");
String b1 = Normalizer.normalize(b, Normalizer.Form.NFD);
String bla3 = b1.replaceAll("[\p{InCombiningDiacriticalMarks}]", "");
String g1 = Normalizer.normalize(g, Normalizer.Form.NFD);
String bla4 = g1.replaceAll("[\p{InCombiningDiacriticalMarks}]", "");
nume = (EditText) findViewById(R.id.nume);
nume.setText(bla2);
prenume = (EditText) findViewById(R.id.prenume);
prenume.setText(bla3);
nr = (EditText) findViewById(R.id.nrserie);
nr.setText(c);
serie = (EditText) findViewById(R.id.serie);
serie.setText(d);
cnp = (EditText) findViewById(R.id.cnp);
cnp.setText(e);
nationalitate = (EditText) findViewById(R.id.nationalitate);
nationalitate.setText(bla);
loc = (EditText) findViewById(R.id.loc);
loc.setText(bla4);
adresa = (EditText) findViewById(R.id.adresa);
adresa.setText(bla1);
emis = (EditText) findViewById(R.id.emis);
emis.setText(i);
sex = (EditText) findViewById(R.id.sexx);
sex.setText(j);
start = (EditText) findViewById(R.id.start);
start.setText(k);
stop = (EditText) findViewById(R.id.stop);
stop.setText(l);
// String s = loadPreferences("selected");
// Toast.makeText(getApplicationContext(), a,Toast.LENGTH_LONG).show();
}
});
}
}, 1000); // End of your timer code.
mRecognizerBundle.clearSavedState();
mFieldByFieldBundle.clearSavedState();
}