80,351
社区成员
发帖
与我相关
我的任务
分享
public static Bitmap toBitmap(BitMatrix byteMatrix)
{
// 定义位图的宽和高
int width = byteMatrix.getWidth();
int height = byteMatrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (byteMatrix.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 class MainActivity extends Activity {
ImageView im;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
}
@Override
protected void onResume() {
super.onResume();
im=(ImageView)findViewById(R.id.imageView1);
im.setImageBitmap(getBarcode("123654789632145874",300,300));
}
private static final String CODE = "utf-8";
public static Bitmap getBarcode(String str, Integer width,
Integer height)
{
if (width == null || width < 200)
{
width = 200;
}
if (height == null || height < 50)
{
height = 50;
}
try
{
// 文字编码
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, CODE);
BitMatrix bitMatrix = new MultiFormatWriter().encode(str,
BarcodeFormat.CODE_128, width, height, hints);
return BitMatrixToBitmap(bitMatrix);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
/**
* BitMatrix转换成Bitmap
* @param matrix
* @return
*/
private static Bitmap BitMatrixToBitmap(BitMatrix matrix) {
final int WHITE = 0xFFFFFFFF;
final int BLACK = 0xFF000000;
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
}
}
return createBitmap(width, height, pixels);
}
/**
* 生成Bitmap
* @param width
* @param height
* @param pixels
* @return
*/
private static Bitmap createBitmap(int width, int height, int[] pixels) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
}
public static Bitmap a(String paramString, com.a.a.a parama, int paramInt1, int paramInt2, int paramInt3)
{
Log.d(a, "getBitmap(" + paramString + ", " + paramInt1 + ") BEGIN");
int i1;
try
{
e locale = new e();
Hashtable localHashtable = new Hashtable();
localHashtable.put(c.b, "UTF-8");
b localb = locale.a(paramString, parama, paramInt2, paramInt1, localHashtable);
int i = localb.b();
int j = localb.c();
Object localObject2 = Bitmap.createBitmap(i, j, Bitmap.Config.ARGB_4444);
int k = 0;
if (k >= i)
{
if (com.a.a.a.f == parama)
{
Bitmap localBitmap = Bitmap.createScaledBitmap((Bitmap)localObject2, paramInt2, paramInt1, false);
localObject2 = localBitmap;
}
return localObject2;
}
for (int l = 0; ; ++l)
{
if (l >= j)
++k;
if (!localb.a(k, l))
break;
i1 = -16777216;
((Bitmap)localObject2).setPixel(k, l, i1);
}
}
catch (Exception localException)
{
}
finally
{
Log.d(a, "getBitmap END");
}
}
public class MainActivity extends Activity {
ImageView im;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
}
@Override
protected void onResume() {
super.onResume();
im=(ImageView)findViewById(R.id.imageView1);
im.setImageBitmap(getBarcode("123654789632145874",300,300));
}
private static final String CODE = "utf-8";
public static Bitmap getBarcode(String str, Integer width,
Integer height)
{
if (width == null || width < 200)
{
width = 200;
}
if (height == null || height < 50)
{
height = 50;
}
try
{
// 文字编码
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, CODE);
BitMatrix bitMatrix = new MultiFormatWriter().encode(str,
BarcodeFormat.CODE_128, width, height, hints);
return BitMatrixToBitmap(bitMatrix);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
/**
* BitMatrix转换成Bitmap
* @param matrix
* @return
*/
private static Bitmap BitMatrixToBitmap(BitMatrix matrix) {
final int WHITE = 0xFFFFFFFF;
final int BLACK = 0xFF000000;
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
}
}
return createBitmap(width, height, pixels);
}
/**
* 生成Bitmap
* @param width
* @param height
* @param pixels
* @return
*/
private static Bitmap createBitmap(int width, int height, int[] pixels) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
}
[/quote]