package your.QRCode.namespace;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class QRCodeTextActivityActivity extends Activity {
/** Called when the activity is first created. */
Button btn1 = null;
Button btn2 = null;
ImageView ivImageView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);// 条形码
btn2 = (Button) findViewById(R.id.button2);// 二维码
ivImageView = (ImageView) findViewById(R.id.imageView1);
final String strconteString = "c2b0f58a6f09cafd1503c06ef08ac7aeb7ddb91a602dac145551c102143e6159e385cdc294";
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Bitmap mBitmap = null;
mBitmap = creatBarcode(QRCodeTextActivityActivity.this,
strconteString, 300, 300, true);
if (mBitmap != null) {
ivImageView.setImageBitmap(mBitmap);
}
}
});
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Bitmap mBitmap = null;
try {
if (!strconteString.equals("")) {
mBitmap = Create2DCode(strconteString);
// Bitmap bm =
// BitmapFactory.decodeResource(getResources(),
// R.drawable.diagnose1);
ivImageView.setImageBitmap(createBitmap(
mBitmap,
zoomBitmap(BitmapFactory.decodeResource(
getResources(), R.drawable.cccc), 100,100)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Bitmap Create2DCode(String str) throws WriterException {
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
hints.put(EncodeHintType.CHARACTER_SET, "GBK");
// hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, 500, 500, hints);
int width = matrix.getWidth();
int height = matrix.getHeight();
// 二维矩阵转为一维像素数组,也就是一直横着排了
int[] pixels = new int[width * height];
for (int i = 0; i < pixels.length; i++) {
pixels[i] = 0xffffffff;
}
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 File GetCodePath(String name) {
String EXTERN_PATH = null;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED) == true) {
EXTERN_PATH = android.os.Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/";
File f = new File(EXTERN_PATH);
if (!f.exists()) {
f.mkdirs();
}
}
return new File(EXTERN_PATH + name);
}
/**
* 图片两端所保留的空白的宽度
*/
private int marginW = 20;
/**
* 条形码的编码类型
*/
private BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;
/**
* 生成条形码
*
* @param context
* @param contents
* 需要生成的内容
* @param desiredWidth
* 生成条形码的宽带
* @param desiredHeight
* 生成条形码的高度
* @param displayCode
* 是否在条形码下方显示内容
* @return
*/
public Bitmap creatBarcode(Context context, String contents,
int desiredWidth, int desiredHeight, boolean displayCode) {
Bitmap ruseltBitmap = null;
if (displayCode) {
Bitmap barcodeBitmap = encodeAsBitmap(contents, barcodeFormat,
desiredWidth, desiredHeight);
Bitmap codeBitmap = creatCodeBitmap(contents, desiredWidth + 2
* marginW, desiredHeight, context);
ruseltBitmap = mixtureBitmap(barcodeBitmap, codeBitmap, new PointF(
0, desiredHeight));
} else {
ruseltBitmap = encodeAsBitmap(contents, barcodeFormat,
desiredWidth, desiredHeight);
}
return ruseltBitmap;
}
/**
* 生成显示编码的Bitmap
*
* @param contents
* @param width
* @param height
* @param context
* @return
*/
protected Bitmap creatCodeBitmap(String contents, int width, int height,
Context context) {
TextView tv = new TextView(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(layoutParams);
tv.setText(contents);
tv.setHeight(height);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setWidth(width);
tv.setDrawingCacheEnabled(true);
tv.setTextColor(Color.BLACK);
tv.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
tv.buildDrawingCache();
Bitmap bitmapCode = tv.getDrawingCache();
return bitmapCode;
}
/**
* 生成条形码的Bitmap
*
* @param contents
* 需要生成的内容
* @param format
* 编码格式
* @param desiredWidth
* @param desiredHeight
* @return
* @throws WriterException
*/
protected Bitmap encodeAsBitmap(String contents, BarcodeFormat format,
int desiredWidth, int desiredHeight) {
final int WHITE = 0xFFFFFFFF;
final int BLACK = 0xFF000000;
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = null;
try {
result = writer.encode(contents, format, desiredWidth,
desiredHeight, null);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
// All are 0, or black, by default
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* 将两个Bitmap合并成一个
*
* @param first
* @param second
* @param fromPoint
* 第二个Bitmap开始绘制的起始位置(相对于第一个Bitmap)
* @return
*/
protected Bitmap mixtureBitmap(Bitmap first, Bitmap second, PointF fromPoint) {
if (first == null || second == null || fromPoint == null) {
return null;
}
Bitmap newBitmap = Bitmap.createBitmap(
first.getWidth() + second.getWidth() + marginW,
first.getHeight() + second.getHeight(), Config.ARGB_4444);
Canvas cv = new Canvas(newBitmap);
cv.drawBitmap(first, marginW, 0, null);
cv.drawBitmap(second, fromPoint.x, fromPoint.y, null);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
return newBitmap;
}
/*** 仿微信二维码开始 ***/
// 图片剪切
public Bitmap cutBitmap(Bitmap mBitmap, Rect r, Bitmap.Config config) {
int width = r.width();
int height = r.height();
Bitmap croppedImage = Bitmap.createBitmap(width, height, config);
Canvas cvs = new Canvas(croppedImage);
Rect dr = new Rect(0, 0, width, height);
cvs.drawBitmap(mBitmap, r, dr, null);
return croppedImage;
}
/***
* 合并图片
*
* @param src
* @param watermark
* @return
*/
private Bitmap createBitmap(Bitmap src, Bitmap watermark) {
String tag = "createBitmap";
Log.d(tag, "create a new bitmap");
if (src == null) {
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
// create the new blank bitmap
Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas(newb);
// draw src into
cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src
// 在src的中间画watermark
cv.drawBitmap(watermark, w / 2 - ww / 2, h / 2 - wh / 2, null);// 设置ic_launcher的位置
// save all clip
cv.save(Canvas.ALL_SAVE_FLAG);// 保存
// store
cv.restore();// 存储
return newb;
}
/***
* 缩放图片
*
* @param src
* @param destWidth
* @param destHeigth
* @return
*/
private Bitmap zoomBitmap(Bitmap src, int destWidth, int destHeigth) {
String tag = "lessenBitmap";
if (src == null) {
return null;
}
int w = src.getWidth();// 源文件的大小
int h = src.getHeight();
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) destWidth) / w;// 宽度缩小比例
float scaleHeight = ((float) destHeigth) / h;// 高度缩小比例
Log.d(tag, "bitmap width is :" + w);
Log.d(tag, "bitmap height is :" + h);
Log.d(tag, "new width is :" + destWidth);
Log.d(tag, "new height is :" + destHeigth);
Log.d(tag, "scale width is :" + scaleWidth);
Log.d(tag, "scale height is :" + scaleHeight);
Matrix m = new Matrix();// 矩阵
m.postScale(scaleWidth, scaleHeight);// 设置矩阵比例
Bitmap resizedBitmap = Bitmap.createBitmap(src, 0, 0, w, h, m, true);// 直接按照矩阵的比例把源文件画入进行
return resizedBitmap;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#ffffff">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="条形码" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="二维码" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="/blog_article/@drawable/ic_launcher/index.html" />
</RelativeLayout>
</LinearLayout>图片美工做下处理。貌似需要做一个描边。png透明背景
源码:
http://download.csdn.net/download/fuweiping/4690213
1楼stone5485346天前 23:00顶一个针对近日媒体曝光部分运营商提供宽带与承诺严重不符的“假宽带”现象,在工信部昨日召开的新闻发布会上,工信部通信司司长张峰首次回应称,工信部将制定法案对超售宽带的违法、不合资质企业进行清理整顿。同时,政府牵头的网络测速标准已制定完毕,并将于近日公布。
张峰透露,此次制定完成的是测速标准中的第一阶段,主要用于指导测试用户端到接入端的网速。该部分已由工信部、研究院所及企业共同研究编制完成,已在全国进行了4个月的大规模基础测验并将于近日发布。而对于第二阶段用户“端到端”的网络测速标准仍在制定中。
运营商的接入速率与实际下载速率不一致,不止发生在中国。根据英国电信监管机构OFCOM的报告,该国接入速率从2009年4月到2010年5月增加了50%,但是用户感受速率仅仅提高了27%.多位互联网业内人士表示,影响用户实际下载速率的因素是多方面的,一是和接入方式有关,二是和所访问网站的带宽有关,三是和路由器、服务器的带宽有关。
相关新闻
低价恶性竞争被禁
记者昨日获悉,近日修订完成的《通信建设项目招标投标管理办法》(以下简称《办法》),将对低价恶性竞争这一通信行业的顽疾进行整治。
《办法》规定,招标项目设有标底的,招标人应当在开标时公布。标底只能作为评标的参考,不得以投标报价是否接近标底作为中标条件,也不得以投标报价超过标底上下浮动范围作为否决投标的条件。而当投标人报价明显低于其他投标报价,可能低于其成本时,投标人需要作出书面说明并提供相关证明材料,否则将被视为低于成本的恶性竞标。
据业内人士透露,在我国电信行业的多次招标中,并没有标底存在,向来是“没有最低只有更低”,而企业间的激烈竞争往往导致了低价的恶性竞标增多,损害了国内通信业的发展。“这是产业自身进行自我矫正的好现象,新管理办法的出台,恶性竞标问题有望得到缓解。”一位通信界人士称。
视频处理如果能使用OpenCL、OpenGL、omap将大量提高运算速度,简单介绍OpenCL、OpenGL 同时工作。
OpenCL和OpenGL都能用于操作GPU,但是前者主要用于通用计算,而后者主要用于图像渲染。在某些情况下,我们希望能用OpenCL计得到算图像,然后展示在显示器上。如果直接调用OpenGL的现有API,则需要把计算得到的结果通过pci-e总线传回host端内存,再由OpenGL再次通过pci-e总线传回gpu端显存。这样就造成了额外的拷贝,传输开销。事实上,可以使用OpenCL的OpenGL扩展功能,使得OpenCL和OpenGL能共享buffer,从而避免额外的传输。这种共享可以通俗的理解为是让OpenCL和OpenGL共享同一块内存的指针。
要实现这种共享需要三步:
1.OpenCL在OpenGL的上下文基础上创建上下文
//创建上下文的属性
cl_context_properties properties[] = {
CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(), //获得OpenGL上下文
CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(), //获得OpenGl设备信息
CL_CONTEXT_PLATFORM, (cl_context_properties) platform, //获得平台信息
0};
//利用刚刚创建的属性创建上下文
ctx = clCreateContext(properties, 1, &device, NULL, NULL, &err);
2.OpenCL在OpenGL内存对象的基础上创建内存对象
//以buffer为例,需要一个OpenGL的vbo才能创建共用的内存对象
cl_mem clCreateFromGLBuffer(cl_context context, cl_mem_flags flags,
GLuint vbo_desc, cl_int *err)
注意:OpenGL的内存对象必须先行创建,使用OpenCL中的image则需要OpenGL中的texture
3.共享内存对象的同步及访问
很显然OpenCL和OpenGL不能同时去访问同一个数据对象,需要加锁,解锁进行同步
//加锁
int clEnqueueAcquireGLObjects(cl_command_queue queue, cl_uint num_objects,
const cl_mem *mem_objects, cl_uint num_events_in_wait_list,
const cl_event *event_wait_list, cl_event *event)
//加锁后在该处运行openCL代码
//解锁
int clEnqueueReleaseGLObjects(cl_command_queue queue, cl_uint num_objects,
const cl_mem *mem_objects, cl_uint num_events_in_wait_list,
const cl_event *event_wait_list, cl_event *event)
做完这三步后,OpenGL即可直接使用共享的内存对象进行渲染及其他操作。
更加详细的信息请参考:Understanding OpenCL-OpenGL Interoperability
OpenCL / OpenGL Interoperation 教程及实例
1楼ad366683476天前 12:28气的我肝疼