2013-10-22 81 views
0

我正在製作一個在線應用程序,我使用zxing library開發QR碼掃描器。現在,它將數據保存在歷史記錄中並生成自己的數據庫。如何在zxing QR碼庫中掃描後跟蹤數據?

但我想生成我自己的數據庫,並在掃描後將任何將我的數據存儲在我的表中。

但我無法做到這一點。

請給我一些方法來做到這一點。我是新來的機器人,我被卡住了。

謝謝。

+0

你知道如何讓數據庫在android系統的任何問題? –

+0

是的我知道在android – user2903521

+0

中製作數據庫那麼問題在於,將結果插入數據庫並檢索相同的結果。 –

回答

0
<FrameLayout 
         android:id="@+id/frame_scan" 
         android:layout_width="150dip" 
         android:layout_height="100dip" 
         android:layout_gravity="center_horizontal" > 

         <include layout="@layout/capture" /> 
        </FrameLayout> 

添加上述XML的代碼在你的main.xml和延伸CaptureActivity如下:

public class ScanCard extends CaptureActivity { 

@Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.scan_card); 
} 

if you using zxing as a library and extend the CaptureActivity, you will get the result as follows : 

    @Override 
    public void handleDecode(Result rawResult, Bitmap barcode) { 
     // TODO Auto-generated method stub 
     super.handleDecode(rawResult, barcode); 

     Toast.makeText(
       ScanCard.this, 
       "Results : " 
         + rawResult.getText().toString(), Toast.LENGTH_SHORT) 
       .show(); 


    } 

}

-

//從上面的方法掃描值,並且保存在數據庫中,如下所示:

MultiMediaDB mDB = new MultiMediaDB(); 
mDB.open(); 

-

你插入代碼數據庫:

說爲前:

mDB.insertMediaURL("scanned results"); 
mDB.close(); 

並且不要忘記關閉db。否則它可能會拋出非法狀態異常

以下是帶有值的簡單數據庫類。

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class MultiMediaDB { 

public static final String PHOTO_URL = "photo"; 
public static final String KEY_ID = "_id"; 
private final Context mCtx; 
private DatabaseHelper mDbHelper; 
public SQLiteDatabase mDb; 
private static final String TAG = "Multimedia DB "; 

// Database Version 
private static final int DATABASE_VERSION = 1; 
// Database Name 
private static final String DATABASE_NAME = "MultiMediaDB.db"; 
// Multimedia table name 
private static final String MUTLIMEDIA_TABLE = "MultiMediaTable"; 

private static final String CREATE_MUTLIMEDIA_TABLE = " CREATE TABLE if not exists " 
     + MUTLIMEDIA_TABLE 
     + "(" 
     + KEY_ID 
     + " integer PRIMARY KEY autoincrement," 
     + PHOTO_URL 
     + " TEXT " 
     + ")"; 

private static class DatabaseHelper extends SQLiteOpenHelper { 
    DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(CREATE_MUTLIMEDIA_TABLE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + MUTLIMEDIA_TABLE); 
     onCreate(db); 
    } 
} 

public MultiMediaDB(Context ctx) { 
    this.mCtx = ctx; 
} 

public MultiMediaDB open() throws SQLException { 
    mDbHelper = new DatabaseHelper(mCtx); 
    mDb = mDbHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    if (mDbHelper != null) { 
     mDbHelper.close(); 
    } 
} 

public SQLiteDatabase getDbName(Context ctx) { 
    return mDb; 
} 

// Delete all the candidates 
public boolean deleteAllCandidates() { 
    int doneDelete = 0; 
    doneDelete = mDb.delete(MUTLIMEDIA_TABLE, null, null); 
    Log.i(TAG, Integer.toString(doneDelete)); 
    return doneDelete > 0; 
} 

// Fetching all the candidates from the database 
public Cursor fetchAllCandidates() { 
    Cursor mCursor = mDb.query(MUTLIMEDIA_TABLE, new String[] { KEY_ID, 
      PHOTO_URL }, null, null, null, null, null); 
    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 
    return mCursor; 
} 

// Deleting single candidate 
public void deleteSingleRow(int row_id) { 
    mDb.delete(MUTLIMEDIA_TABLE, KEY_ID + " = ?", 
      new String[] { String.valueOf(row_id) }); 
} 

// Get All PHOTO_URL : 

public Cursor getAllPhotURL(String string) { 

    Cursor mCursor = null; 
    mCursor = mDb.rawQuery(" SELECT " + PHOTO_URL + " FROM " 
      + MUTLIMEDIA_TABLE, null); 
    return mCursor; 
} 

// Check the database for the photo url available in database. 

public boolean isPhotoAvailable(String photoUrl) { 
    Cursor c = mDb.rawQuery("SELECT " + PHOTO_URL + " FROM " 
      + MUTLIMEDIA_TABLE + " WHERE " + PHOTO_URL + " = " + "'" 
      + photoUrl + "'", null); 
    if (c.getCount() == 0) 
     return false; 
    else 
     return true; 
} 

public long insertMediaURL(String mPhotURL) { 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(PHOTO_URL, mPhotURL); 
    Log.d("The Values are ", " Inserted" + mPhotURL); 
    return mDb.insert(MUTLIMEDIA_TABLE, null, initialValues); 
} 
} 

讓我知道如果有

+0

實際上我的數據庫是1行1列。我只需要掃描產品ID。我想知道,在掃描任何我們想要的數據後,可以在handleDecode()方法中獲得。??..因爲我m ( – user2903521

+0

)您是將zxing的代碼作爲單獨的庫使用,還是將所有代碼包裝在應用程序中?請使用zxing掃描程序作爲單獨的庫並將庫中的captureActivity擴展,以便您可以更改 是的,HandleDecode()方法將得到掃描結果 –

+0

我使用Zxing掃描儀作爲單獨的庫。如果我想在另一個活動中獲取掃描數據而不是捕獲活動,那麼我必須做什麼那麼我還必須擴展captureActivity? – user2903521

0

你可能會得到結果,這樣在你的活動

public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); 
     if (scanResult != null) { 
     // handle scan result, Save result in database 'result.getText().toString();' 
     } 
     // else continue with any other code you need in the method 
     ... 
    }