AffineTransform a1 = new pc studio 1.5AffineTransform(1, 0, 0, -1,...

抛体运动问题,45度斜抛和垂直上抛 - cangyingzhijia的专栏
- 博客频道 - CSDN.NET
&import java.awt.C
import java.awt.G
import java.awt.Graphics2D;
import java.awt.P
import java.awt.event.WindowA
import java.awt.event.WindowE
import java.awt.geom.AffineT
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.ArrayL
import java.util.L
import javax.swing.JF
import javax.swing.JP
public class XYThrowMain {
&& &static class XYThrow extends JPanel {
&& &&& &double x1, y1; // 垂直上抛坐标
&& &&& &double x21, y21, x22, y22; // 斜抛坐标
&& &&& &double v01 = 6000;
&& &&& &boolean stop = //停止标志
&& &&& &int t = 0;
&& &&& &double v02 = 100; //斜抛出去的初速度
&& &&& &double g = 9.8; //重力加速度
&& &&& &double xscale = 300; //为了方便作图而做的缩小变换
&& &&& &double yscale = 20000;
&& &&& &private void setAffine(Graphics2D g) {
&& &&& &&& &AffineTransform a1 = new AffineTransform(1, 0, 0, -1, 50, 100);
&& &&& &&& &g.setTransform(a1);
&& &&& &@Override
&& &&& &public void paint(Graphics g1) {
&& &&& &&& &super.paint(g1);
&& &&& &&& &//System.out.println(x21 + & & + y21 + & & + x22 + & & + y22);
&& &&& &&& &Graphics2D g = (Graphics2D) g1;
&& &&& &&& &setAffine(g);
&& &&& &&& &g.setPaint(Color.white);
&& &&& &&& &coordinate(g);
&& &&& &&& &paintV(g);
&& &&& &&& &paintTow(g);
&& &&& &private void coordinate(Graphics2D g) {
&& &&& &&& &g.drawLine(0, -900, 0, 100); // y
&& &&& &&& &g.drawLine(-100, 0, 900, 0);// x
&& &&& &&& &Paint tmp = g.getPaint();
&& &&& &&& &g.setPaint(Color.red);
&& &&& &&& &g.drawString(&0&, -10, -5); //坐标原点
&& &&& &&& &g.drawString(&x&, 700, 10);
&& &&& &&& &g.setPaint(Color.black);& //画一条基准线,方便观察
&& &&& &&& &g.drawLine(20, -900, 20, 100);
&& &&& &&& &g.setPaint(tmp);&& &&& &&& &
&& &&& &public void start() throws InterruptedException {
&& &&& &&& &new Thread(new VerticalThrow()).start();
&& &&& &&& &new Thread(new ParabolaThrow()).start();
&& &&& &&& &for (int i = 0; i & 250; i++) {
&& &&& &&& &&& &t += 10; //模拟时间进度
&& &&& &&& &&& &Thread.sleep(100);
&& &&& &&& &&& &repaint();
&& &&& &&& &}
&& &&& &private List&Line2D& towList = new ArrayList&Line2D&();
&& &&& &private void paintTow(Graphics2D g) {
&& &&& &&& &g.setPaint(Color.red);
&& &&& &&& &towList.add(new Line2D.Double(x21 / xscale, y21 / yscale, x22
&& &&& &&& &&& &&& &/ xscale, y22 / yscale));
&& &&& &&& &for (Line2D l : towList) {
&& &&& &&& &&& &g.draw(l);
&& &&& &&& &}
&& &&& &private void paintV(Graphics2D g) {
&& &&& &&& &Paint tmp = g.getPaint();
&& &&& &&& &g.setPaint(Color.red);
&& &&& &&& &g.fill(new Ellipse2D.Double(x1,y1/yscale,3,3));
&& &&& &&& &g.setPaint(tmp);
&& &&& &@Override
&& &&& &public void update(Graphics g) {
&& &&& &&& &paint(g);
&& &&& &class VerticalThrow implements Runnable {
&& &&& &&& &@Override
&& &&& &&& &public void run() {
&& &&& &&& &&& &while (!stop) {
&& &&& &&& &&& &&& &x1 = 20;
&& &&& &&& &&& &&& &y1 = v01 * t - 0.5 * g * Math.pow(t, 2);
&& &&& &&& &&& &&& &try {
&& &&& &&& &&& &&& &&& &Thread.sleep(100);
&& &&& &&& &&& &&& &} catch (InterruptedException e) {
&& &&& &&& &&& &&& &&& &// TODO Auto-generated catch block
&& &&& &&& &&& &&& &&& &e.printStackTrace();
&& &&& &&& &&& &&& &}
&& &&& &&& &&& &}
&& &&& &&& &}
&& &&& &class ParabolaThrow& implements Runnable {
&& &&& &&& &@Override
&& &&& &&& &public void run() {
&& &&& &&& &&& &while (!stop) {
&& &&& &&& &&& &&& &x21 = x22;
&& &&& &&& &&& &&& &y21 = y22;
&& &&& &&& &&& &&& &x22 = v02 * t *Math.cos(45);
&& &&& &&& &&& &&& &y22 = v02 * t * Math.sin(45) - 0.5 * g * Math.pow(t, 2);
&& &&& &&& &&& &&& &try {
&& &&& &&& &&& &&& &&& &Thread.sleep(200);
&& &&& &&& &&& &&& &} catch (InterruptedException e) {
&& &&& &&& &&& &&& &&& &// TODO Auto-generated catch block
&& &&& &&& &&& &&& &&& &e.printStackTrace();
&& &&& &&& &&& &&& &}
&& &&& &&& &&& &}
&& &&& &&& &}
&& & * @param args
&& & * @throws InterruptedException
&& &public static void main(String[] args) throws InterruptedException {
&& &&& &final JFrame jframe = new JFrame();
&& &&& &jframe.addWindowListener(new WindowAdapter(){
&& &&& &&& &@Override
&& &&& &&& &public void windowClosed(WindowEvent e) {
&& &&& &&& &&& &jframe.dispose();
&& &&& &&& &}
&& &&& &&& &
&& &&& &});
&& &&& &XYThrow jpanel = new XYThrow();
&& &&& &jframe.add(jpanel);
&& &&& &jframe.setBounds(50, 50, 900, 800);
&& &&& &jframe.setVisible(true);
&& &&& &jpanel.start();
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:47780次
排名:千里之外
原创:21篇
转载:18篇
评论:13条
(1)(3)(7)(2)(3)(2)(1)(3)(3)(5)(2)(7)异常:Width (-1) and height (-1) cannot be &= 0
[问题点数:20分,结帖人tuquanren2]
异常:Width (-1) and height (-1) cannot be &= 0
[问题点数:20分,结帖人tuquanren2]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
相关帖子推荐:
2009年11月 Java大版内专家分月排行榜第三
2009年10月 Java大版内专家分月排行榜第一
2009年11月 Java大版内专家分月排行榜第二
2009年10月 Java大版内专家分月排行榜第一
2009年11月 Java大版内专家分月排行榜第二
本帖子已过去太久远了,不再提供回复功能。j2se图片旋转翻转 - 千山万水_ - 博客园
随笔 - 132, 文章 - 0, 评论 - 8, 引用 - 0
* 水平翻转图像
* @param img
public Image flipImage_H(Image img)
int width = img.getWidth(null);
int height = img.getHeight(null);
BufferedImage newImg = new BufferedImage(width, height, 1);
Graphics g = newImg.getGraphics();
g.drawImage(img, 0, 0, width, height, width, 0, 0, height, null);
g.dispose();
return newI
* 竖直翻转图像
* @param img
public Image flipImage_V(Image img)
int width = img.getWidth(null);
int height = img.getHeight(null);
BufferedImage newImg = new BufferedImage(width, height, 1);
Graphics g = newImg.getGraphics();
Graphics2D g2d = (Graphics2D)
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN, 0.1f));
g2d.drawImage(img, 0, 0, width, height, 0, height, width, 0, null);
g2d.dispose();
return newI
* 顺时针转90度
* @param img
public Image getImage_rote90(Image img)
int width = img.getWidth(null);
int height = img.getHeight(null);
BufferedImage newImg = new BufferedImage(height, width, 1);
Graphics g = newImg.getGraphics();
Graphics2D g2d = (Graphics2D)
g2d.rotate(Math.toRadians(90), height, 0);
g2d.drawImage(img, height, 0, width, height,
g2d.dispose();
return newI
* 顺时针转180
* @param img
public Image getImage_rote180(Image img)
int width = img.getWidth(null);
int height = img.getHeight(null);
BufferedImage newImg = new BufferedImage(width, height, 1);
Graphics g = newImg.getGraphics();
g.drawImage(img, 0, 0, width, height, width, height, 0, 0, null);
g.dispose();
return newI
* 顺时针转270
* @param img
public Image getImage_rote270(Image img)
int width = img.getWidth(null);
int height = img.getHeight(null);
BufferedImage newImg = new BufferedImage(height, width, 1);
Graphics g = newImg.getGraphics();
Graphics2D g2d = (Graphics2D)
g2d.rotate(Math.toRadians(-90), 0, 0);
g2d.drawImage(img, -width, 0, width, height,
g2d.dispose();
return newI
* 关于y=x翻转
* @param srcImg
public Image getImage_FLIP_YISX(Image srcImg)
int width = srcImg.getWidth(null);
int height = srcImg.getHeight(null);
BufferedImage result = new BufferedImage(height, width, 1);
Graphics g = result.getGraphics();
Graphics2D g2d = (Graphics2D)
AffineTransform affineTransform = new AffineTransform(0, -1, -1, 0, height, width);
AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
affineTransformOp.filter(getClone(srcImg), result);
g2d.dispose();
* 关于y=-x翻转
* @param srcImg
public Image getImage_FLIP_YISFUX(Image srcImg)
int width = srcImg.getWidth(null);
int height = srcImg.getHeight(null);
BufferedImage result = new BufferedImage(height, width, 1);
Graphics g = result.getGraphics();
Graphics2D g2d = (Graphics2D)
AffineTransform affineTransform = new AffineTransform(0, 1, 1, 0, 0, 0);
AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
affineTransformOp.filter(getClone(srcImg), result);
g2d.dispose();
* 得到克隆图像
* @param srcImg
public BufferedImage getClone(Image srcImg)
int width = srcImg.getWidth(null);
int height = srcImg.getHeight(null);
BufferedImage result = new BufferedImage(width, height, 1);
Graphics g = result.getGraphics();
g.drawImage(srcImg, 0, 0, width, height, 0, 0, width, height,null);
g.dispose();一旦删除此代码片,将不能恢复!
来自CSDN博客:java二维码工具类,中间带LOGO的,很强大 .
blog__5935031
package com.util.cccm;import java.awt.BasicStroke;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Shape;import java.awt.geom.RoundRectangle2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.OutputStream;import java.util.Hashtable;import java.util.Random;import javax.imageio.ImageIO;import com.google.zxing.BarcodeFormat;import com.google.zxing.BinaryBitmap;import com.google.zxing.DecodeHintType;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatReader;import com.google.zxing.MultiFormatWriter;import com.google.zxing.Result;import com.mon.BitMatrix;import com.mon.HybridBinarizer;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;/** * 二维码工具类 *
*/public class QRCodeUtil { private static final String CHARSET = &utf-8&; private static final String FORMAT_NAME = &JPG&; // 二维码尺寸 private static final int QRCODE_SIZE = 300; // LOGO宽度 private static final int WIDTH = 60; // LOGO高度 private static final int HEIGHT = 60; private static BufferedImage createImage(String content, String imgPath,
boolean needCompress) throws Exception {
Hashtable&EncodeHintType, Object& hints = new Hashtable&EncodeHintType, Object&();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x & width; x++) {
for (int y = 0; y & height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
: 0xFFFFFFFF);
if (imgPath == null || &&.equals(imgPath)) {
return image;
// 插入图片
QRCodeUtil.insertImage(image, imgPath, needCompress);
return image; } /**
* 插入LOGO
* @param source
二维码图片
* @param imgPath
LOGO图片地址
* @param needCompress
* @throws Exception
*/ private static void insertImage(BufferedImage source, String imgPath,
boolean needCompress) throws Exception {
File file = new File(imgPath);
if (!file.exists()) {
System.err.println(&&+imgPath+&
该文件不存在!&);
Image src = ImageIO.read(new File(imgPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width & WIDTH) {
width = WIDTH;
if (height & HEIGHT) {
height = HEIGHT;
Image image = src.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose(); } /**
* 生成二维码(内嵌LOGO)
* @param content
* @param imgPath
* @param destPath
* @param needCompress
是否压缩LOGO
* @throws Exception
*/ public static void encode(String content, String imgPath, String destPath,
boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath,
needCompress);
mkdirs(destPath);
String file = new Random().nextInt()+&.jpg&;
ImageIO.write(image, FORMAT_NAME, new File(destPath+&/&+file)); } /**
* 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
* @author lanyuan
上午10:16:36
* @param destPath 存放目录
*/ public static void mkdirs(String destPath) {
File file =new File(destPath);
//当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
* 生成二维码(内嵌LOGO)
* @param content
* @param imgPath
* @param destPath
* @throws Exception
*/ public static void encode(String content, String imgPath, String destPath)
throws Exception {
QRCodeUtil.encode(content, imgPath, destPath, false); } /**
* 生成二维码
* @param content
* @param destPath
* @param needCompress
是否压缩LOGO
* @throws Exception
*/ public static void encode(String content, String destPath,
boolean needCompress) throws Exception {
QRCodeUtil.encode(content, null, destPath, needCompress); } /**
* 生成二维码
* @param content
* @param destPath
* @throws Exception
*/ public static void encode(String content, String destPath) throws Exception {
QRCodeUtil.encode(content, null, destPath, false); } /**
* 生成二维码(内嵌LOGO)
* @param content
* @param imgPath
* @param output
* @param needCompress
是否压缩LOGO
* @throws Exception
*/ public static void encode(String content, String imgPath,
OutputStream output, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath,
needCompress);
ImageIO.write(image, FORMAT_NAME, output); } /**
* 生成二维码
* @param content
* @param output
* @throws Exception
*/ public static void encode(String content, OutputStream output)
throws Exception {
QRCodeUtil.encode(content, null, output, false); } /**
* 解析二维码
* @param file
二维码图片
* @throws Exception
*/ public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable&DecodeHintType, Object& hints = new Hashtable&DecodeHintType, Object&();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr; } /**
* 解析二维码
* @param path
二维码图片地址
* @throws Exception
*/ public static String decode(String path) throws Exception {
return QRCodeUtil.decode(new File(path)); } public static void main(String[] args) throws Exception {
String text = &薯 灯可分列式本上楞珂要瓜熟蒂落!000&;
QRCodeUtil.encode(text, &c:/df.jsp&, &c:/a/&, true); }}
blog__9763061
&div class=&dp-highlighter bg_java&&&div class=&bar&&&div class=&tools&&&strong&[java]&/strong& &a target=_blank class=&ViewSource& title=&view plain& href=&http://blog.csdn.net/mmm333zzz/article/details/#&&view plain&/a&&a target=_blank class=&CopyToClipboard& title=&copy& href=&http://blog.csdn.net/mmm333zzz/article/details/#&&copy&/a&&a target=_blank class=&PrintSource& title=&print& href=&http://blog.csdn.net/mmm333zzz/article/details/#&&print&/a&&a target=_blank class=&About& title=&?& href=&http://blog.csdn.net/mmm333zzz/article/details/#&&?&/a&&a target=_blank style=&TEXT-INDENT: 0px& title=&在CODE上查看代码片& href=&https://code.csdn.net/snippets/107177& target=&_blank&&&img style=&POSITION: TOP: 1 LEFT: 2px& alt=&在CODE上查看代码片& src=&https://code.csdn.net/assets/CODE_ico.png& width=&12& height=&12& /&&/a&&a target=_blank style=&TEXT-INDENT: 0px& title=&派生到我的代码片& href=&https://code.csdn.net/snippets/107177/fork& target=&_blank&&&img style=&POSITION: TOP: 2 LEFT: 2px& alt=&派生到我的代码片& src=&https://code.csdn.net/assets/ico_fork.svg& width=&12& height=&12& /&&/a&&/div&&/div&&ol class=&dp-j&&&li class=&alt&&&span&&span class=&keyword&&package&/span&&span& com.util.cccm;
&/span&&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&&/span&&span class=&keyword&&import&/span&&span& java.awt.Graphics2D;
&/span&&/li&&li&&span&&/span&&span class=&keyword&&import&/span&&span& java.awt.geom.AffineTransform;
&/span&&/li&&li class=&alt&&&span&&/span&&span class=&keyword&&import&/span&&span& java.awt.image.BufferedImage;
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&&/span&&span class=&keyword&&import&/span&&span& com.google.zxing.LuminanceSource;
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&&/span&&span class=&keyword&&public&/span&&span& &/span&&span class=&keyword&&class&/span&&span& BufferedImageLuminanceSource &/span&&span class=&keyword&&extends&/span&&span& LuminanceSource {
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&private&/span&&span& &/span&&span class=&keyword&&final&/span&&span& BufferedImage image;
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&private&/span&&span& &/span&&span class=&keyword&&final&/span&&span& &/span&&span class=&keyword&&int&/span&&span& left;
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&private&/span&&span& &/span&&span class=&keyword&&final&/span&&span& &/span&&span class=&keyword&&int&/span&&span& top;
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&public&/span&&span& BufferedImageLuminanceSource(BufferedImage image) {
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&this&/span&&span&(image, &/span&&span class=&number&&0&/span&&span&, &/span&&span class=&number&&0&/span&&span&, image.getWidth(), image.getHeight());
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&public&/span&&span& BufferedImageLuminanceSource(BufferedImage image, &/span&&span class=&keyword&&int&/span&&span& left,
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&int&/span&&span& top, &/span&&span class=&keyword&&int&/span&&span& width, &/span&&span class=&keyword&&int&/span&&span& height) {
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&super&/span&&span&(width, height);
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&int&/span&&span& sourceWidth = image.getWidth();
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&int&/span&&span& sourceHeight = image.getHeight();
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&if&/span&&span& (left + width & sourceWidth || top + height & sourceHeight) {
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&throw&/span&&span& &/span&&span class=&keyword&&new&/span&&span& IllegalArgumentException(
&/span&&/li&&li&&span&
&/span&&span class=&string&&&Crop rectangle does not fit within image data.&&/span&&span&);
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&for&/span&&span& (&/span&&span class=&keyword&&int&/span&&span& y = top; y & top + height; y++) {
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&for&/span&&span& (&/span&&span class=&keyword&&int&/span&&span& x = left; x & left + width; x++) {
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&if&/span&&span& ((image.getRGB(x, y) & &/span&&span class=&number&&0xFF000000&/span&&span&) == &/span&&span class=&number&&0&/span&&span&) {
&/span&&/li&&li&&span&
image.setRGB(x, y, &/span&&span class=&number&&0xFFFFFFFF&/span&&span&); &/span&&span class=&comment&&// = white &/span&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&this&/span&&span&.image = &/span&&span class=&keyword&&new&/span&&span& BufferedImage(sourceWidth, sourceHeight,
&/span&&/li&&li&&span&
BufferedImage.TYPE_BYTE_GRAY);
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&this&/span&&span&.image.getGraphics().drawImage(image, &/span&&span class=&number&&0&/span&&span&, &/span&&span class=&number&&0&/span&&span&, &/span&&span class=&keyword&&null&/span&&span&);
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&this&/span&&span&.left =
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&this&/span&&span&.top =
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&public&/span&&span& &/span&&span class=&keyword&&byte&/span&&span&[] getRow(&/span&&span class=&keyword&&int&/span&&span& y, &/span&&span class=&keyword&&byte&/span&&span&[] row) {
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&if&/span&&span& (y & &/span&&span class=&number&&0&/span&&span& || y &= getHeight()) {
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&throw&/span&&span& &/span&&span class=&keyword&&new&/span&&span& IllegalArgumentException(
&/span&&/li&&li&&span&
&/span&&span class=&string&&&Requested row is outside the image: &&/span&&span& + y);
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&int&/span&&span& width = getWidth();
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&if&/span&&span& (row == &/span&&span class=&keyword&&null&/span&&span& || row.length & width) {
&/span&&/li&&li&&span&
row = &/span&&span class=&keyword&&new&/span&&span& &/span&&span class=&keyword&&byte&/span&&span&[width];
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
image.getRaster().getDataElements(left, top + y, width, &/span&&span class=&number&&1&/span&&span&, row);
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&return&/span&&span&
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&public&/span&&span& &/span&&span class=&keyword&&byte&/span&&span&[] getMatrix() {
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&int&/span&&span& width = getWidth();
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&int&/span&&span& height = getHeight();
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&int&/span&&span& area = width *
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&byte&/span&&span&[] matrix = &/span&&span class=&keyword&&new&/span&&span& &/span&&span class=&keyword&&byte&/span&&span&[area];
&/span&&/li&&li&&span&
image.getRaster().getDataElements(left, top, width, height, matrix);
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&return&/span&&span&
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&public&/span&&span& &/span&&span class=&keyword&&boolean&/span&&span& isCropSupported() {
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&return&/span&&span& &/span&&span class=&keyword&&true&/span&&span&;
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&public&/span&&span& LuminanceSource crop(&/span&&span class=&keyword&&int&/span&&span& left, &/span&&span class=&keyword&&int&/span&&span& top, &/span&&span class=&keyword&&int&/span&&span& width, &/span&&span class=&keyword&&int&/span&&span& height) {
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&return&/span&&span& &/span&&span class=&keyword&&new&/span&&span& BufferedImageLuminanceSource(image, &/span&&span class=&keyword&&this&/span&&span&.left + left,
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&this&/span&&span&.top + top, width, height);
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&public&/span&&span& &/span&&span class=&keyword&&boolean&/span&&span& isRotateSupported() {
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&return&/span&&span& &/span&&span class=&keyword&&true&/span&&span&;
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&public&/span&&span& LuminanceSource rotateCounterClockwise() {
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&int&/span&&span& sourceWidth = image.getWidth();
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&int&/span&&span& sourceHeight = image.getHeight();
&/span&&/li&&li&&span&
AffineTransform transform = &/span&&span class=&keyword&&new&/span&&span& AffineTransform(&/span&&span class=&number&&0.0&/span&&span&, -&/span&&span class=&number&&1.0&/span&&span&, &/span&&span class=&number&&1.0&/span&&span&,
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&number&&0.0&/span&&span&, &/span&&span class=&number&&0.0&/span&&span&, sourceWidth);
&/span&&/li&&li&&span&
BufferedImage rotatedImage = &/span&&span class=&keyword&&new&/span&&span& BufferedImage(sourceHeight,
&/span&&/li&&li class=&alt&&&span&
sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
&/span&&/li&&li&&span&
Graphics2D g = rotatedImage.createGraphics();
&/span&&/li&&li class=&alt&&&span&
g.drawImage(image, transform, &/span&&span class=&keyword&&null&/span&&span&);
&/span&&/li&&li&&span&
g.dispose();
&/span&&/li&&li class=&alt&&&span&
&/span&&span class=&keyword&&int&/span&&span& width = getWidth();
&/span&&/li&&li&&span&
&/span&&span class=&keyword&&return&/span&&span& &/span&&span class=&keyword&&new&/span&&span& BufferedImageLuminanceSource(rotatedImage, top,
&/span&&/li&&li class=&alt&&&span&
sourceWidth - (left + width), getHeight(), width);
&/span&&/li&&li&&span&
&/span&&/li&&li class=&alt&&&span&}
&/span&&/li&&/ol&&/div&&pre style=&DISPLAY: none& class=&java& name=&code& snippet_file_name=&blog__4920492& code_snippet_id=&107177&&package com.util.import java.awt.Graphics2D;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import com.google.zxing.LuminanceSource;public class BufferedImageLuminanceSource extends LuminanceSource { private final BufferedImage image; private final int left; private final int top; public BufferedImageLuminanceSource(BufferedImage image) {
this(image, 0, 0, image.getWidth(), image.getHeight()); } public BufferedImageLuminanceSource(BufferedImage image, int left,
int top, int width, int height) {
super(width, height);
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
if (left + width & sourceWidth || top + height & sourceHeight) {
throw new IllegalArgumentException(
&Crop rectangle does not fit within image data.&);
for (int y = top; y & top + height; y++) {
for (int x = left; x & left + width; x++) {
if ((image.getRGB(x, y) & 0xFF000000) == 0) {
image.setRGB(x, y, 0xFFFFFFFF); // = white
this.image = new BufferedImage(sourceWidth, sourceHeight,
BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image, 0, 0, null);
this.left = left;
this.top = top; }
public byte[] getRow(int y, byte[] row) {
if (y & 0 || y &= getHeight()) {
throw new IllegalArgumentException(
&Requested row is outside the image: & + y);
int width = getWidth();
if (row == null || row.length & width) {
row = new byte[width];
image.getRaster().getDataElements(left, top + y, width, 1, row);
return row; }
public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
int area = width * height;
byte[] matrix = new byte[area];
image.getRaster().getDataElements(left, top, width, height, matrix);
return matrix; }
public boolean isCropSupported() {
return true; }
public LuminanceSource crop(int left, int top, int width, int height) {
return new BufferedImageLuminanceSource(image, this.left + left,
this.top + top, width, height); }
public boolean isRotateSupported() {
return true; }
public LuminanceSource rotateCounterClockwise() {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0,
0.0, 0.0, sourceWidth);
BufferedImage rotatedImage = new BufferedImage(sourceHeight,
sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = rotatedImage.createGraphics();
g.drawImage(image, transform, null);
g.dispose();
int width = getWidth();
return new BufferedImageLuminanceSource(rotatedImage, top,
sourceWidth - (left + width), getHeight(), width); }}
blog__4920492
package com.util.cccm;import java.awt.Graphics2D;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import com.google.zxing.LuminanceSource;public class BufferedImageLuminanceSource extends LuminanceSource { private final BufferedImage image; private final int left; private final int top; public BufferedImageLuminanceSource(BufferedImage image) {
this(image, 0, 0, image.getWidth(), image.getHeight()); } public BufferedImageLuminanceSource(BufferedImage image, int left,
int top, int width, int height) {
super(width, height);
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
if (left + width & sourceWidth || top + height & sourceHeight) {
throw new IllegalArgumentException(
&Crop rectangle does not fit within image data.&);
for (int y = top; y & top + height; y++) {
for (int x = left; x & left + width; x++) {
if ((image.getRGB(x, y) & 0xFF000000) == 0) {
image.setRGB(x, y, 0xFFFFFFFF); // = white
this.image = new BufferedImage(sourceWidth, sourceHeight,
BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image, 0, 0, null);
this.left = left;
this.top = top; }
public byte[] getRow(int y, byte[] row) {
if (y & 0 || y &= getHeight()) {
throw new IllegalArgumentException(
&Requested row is outside the image: & + y);
int width = getWidth();
if (row == null || row.length & width) {
row = new byte[width];
image.getRaster().getDataElements(left, top + y, width, 1, row);
return row; }
public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
int area = width * height;
byte[] matrix = new byte[area];
image.getRaster().getDataElements(left, top, width, height, matrix);
return matrix; }
public boolean isCropSupported() {
return true; }
public LuminanceSource crop(int left, int top, int width, int height) {
return new BufferedImageLuminanceSource(image, this.left + left,
this.top + top, width, height); }
public boolean isRotateSupported() {
return true; }
public LuminanceSource rotateCounterClockwise() {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0,
0.0, 0.0, sourceWidth);
BufferedImage rotatedImage = new BufferedImage(sourceHeight,
sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = rotatedImage.createGraphics();
g.drawImage(image, transform, null);
g.dispose();
int width = getWidth();
return new BufferedImageLuminanceSource(rotatedImage, top,
sourceWidth - (left + width), getHeight(), width); }}}

我要回帖

更多关于 new2.3.1 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信