您好,登錄后才能下訂單哦!
import com.google.zxing.WriterException;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ShareActivity extends Activity {
private Button btnshare;
private Context context;
private String msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
context = ShareActivity.this;
msg = "Test share";
btnshare = (Button) findViewById(R.id.button1);
btnshare.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
shareMemo(context, msg);
}
});
}
public static void shareMemo(final Context context, final String uid) {
new AlertDialog.Builder(context)
.setTitle(context.getString(R.string.selectsharemode))
.setItems(
context.getResources()
.getStringArray(R.array.sharemode),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
if (which == 0) {
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_TEXT, uid);
// it.setType("audio
public Bitmap Create2DCode(String str) throws WriterException {
//生成二維矩陣,編碼時指定大小,不要生成了圖片以后再進行縮放,這樣會模糊導致識別失敗
qrcode = str;
BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, 300, 300);
int width = matrix.getWidth();
int height = matrix.getHeight();
//二維矩陣轉為一維像素數組,也就是一直橫著排了
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if(matrix.get(x, y)){
pixels[y * width + x] = 0xff000000;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
//通過像素數組生成bitmap,具體參考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
public Uri WriteBitmap(Bitmap bm)
{
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + qrcode + ".png";
File file = new File(filepath);
try {
if(!file.createNewFile()) {
System.out.println("File already exists");
}
} catch (IOException ex) {
System.out.println(ex);
}
try {
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Uri.fromFile(file);
}
}
//少不了權限
//strings.xml
請選擇分享模式
文字分享UID
圖片分享UID
另外需要導入core.jar庫文件
這個可從網上下載
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。