zoukankan      html  css  js  c++  java
  • 图像属性——老照片特效

    1、实现抽象类AbstractBufferedImageOp ——————读取和写入像素数据操作

    package chapter4;

    import java.awt.image.BufferedImage;

    public class AbstractBufferedImageOp {

    //读取像素数据
    public int[] getRGB(BufferedImage image,int x, int y, int width, int height,int[] pixels){
    int type = image.getType();
    if(type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB){
    return (int[]) image.getRaster().getDataElements(x,y,width,height,pixels);
    }else{
    return image.getRGB(x,y,width,height,pixels,0,width);
    }
    }
    //写入像素数据
    public void setRGB(BufferedImage image,int x, int y, int width, int height, int[] pixels){
    int type =image.getType();
    if(type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB){
    image.getRaster().setDataElements(x,y,width,height,pixels);
    }else {
    image.setRGB(x,y,width,height,pixels,0,width);
    }
    }
    public BufferedImage creatCompatibleDestImage(BufferedImage src,BufferedImage dest){

    return new BufferedImage(src.getWidth(),src.getHeight(),BufferedImage.TYPE_INT_RGB);
    }
    }

    2、实现照片泛黄代码————SepiaToneFilter类

    package chapter4;

    import java.awt.image.BufferedImage;

    public class SepiaToneFilter extends AbstractBufferedImageOp{
    public BufferedImage filter(BufferedImage src, BufferedImage dest){
    int width = src.getWidth();
    int height = src.getHeight();

    if(dest == null){
    dest = creatCompatibleDestImage(src,null);
    }
    int[] inpixels = new int[width*height];
    int[] outpixels = new int[width*height];
    getRGB(src,0,0,width,height,inpixels);
    int index = 0;
    for(int row=0;row<height;row++){
    int ta=0, tr=0, tg=0,tb=0;
    for(int col=0;col<width;col++){
    index = row*width+col;
    ta = (inpixels[index] >> 24) & 0xff;
    tr = (inpixels[index] >> 16) & 0xff;
    tg = (inpixels[index] >> 8) & 0xff;
    tb = (inpixels[index]) & 0xff;

    int fr = (int) colorBlend(noise(),(tr*0.393)+(tg*0.769)+(tb*0.189),tr);
    int fg = (int) colorBlend(noise(),(tr*0.349)+(tg*0.686)+(tb*0.189),tg);
    int fb = (int) colorBlend(noise(),(tr*0.272)+(tg*0.534)+(tb*0.131),tb);

    outpixels[index] = (ta<<24) | clamp(fr) << 16 | clamp(fg) << 8 | clamp(fb);
    }
    }
    setRGB(dest,0,0,width,height,outpixels);
    return dest;
    }
    //设置权重系数
    public double noise(){
    return Math.random()*0.5+0.5;
    }
    //计算新的像素值=新的像素值+原来像素值-原来像素值*权重系数
    public double colorBlend(double scale, double dest, double src){
    return (scale*dest+(1-scale)*src);
    }
    /* c<0 0
    c>255 255
    * 0<c<255 c
    * */
    public static int clamp(int c){
    return c>255 ? 255:((c<0) ? 0:c);
    }
    public String toString(){
    return "Sepia Tone Effect - Effect from Photoshop App";
    }
    }

    3、在ImagePanel类上加入滤镜操作

    package chapter4;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;

    public class ImageJPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private BufferedImage sourceImage;
    private BufferedImage destImage;

    public ImageJPanel(){}
    public BufferedImage getSourceImage() {
    return sourceImage;
    }

    public void setSourceImage(BufferedImage sourceImage) {
    this.sourceImage = sourceImage;
    }

    public BufferedImage getDestImage() {
    return destImage;
    }

    public void setDestImage(BufferedImage destImage) {
    this.destImage = destImage;
    }


    public void process(){
    SepiaToneFilter filter = new SepiaToneFilter();
    destImage = filter.filter(sourceImage,null);
    }

    public void paintComponent(Graphics g){
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.clearRect(0,0,this.getWidth(),this.getHeight());

    if(sourceImage != null){
    g2d.drawImage(sourceImage,0,0,sourceImage.getWidth(),sourceImage.getHeight(),null);
    if(destImage != null){
    g2d.drawImage(destImage,sourceImage.getWidth()+10,0,destImage.getWidth(),destImage.getHeight(),null);
    }
    }

    }

    }

    4、在UI_MainUI类中运行

    package chapter4;

    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.filechooser.FileNameExtensionFilter;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;

    /**
    * Created by LENOVO on 18-1-29.
    */
    public class UI_MainUI extends JFrame implements ActionListener{
    private static final long serialVersionUID = 1L;
    private static final String IMG_CMD = "选择图像...";
    private static final String PROCESS_CMD = "处理";

    private JButton imgBtn;
    private JButton processBtn;
    private ImageJPanel imageJPanel;

    private BufferedImage srcImage;
    public UI_MainUI(){
    setTitle("JFrame UI —演示");
    imgBtn = new JButton(IMG_CMD);
    processBtn = new JButton(PROCESS_CMD);

    JPanel btnJpanel = new JPanel();
    btnJpanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    btnJpanel.add(imgBtn);
    btnJpanel.add(processBtn);

    imageJPanel = new ImageJPanel();
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(imageJPanel,BorderLayout.CENTER);
    getContentPane().add(btnJpanel,BorderLayout.SOUTH);

    setupActionListener();

    }

    public void setupActionListener(){
    imgBtn.addActionListener(this);
    processBtn.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    if(SwingUtilities.isEventDispatchThread()){
    System.out.println("Event Dispatch Thred!!");
    }
    if(srcImage == null){

    try {
    JOptionPane.showMessageDialog(this,"请先选择图像源文件");
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    setFileTypeFilter(chooser);
    File f = chooser.getSelectedFile();
    if(f != null){
    srcImage = ImageIO.read(f);
    imageJPanel.setSourceImage(srcImage);//图像面板添加图像
    imageJPanel.repaint();
    }
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    return ;
    }
    if (IMG_CMD.equals(e.getActionCommand())){
    if(srcImage == null){

    try {
    JOptionPane.showMessageDialog(this,"请先选择图像源文件");
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();
    if(f != null){
    srcImage = ImageIO.read(f);
    imageJPanel.setSourceImage(srcImage);//图像面板添加图像
    imageJPanel.repaint();
    }
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }
    }else if(PROCESS_CMD.equals(e.getActionCommand())){
    imageJPanel.process();
    imageJPanel.repaint();
    }
    }
    public void setFileTypeFilter(JFileChooser chooser){
    FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & PNG Images","jpg","png");
    chooser.setFileFilter(filter);
    }
    public void openView(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setPreferredSize(new Dimension(800,600));
    pack();
    setVisible(true);
    }
    public static void main(String args[]){
    UI_MainUI ui = new UI_MainUI();
    ui.openView();
    }
    }

  • 相关阅读:
    linux 下查看文件个数及大小
    weblogic日志小结
    Excel数据通过plsql导入到Oracle
    Linux查看外网IP
    linux挂载/卸载优盘
    git版本回退
    linux修改文件所属用户、用户组
    retry.RetryInvocationHandler (RetryInvocationHandler.java:invoke(140))
    Hadoop切换namenode为active
    Netty使用LineBasedFrameDecoder解决TCP粘包/拆包
  • 原文地址:https://www.cnblogs.com/bigdream6/p/8377499.html
Copyright © 2011-2022 走看看