将用户上传的图片转换成二维码并保存到数据库,急!! 1.如何将图片转换成二维码2.怎么样赋值给对象最好是代码说明 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 图片转二维码?二维码本身就是图片吧,你是想提取图片中的信息?http://qrcode.sourceforge.jp/ 楼主是想说转换成二进制吧?File f = new File("D:\\1.jpg");byte[] file = org.apache.commons.io.FileUtils.readFileToByteArray(f); 二维码 编码 、 解码 可用Google的开源项目ZXing包下载地址http://code.google.com/p/zxing/downloads/list小例子http://mazd1002.blog.163.com/blog/static/6657496520116241120197/楼主也可以搜搜相关资料。 package com.google.zxing;import java.io.File;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){ } }}偶粘出来给你,自己试试好不好用。 好的我试下,上午刚好把jar包引入了,不过转化之后的结果是啥?我不知道如何弄到页面上显示啊 zxing2.0貌似与jdk1.5不能一起使用,java.lang.UnsupportedClassVersionError: Bad version number in .class file, zxing1.7的版本不冲突吧? 二维码有很多工具可以使用zxing就是一个,网上例子狠毒奥的,自己搜罗下吧 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; } 转化成图片后我应该怎么显示到页面中去啊? java找工作 经验问题 ssh架构什么情况要用反射? 各位好!有没有J2EE的中文帮助??? 学习框架或者考SCWCD大家看什么书,推荐一下! 跪求大虾帮忙解决一个struts的问题 求一个hibernate的小例子,只要能运行,马上给分!!! java做的程序速度太慢????? 我编译一个J2EE的例子,就两句都出错,请高手来帮我看看!!!!!!!! ssh 事物 大量数据数组去重 图片转化为二维码存入数据库之后,能取出来再转化为图片显示么? struts2的action不能跳转到JSP页面
File f = new File("D:\\1.jpg");
byte[] file = org.apache.commons.io.FileUtils.readFileToByteArray(f);
包下载地址http://code.google.com/p/zxing/downloads/list小例子
http://mazd1002.blog.163.com/blog/static/6657496520116241120197/
楼主也可以搜搜相关资料。
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){
}
}}偶粘出来给你,自己试试好不好用。
好的我试下,上午刚好把jar包引入了,不过转化之后的结果是啥?我不知道如何弄到页面上显示啊
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;
} 转化成图片后我应该怎么显示到页面中去啊?