之前从网上找的代码,将图片转化为二维码保存到数据库中(数据库中的值:[B@156d401)
请问能不能写程序将二维码转化为图片呢?如果可以最好贴出代码鉴赏
请问能不能写程序将二维码转化为图片呢?如果可以最好贴出代码鉴赏
解决方案 »
- hibernate怎么更新二级缓存
- weblogic发布webservice
- 昨天我喝多了 是静态网址 怎么储存到xml里?
- jboss 下 EJB3.0 绑定出错 快疯了 救命
- 哪位大侠帮个忙啊!给个jsp+servlet+javabean的例子!简单的!
- 本人即将从一个软件培训班毕业,希望能在福州找一份工作,这是我的简历,大家看看,谢谢
- ***************高手指点迷津***************
- 散分请教:怎么在JAVA程序中调用C程序?
- Struts中文问题之写文件
- j2ee不能启动
- 使用struts2标签要哪些jar????
- 将用户上传的图片转换成二维码并保存到数据库,急!!
import java.io.IOException;
import java.util.Hashtable;import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;/**
* @author hwy
*
*/
public class TestEnDeCode { /**
*
*/
public TestEnDeCode() {
// TODO Auto-generated constructor stub
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub }
//编码
public void encode(){
try {
String str = "CN:男;COP:公司;ZW:职务";// 二维码内容
String path = "D://hwy.png";
Hashtable hints= new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "GBK");
BitMatrix byteMatrix;
byteMatrix= new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 200, 200);
File file = new File(path);
// writeToFile(byteMatrix, "png", file);
} catch (Exception e) {
e.printStackTrace();
}
}
//解码
public void decode(){
try{
Reader reader = new MultiFormatReader();
String imgPath = "D://hwy.png";
File file = new File(imgPath);
BufferedImage image;
try {
image = ImageIO.read(file);
if (image == null) {
System.out.println("Could not decode image");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable hints= new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, "GBK");
result = new MultiFormatReader().decode(bitmap,hints);
String resultStr = result.getText();
System.out.println(resultStr); } catch (IOException ioe) {
System.out.println(ioe.toString());
} catch (ReaderException re) {
System.out.println(re.toString());
} }catch(Exception ex){
}
}}网上找到例子,偶粘出来给你,自己试试好不好用。
包可以从
http://code.google.com/p/zxing/downloads/list
下载
public static byte[] imageToBytes(Image image, String format) {
BufferedImage bImage = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics bg = bImage.getGraphics();
bg.drawImage(image, 0, 0, null);
bg.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(bImage, format, out);
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
//test2_to image byte b[] = s.getBytes();//String转换为byte[]
@RemoteMethod
public Image bytesToImage(String imgStr) {
byte[] bytes = imgStr.getBytes();
Image image = Toolkit.getDefaultToolkit().createImage(bytes);
try {
MediaTracker mt = new MediaTracker(new Label());
mt.addImage(image, 0);
mt.waitForAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
return image;
} 从数据库取出数据后转化成图片,然后我该怎么样将图片显示到页面上?
<img src="d:/pic.jpg">
这时把就把图片保存在对应目录