J'essaye de construire une application d'une seule page qui scanne les codes à barres avec ZXing
et me permet ensuite d'imprimer les valeurs EditText
sur une imprimante Bluetooth bon marché. Ensuite, je peux effacer les champs EditText
et répéter le processus. J'ai le processus d'impression et de compensation fonctionnant, mais j'ai du mal à ajouter les pièces ZXing
dans la classe principale. Toute aide ou conseil serait grandement apprécié. Ne tenez pas compte des symboles étranges qui ne s'affichent pas. Je n'ai aucun problème avec ceux-ci. MerciProblèmes ajoutant plusieurs valeurs onClick et onActivityResult sur Android
Voici ma classe principale:
import com.zj.printdemo.R;
import android.app.Fragment;
import android.content.Intent;
import com.zj.btsdk.BluetoothService;
import com.zj.btsdk.PrintPic;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import android.view.View.OnClickListener;
import android.util.Log;
import android.view.ViewGroup;
public class PrintDemo extends Activity {
Button btnSearch;
Button btnSendDraw;
Button btnSend;
Button btnClose;
Button clearBtn;
Button btnBarcode;
EditText edtContext;
EditText edtContextQTY;
EditText edtPrint;
private static final int REQUEST_ENABLE_BT = 2;
BluetoothService mService = null;
BluetoothDevice con_dev = null;
private static final int REQUEST_CONNECT_DEVICE = 1; //��ȡ�豸��Ϣ
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
//Button clearBtn = (Button)findViewById(R.id.btnClear);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mService = new BluetoothService(this, mHandler);
//�����������˳�����
if(mService.isAvailable() == false){
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
}
clearBtn = (Button)findViewById(R.id.btnClear);
OnClickListener ClearOnClickListener = new OnClickListener(){
@Override
public void onClick(View v) {
edtContext.setText("");
edtContextQTY.setText("");
}
};
clearBtn.setOnClickListener(ClearOnClickListener);
}
public void onStart() {
super.onStart();
//�����������
if(mService.isBTopen() == false)
{
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
try {
btnSendDraw = (Button) this.findViewById(R.id.btn_test);
btnSendDraw.setOnClickListener(new ClickEvent());
btnSearch = (Button) this.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new ClickEvent());
btnSend = (Button) this.findViewById(R.id.btnSend);
btnSend.setOnClickListener(new ClickEvent());
btnClose = (Button) this.findViewById(R.id.btnClose);
btnClose.setOnClickListener(new ClickEvent());
edtContext = (EditText) findViewById(R.id.txt_content);
edtContextQTY = (EditText) findViewById(R.id.txt_contentQTY);
btnClose.setEnabled(false);
btnSend.setEnabled(false);
btnSendDraw.setEnabled(false);
} catch (Exception ex) {
Log.e("������Ϣ",ex.getMessage());
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mService != null)
mService.stop();
mService = null;
}
class ClickEvent implements View.OnClickListener {
public void onClick(View v) {
if (v == btnSearch) {
Intent serverIntent = new Intent(PrintDemo.this,DeviceListActivity.class); //��������һ����Ļ
startActivityForResult(serverIntent,REQUEST_CONNECT_DEVICE);
} else if (v == btnSend) {
String msg = edtContext.getText().toString();
String msgQTY = edtContextQTY.getText().toString();
if(msg.length() > 0){
mService.sendMessage("ItemID:"+msg+"\n","ItemQTY:"+msgQTY+"\n");
}
} else if (v == btnClose) {
mService.stop();
} else if (v == btnSendDraw) {
String msg = "";
String lang = getString(R.string.strLang);
//printImage();
byte[] cmd = new byte[3];
cmd[0] = 0x1b;
cmd[1] = 0x21;
if((lang.compareTo("en")) == 0){
cmd[2] |= 0x10;
mService.write(cmd); //��������ģʽ
mService.sendMessage("Congratulations!\n", "GBK");
cmd[2] &= 0xEF;
mService.write(cmd); //ȡ�����ߡ�����ģʽ
msg = " You have sucessfully created communications between your device and our bluetooth printer.\n\n"
+" the company is a high-tech enterprise which specializes" +
" in R&D,manufacturing,marketing of thermal printers and barcode scanners.\n\n";
mService.sendMessage(msg,"GBK");
}else if((lang.compareTo("ch")) == 0){
cmd[2] |= 0x10;
mService.write(cmd); //��������ģʽ
mService.sendMessage("��ϲ����\n", "GBK");
cmd[2] &= 0xEF;
mService.write(cmd); //ȡ�����ߡ�����ģʽ
msg = " ���Ѿ��ɹ��������������ǵ�������ӡ����\n\n"
+ " ����˾��һ��רҵ�����з�����������������Ʊ�ݴ�ӡ��������ɨ���豸��һ��ĸ߿Ƽ���ҵ.\n\n";
mService.sendMessage(msg,"GBK");
}
}
}
}
/**
* ����һ��Handlerʵ�������ڽ���BluetoothService��ػ�������Ϣ
*/
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case BluetoothService.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothService.STATE_CONNECTED: //������
Toast.makeText(getApplicationContext(), "Connect successful",
Toast.LENGTH_SHORT).show();
btnClose.setEnabled(true);
btnSend.setEnabled(true);
btnSendDraw.setEnabled(true);
break;
case BluetoothService.STATE_CONNECTING: //��������
Log.d("��������","��������.....");
break;
case BluetoothService.STATE_LISTEN: //�������ӵĵ���
case BluetoothService.STATE_NONE:
Log.d("��������","�ȴ�����.....");
break;
}
break;
case BluetoothService.MESSAGE_CONNECTION_LOST: //�����ѶϿ�����
Toast.makeText(getApplicationContext(), "Device connection was lost",
Toast.LENGTH_SHORT).show();
btnClose.setEnabled(false);
btnSend.setEnabled(false);
btnSendDraw.setEnabled(false);
break;
case BluetoothService.MESSAGE_UNABLE_CONNECT: //�������豸
Toast.makeText(getApplicationContext(), "Unable to connect device",
Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT: //���������
if (resultCode == Activity.RESULT_OK) { //�����Ѿ���
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else { //�û������������
finish();
}
break;
case REQUEST_CONNECT_DEVICE: //��������ijһ�����豸
if (resultCode == Activity.RESULT_OK) { //�ѵ�������б��е�ij���豸��
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); //��ȡ�б������豸��mac��ַ
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
}
}
//��ӡͼ��
@SuppressLint("SdCardPath")
private void printImage() {
byte[] sendData = null;
PrintPic pg = new PrintPic();
pg.initCanvas(384);
pg.initPaint();
pg.drawImage(0, 0, "/mnt/sdcard/icon.jpg");
sendData = pg.printDraw();
mService.write(sendData); //��ӡbyte������
}
Voici le code que je suis en train de combiner à ce qui précède:
public void onClick(View v) {
if (v.getId() == R.id.btnBarcode) {
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
Aussi, voici ma mise en page principale de la page:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="match_parent" android:id="@+id/MainParent">
<Button
android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnSearch" >
</Button>
<EditText
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|top"
android:textSize="16sp"
android:hint="Item ID">
</EditText>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/txt_contentQTY"
android:hint="Item QTY"
android:inputType="number"/>
<Button
android:id="@+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnSend" >
</Button>
<Button
android:id="@+id/btn_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_test" >
</Button>
<Button
android:id="@+id/btnClose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnClose" >
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/button_barcode"
android:id="@+id/btnBarcode" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/button_clear"
android:id="@+id/btnClear" />
</LinearLayout>
problème:
Quand j'ajouter le onClick que je dois mettre en place une nouvelle OnClickListener
dans OnCreate
et quand je fais que le IntenIntegrator
a un problème (cela): IntentIntegrator scanIntegrator = new IntentIntegrator(this);
L'erreur est la IntnentIntegrator
ne peut pas être appliquée à (android anonyme .view, View.OnClickListener)
OnClickListener BarcodeOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnBarcode) {
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
}
Je ne suis pas sûr de savoir comment mettre en œuvre la deuxième partie. Puis-je combiner le onActivityResult comme ceci:
public void onActivityResult(int requestCode, int resultCode, Intent data, Intent intent) {
switch (requestCode) {
case REQUEST_ENABLE_BT: //���������
if (resultCode == Activity.RESULT_OK) { //�����Ѿ���
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else { //�û������������
finish();
}
break;
case REQUEST_CONNECT_DEVICE: //��������ijһ�����豸
if (resultCode == Activity.RESULT_OK) { //�ѵ�������б��е�ij���豸��
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); //��ȡ�б������豸��mac��ַ
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
}
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
MISE À JOUR 4/14:
Avec l'aide affichée par @Tung j'ai pu obtenir la question OnClickListener
fixe. J'ai fait quelques réglages et j'ai obtenu l'application pour compiler et ça marche.
Voici ma mise à jour OnClickListener
:
btnBarcode = (Button) findViewById(R.id.btnBarcode);
OnClickListener BarcodeOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnBarcode) {
IntentIntegrator integrator = new IntentIntegrator(PrintDemo.this);
integrator.initiateScan();
}
}
};
btnBarcode.setOnClickListener(BarcodeOnClickListener);
Nouveau problème: J'ai une question légère maintenant et je pense qu'il a à voir avec mon onActivityResult
. Lorsque je connecte l'imprimante Bluetooth, puis cliquez sur le bouton pour scanner un code à barres, je déconnecte mon imprimante Bluetooth. Voici le code que j'ai pour mon onActivityResult
:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT: //���������
if (resultCode == Activity.RESULT_OK) { //�����Ѿ���
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else { //�û������������
finish();
}
break;
case REQUEST_CONNECT_DEVICE: //��������ijһ�����豸
if (resultCode == Activity.RESULT_OK) { //�ѵ�������б��е�ij���豸��
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); //��ȡ�б������豸��mac��ַ
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
}
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
Quelle est l'erreur? –
Informations d'erreur ajoutées pour @Tung D. Nguyen – Chrono124
@ TungD.Nguyen Je vois que vous êtes très expérimenté sur ce sujet et sur ce site. Y a-t-il autre chose que je pourrais ajouter à ma question pour que ce soit plus clair afin que quelqu'un puisse m'aider? Merci également de m'avoir aidé à reformuler ma question plus tôt. – Chrono124