Java基础之java8 新特性

article/2025/7/28 1:05:58

Java基础之java8 新特性

  • 一、Lambda 表达式
  • 二、Stream 初体验
  • 三、方法引用
  • 四、默认方法
  • 五、Optional 类
  • 六、Base 64
  • 七、字符串拼接
  • 八、== equals 与 instanceof
  • 九、final

一、Lambda 表达式

  Lambda 表达式是在 Java8 中引入的,并号称是 Java8 的最大的特点。Lambda 表达式有利于函数式编程,简化了开发了很多。

  语法:parameter -> expression body

  操作符 “->” 称为箭头操作符或 Lambda 操作符

  箭头操作符将 Lambda 表达式拆分成两部分:

  左侧:Lambda 表达式的参数列表

  右侧:Lambda 表达式中所需执行的功能, 即 Lambda 体

  案例:集合排序

public class Demo {public static void main(String[] args) {List<Entity> list = new ArrayList<>();list.add(new Entity("张一", 18));list.add(new Entity("张二", 25));list.add(new Entity("张三", 14));list.add(new Entity("张四", 19));list.add(new Entity("张五", 23));list.sort(new Comparator<Entity>() {@Overridepublic int compare(Entity o1, Entity o2) {return o1.age > o2.age ? 1 : o1.age < o2.age ? -1 : 0;}});System.out.println(list);}
}
class Entity {String name;Integer age;public Entity(String name, Integer age) {this.age = age;this.name = name;}
}

  转化后的排序变成一行代码:两个参数一条语句(大括号和 return 可以不写),Lambda 表达式的参数列表的数据类型可以省略不写,因为 JVM 编译器通过上下文推断出,数据类型,即“类型推断”。

list.sort((o1, o2) -> o1.age > o2.age ? 1 : o1.age < o2.age ? -1 : 0);

  idea 一般都会自动提示普通写法和 lambda 相互转化,(alt + 回车)一般按照提示转化即可。
在这里插入图片描述
在这里插入图片描述
  案例:Runnable :lambda 表达式一般可以操作加 @FunctionalInterface 注解的接口(函数式接口)

Runnable r = new Runnable() {@Overridepublic void run() {System.out.println("Hello lambda!");}
};		
r.run();//变成Runnable r1 = () -> System.out.println("Hello Lambda!");
r1.run();

  案例:forEach 遍历 list

List<Entity> list = new ArrayList<>();
list.add(new Entity("张一", 18));
list.add(new Entity("张二", 25));
list.add(new Entity("张三", 14));
list.add(new Entity("张四", 19));
list.add(new Entity("张五", 23));
list.forEach(new Consumer<Entity>() {@Overridepublic void accept(Entity entity) {System.out.println(entity);}
});

  转化为 Lambda 后,当方法的参数只有一个时前面的括号可以省略。

list.forEach(entity -> System.out.println(entity));

  案例:forEach 遍历 map

Map<String, Object> map = new HashMap<>();
map.put("id", 1);
map.put("name", "张三");
map.put("password", "123456");
map.forEach((k, v) -> {System.out.println(k);System.out.println(v);
});

二、Stream 初体验

  Java 8 API 添加了一个新的抽象称为流 Stream,可以让你以一种声明的方式处理数据。 Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达的高阶抽象。 Stream API 可以极大提高 Java 程序员的生产力,让程序员写出高效率、干净、简洁的代码。 这种风格将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如筛选, 排序,聚合等。
在这里插入图片描述
  元素流在管道中经过中间操作(intermediate operation)的处理,最后由最终操作(terminal operation)得到前面处理的结果。

  流程:

stream of elements -> filter -> sorted -> map -> collect

map

  map 方法用于映射每个元素到对应的结果。

  案例:使用 map 输出了元素对应的平方数:

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
// 获取对应的平方数
List<Integer> squaresList = numbers.stream().map( i -> i*i).collect(Collectors.toList());

filter

  filter 方法用于通过设置的条件过滤出元素。

  案例:使用 filter 方法过滤出空字符串:

List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
// 获取空字符串的数量
long count = strings.stream().filter(string -> string.isEmpty()).count();

limit

  limit 方法用于获取指定数量的流。

  案例:使用 limit 方法获取前 3 条不为空的数组数据:

List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> collect = strings.stream().filter(string -> string != "").limit(3).collect(Collectors.toList());

sorted

  sorted 方法用于对流进行排序。

  案例:使用 sorted 方法对集合数据进行排序:

List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> collect = strings.stream().filter(string -> string != "").sorted().collect(Collectors.toList());

distinct

  distinct 方法用于对流进行去重。

  案例:

List<String>strings = Arrays.asList("bc", "", "bc", "efg", "abcd","", "jkl");
List<String> collect = strings.stream().filter(string -> string != "").distinct().collect(Collectors.toList());

count

  count 方法用于对流进行计数。

List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
long count = strings.stream().filter(string -> string.isEmpty()).count();

  扩展案例:

Random random = new Random();
//生成 10 个[0,100] 的随机数
List<Integer> collect = random.ints(0, 101).boxed().limit(10).collect(Collectors.toList());
System.out.println(collect);//生成一个从0开始到10的集合
List<Integer> list = IntStream.range(1, 11).boxed().collect(Collectors.toList());
System.out.println(list);//计算上面集合的平均值
Double avarage = list.stream().collect(Collectors.averagingInt(item -> item));
System.out.println(avarage);//对列表元素进行统计
IntSummaryStatistics iss = list.stream().collect(Collectors.summarizingInt(value -> value));
System.out.println(iss);
//{count=10, sum=55, min=1, average=5.500000, max=10}//根据 List 创建 Map
Map<Integer, Integer> map = list.stream().collect(Collectors.toMap(p -> p, q->q*3));
System.out.println(map);//获取列表的最大值
Optional<Integer> max = list.stream().reduce(Math::max);
max.ifPresent(value -> System.out.println(value));

注意:

  1.boxed 用于将 IntStream 转化成 Stream。

  2.Math::max 称为方法引用。

三、方法引用

  在 Java8 中,你可以使用 class::methodName 语法引用类或对象的方法。让我们来学习一下 Java 8 中不同类型的方法引用。Java 8 中包含了四种类型的方法引用。

方法引用                	描述	                                    例子
静态方法引用            	用于引用类的静态方法	                    Math::max 相当于 Math.max(x,y)
从对象中引用实例方法    	使用对象的引用来调用实例方法	            System.out::println 相当于 System.out.println(x)
从类中引用实例方法        	在上下文提供的对象的引用上调用实例方法	    String::length 相当于 str.length()
引用构造函数            	引用构造函数	                            ArrayList::new```相当于new ArrayList()`

  引用静态方法 - Class::staticMethodName

  一个使用 Math.max() 静态方法的例子。

List<Integer> integers = Arrays.asList(1,12,433,5);
Optional<Integer> max = integers.stream().reduce( Math::max ); 
max.ifPresent(value -> System.out.println(value));//433

  从对象中引用实例方法 - ClassInstance::instanceMethodName

  在上面的例子中,我们使用了 System.out.println(value) 打印集合中的最大值,我们可以使用 System.out::println 打印这个值。

List<Integer> integers = Arrays.asList(1,12,433,5);        
Optional<Integer> max = integers.stream().reduce( Math::max ); 
max.ifPresent( System.out::println );//433

  引用特定类型的实例方法 - Class::instanceMethodName

  在这个例子中 s1.compareTo(s2) 被简写为 String::compareTo。

List<String> strings = Arrays.asList("how", "to", "do", "in", "java", "dot", "com");
List<String> sortedAlt = strings.stream().sorted(String::compareTo).collect(Collectors.toList());
System.out.println(sortedAlt);

引用构造函数 - Class::new

  使用 lambda 表达式修改第一个例子中的方法,可以非常简单的创建一个从1到100的集合(不包含100)。创建一个新的 ArrayList 实例,我们可以使用 ArrayList::new。

List<Integer> integers = IntStream.range(1, 100).boxed().collect(Collectors.toCollection( ArrayList::new ));
Optional<Integer> max = integers.stream().reduce(Math::max); 
max.ifPresent(System.out::println);//99

  案例:

public class User {private String username;private Integer age;public User() {}public User(String username, Integer age) {this.username = username;this.age = age;}@Overridepublic String toString() {return "User{" +"username='" + username + '\'' +", age=" + age +'}';}// Getter&amp;Setter
}
public static void main(String[] args) {// 使用双冒号::来构造静态函数引用Function<String, Integer> fun = Integer::parseInt;Integer value = fun.apply("123");System.out.println(value);// 使用双冒号::来构造非静态函数引用String content = "Hello JDK8";Function<Integer, String> func = content::substring;String result = func.apply(1);System.out.println(result);// 构造函数引用BiFunction<String, Integer, User> biFunction = User::new;User user = biFunction.apply("mengday", 28);System.out.println(user.toString());// 函数引用也是一种函数式接口,所以也可以将函数引用作为方法的参数sayHello(String::toUpperCase, "hello");
}// 方法有两个参数,一个是
private static void sayHello(Function<String, String> func, String parameter){String result = func.apply(parameter);System.out.println(result);
}

四、默认方法

  Java 8 新增了接口的默认方法。简单说,默认方法就是接口可以有实现方法,而且不需要实现类去实现其方法。

  我们只需在方法名前面加个 default [dɪˈfɔːlt]关键字即可实现默认方法。为什么要有这个特性?

  首先,之前的接口是个双刃剑,好处是面向抽象而不是面向具体编程,缺陷是,当需要修改接口时候,需要修改全部实现该接口的类,目前的 java 8 之前的集合框架没有 foreach 方法,通常能想到的解决办法是在 JDK 里给相关的接口添加新的方法及实现。然而,对于已经发布的版本,是没法在给接口添加新方法的同时不影响已有的实现。所以引进的默认方法。他们的目的是为了解决接口的修改与现有的实现不兼容的问题。

  案例:

public interface Moveable {default void move(){System.out.println("I am moving");}
}

  Moveable 接口定义了一个 move() 方法并且提供了默认的实现。如果任意一个 class 实现这个接口都没有必要去实现这个 move() 方法,能够直接使用 instance.move() 进行调用。

  案例:

public class Animal implements Moveable{public static void main(String[] args){Animal tiger = new Animal();tiger.move();}
}
// I am moving

  如果该 class 想要自定义一个 move() 也能提供自定义的实现去覆写这个 move 方法。

五、Optional 类

  是一个可以为 null 的容器对象。如果值存在则 isPresent() [ˈpreznt , prɪˈzent] 方法会返回 true,调用 get() 方法会返回该对象。

  Optional 是个容器:它可以保存类型 T 的值,或者仅仅保存 null。Optional 提供很多有用的方法,这样我们就不用显式进行空值检测。

  Optional 类的引入很好的解决空指针异常。

  案例:

import java.util.Optional;public class OptionalTester {public static void main(String args[]) {Integer value1 = null;Integer value2 = new Integer(10);// Optional.ofNullable - 允许传递为 null 参数Optional<Integer> a = Optional.ofNullable(value1);// Optional.of - 如果传递的参数是 null,抛出异常 NullPointerExceptionOptional<Integer> b = Optional.of(value2);System.out.println(sum(a, b));}public static Integer sum(Optional<Integer> a, Optional<Integer> b) {// Optional.isPresent - 判断值是否存在System.out.println("第一个参数值存在: " + a.isPresent());System.out.println("第二个参数值存在: " + b.isPresent());// Optional.orElse - 如果值存在,返回它,否则返回默认值 3Integer value1 = a.orElse(new Integer(3));//Optional.get - 获取值,值需要存在Integer value2 = b.get();return value1 + value2;}
}

  使用示例:求和时如遇空数字则使用默认值 0 参与运算。

import java.util.Optional;public class OptionalTester {public static void main(String args[]) {Integer value1 = null;Integer value2 = new Integer(10);//同样都是求和,如果数据为空则使用默认值 0 参与计算System.out.println(sum(value1, value2));System.out.println(sum(Optional.ofNullable(value1), Optional.ofNullable(value2)));}public static Integer sum(Optional<Integer> a, Optional<Integer> b) {return a.orElse(0) + b.orElse(0);}public static Integer sum(Integer a, Integer b) {if (a == null &amp;&amp; b == null)return 0;if (a == null)return 0 + b;if (b == null)return a + 0;return a + b;}
}

六、Base 64

  Base64 是一种用 64 个字符来表示任意二进制数据的方法,有些人也叫 Base64 加密。

  用记事本打开 exe、jpg、pdf 这些文件时,我们都会看到一大堆乱码,因为二进制文件包含很多无法显示和打印的字符,所以,如果要让记事本这样的文本处理软件能处理二进制数据,就需要一个二进制到字符串的转换方法。Base64 是一种最常见的二进制解编码方法。

  Base64 的原理很简单,首先,准备一个包含 64 个字符的数组:

['A', 'B', 'C', ... 'a', 'b', 'c', ... '0', '1', ... '+', '/']

  然后,对二进制数据进行处理,每 3 个字节一组,一共是 3x8=24bit,划为 4 组,每组正好 6 个 bit:
在这里插入图片描述
  这样我们得到 4 个数字作为索引,然后查表,获得相应的 4 个字符,就是编码后的字符串。

  所以,Base64 编码会把 3 字节的二进制数据编码为 4 字节的文本数据,长度增加 33%,好处是编码后的文本数据可以在邮件正文、网页等直接显示。 如果要编码的二进制数据不是 3 的倍数,最后会剩下 1 个或 2 个字节怎么办?Base64 用 \x00 字节在末尾补足后,再在编码的末尾加上 1 个或 2 个 = 号,表示补了多少字节,解码的时候,会自动去掉。

  java8 在 Base64 加解码上已经提供一套标准的工具

  String base64encodedString = Base64.getEncoder().encodeToString(byte数组);

  byte[] base64decodedBytes = Base64.getDecoder().decode(base64字符串);

  字符串获取 byte 数组时 .getBytes(“utf-8”); 解决中文乱码问题。

  案例:

String string = Base64.getEncoder().encodeToString("hello word,你好,世界".getBytes());
System.out.println(string);
byte[] bytes = Base64.getDecoder().decode(string);
System.out.println(new String(bytes));

  案例:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;public class Hello {public static void main(String[] args) throws IOException {FileInputStream inputStream = new FileInputStream("c:/下载.png");byte[] bytes = new byte[10240000];int read = inputStream.read(bytes);if (read < 10240000){String base64encodedString = Base64.getEncoder().encodeToString(Arrays.copyOfRange(bytes,0,read));System.out.println(base64encodedString);}}
}

七、字符串拼接

  从 Java7 到目前为止,我们可以通过向 String.split() 方法中传递参数的方式来分割一个字符串,然后把分割后的字符串列表以数组的形式返回。

  但是,如果需要连接字符串或者通过分隔符连接的字符串来创建一个 CSV 文件,则必须遍历字符串列表或数组, 然后使用 StringBuilder 或 StringBuffer 对象拼接这些字符串,最后得到 CSV。

  在 Java8 中使得字符串拼接任务变得容易。现在你可以使用 String.join() 方法, 其中第一个参数是分隔符,然后可以传递多个字符串或实现了 Iterable 接口的实例作为第二个参数,下面的示例将返回。

import java.time.ZoneId;
public class StringJoinDemo {public static void main(String[] args){String joined = String.join("/","usr","local","bin");System.out.println(joined);String ids = String.join(", ", ZoneId.getAvailableZoneIds());System.out.println(ids);}
}//usr/local/bin
//Asia/Aden, America/Cuiaba, Etc/GMT+9, Etc/GMT+8.....

八、== equals 与 instanceof

  == 判断内存地址是否相同(如果是简单类型则比较他们的值是否相等)

  equals 调用对象的 equals 方法判断两个对象是否相等,如果对象的类没有重写 equals 方法,则使用 object 的 equals 方法,object 的 equals 方法是比较内存地址。

  instanceof:关键字用来确定对象所属的类,或父类。

  System.out.println(student instanceof Student);//true

九、final

  final 中文意思:最后的,最终的。

  final 可以修饰变量、方法或者类

  • 当不希望父类的某个方法被子类覆盖(override)时,可以用 final 关键字修饰。
  • 当不希望类的某个变量的值被修改,可以用 final 修饰。如果一个变量是 final,则必须赋初值,否则编译出错。
  • 当类不希望被继承时,可以在类前面用 final 修饰。

注意:

  final 修饰的变量又叫常量,一般用全大写下划线命名。( Integer.MAX_VALUE )
  final 修饰的变量在定义时,必须初始化,并且以后不能再赋值。

章节练习:

  1.遍历集合:使用 Lambda 表达式遍历如下的集合,代码尽可能精简。

List<Employee> emps = Arrays.asList(new Employee(101, "张三", 18, 9999.99), new Employee(102, "李四", 59, 6666.66),new Employee(103, "王五", 28, 3333.33), new Employee(104, "赵六", 18, 7777.77),new Employee(105, "田七", 38, 5555.55));

  2.遍历 map:使用 Lambda 表达式遍历如下的 Map,代码尽可能精简。

Map<String, Employee> map = new HashMap<>();
map.put("101", new Employee(101, "张三", 18, 9999.99));
map.put("102", new Employee(102, "李四", 59, 6666.66));
map.put("103", new Employee(103, "王五", 28, 3333.33));
map.put("104", new Employee(104, "赵六", 8, 7777.77));
map.put("105", new Employee(105, "田七", 38, 5555.55));

  3.获取集合里面的某属性:使用 Stream 获取第一题里面的全部 id 的集合。

  4.字符串的 base64:使用 base64 加密与解密如下字符串:“abcd1234”。

  5.文件的 base64:使用 base64 算法获取 C:/Windows/win.ini 的 base64 字符串。

  6.乱序集合:创建一个方法 void shuffle(List arr),使其能在代码最精简,且最省内存的情况下完成数组的乱序。

  参考代码:

import org.junit.Test;import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;public class Demo {/*** 1.遍历集合*/@Testpublic void t1() {List<Employee> list = Arrays.asList(new Employee(101, "张三", 18, 9999.99),new Employee(102, "李四", 59, 6666.66), new Employee(103, "王五", 28, 3333.33),new Employee(104, "赵六", 18, 7777.77), new Employee(105, "田七", 38, 5555.55));list.forEach(System.out::println);}/*** 2.遍历 map*/@Testpublic void t2() {Map<String, Employee> map = new HashMap<>();map.put("101", new Employee(101, "张三", 18, 9999.99));map.put("102", new Employee(102, "李四", 59, 6666.66));map.put("103", new Employee(103, "王五", 28, 3333.33));map.put("104", new Employee(104, "赵六", 8, 7777.77));map.put("105", new Employee(105, "田七", 38, 5555.55));map.forEach((k, v) -> System.out.println(k + "=" + v));}/*** 3.获取集合里面的某属性*/@Testpublic void t3() {List<Employee> list = Arrays.asList(new Employee(101, "张三", 18, 9999.99),new Employee(102, "李四", 59, 6666.66), new Employee(103, "王五", 28, 3333.33),new Employee(104, "赵六", 18, 7777.77), new Employee(111, "田七", 38, 5555.55));List<Integer> collect = list.stream().map(Employee::getId).collect(Collectors.toList());System.out.println(collect);}/*** 4.字符串的 base64*/@Testpublic void t4() {String s1 = "abcd1234";String string = Base64.getEncoder().encodeToString(s1.getBytes());System.out.println(string);byte[] decode = Base64.getDecoder().decode(string);System.out.println(new String(decode));}/*** 5.获取文件的 base64*/@Testpublic void t5() throws IOException {InputStream stream = new FileInputStream("C:/Windows/win.ini");byte[] bytes = new byte[1024];int n = stream.read(bytes);String string = Base64.getEncoder().encodeToString(Arrays.copyOfRange(bytes, 0, n));System.out.println(string);byte[] decode = Base64.getDecoder().decode(string);System.out.println(new String(decode));stream.close();}/*** 6.乱序集合*/@Testpublic void t6() {List<Integer> list = IntStream.range(1, 11).boxed().collect(Collectors.toList());System.out.println(list);shuffle(list);System.out.println(list);}public void shuffle(List<Integer> arr) {for (int i = 0; i < arr.size(); i++) {int index = (int) (Math.random() * arr.size());int temp = arr.get(i);arr.set(i, arr.get(index));arr.set(index, temp);}}
}class Employee {private Integer id;private String name;private Integer age;private Double sal;public Employee(Integer id, String name, Integer age, Double sal) {this.id = id;this.name = name;this.age = age;this.sal = sal;}@Overridepublic String toString() {return "Employee{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", sal=" + sal +'}';}
}

http://chatgpt.dhexx.cn/article/9KdyNjaT.shtml

相关文章

java8新特性Lambda和Stream以及函数式接口等新特性介绍

主要内容 1.Lambda 表达式 2.函数式接口 3.方法引用与构造器引用 4.Stream API 5.接口中的默认方法与静态方法 6.新时间日期API 7.其他新特性 Java 8新特性简介 速度更快速度更快代码更少&#xff08;增加了新的语法Lambda 表达式&#xff09;强大的Stream API便于并行最大化…

python 读取并显示图片的两种方法

转自&#xff1a;http://www.cnblogs.com/yinxiangnan-charles/p/5928689.html 在 python 中除了用 opencv&#xff0c;也可以用 matplotlib 和 PIL 这两个库操作图片。本人偏爱 matpoltlib&#xff0c;因为它的语法更像 matlab。 一、matplotlib 1. 显示图片 import matplotli…

Python同时显示多张图片在一个画面中(两种方法)

很多时候需要把很多图片同时显示到一个画面中&#xff0c;现在分享两个方法&#xff0c;这里我恰好拿之前写的爬取网上图片保存到本地的爬虫模型爬一些图片作为素材Python 爬虫批量爬取网页图片保存到本地。 得到素材如下所示&#xff1a; 现在让这些图片同时显示。 方法一 …

python 打开 显示图片

import matplotlib.pyplot as plt # plt 用于显示图片 from PIL import Imageplt.figure() plt.plot([1,2], [1,2])plt.rcParams[font.sans-serif] [SimHei] # 中文乱码 plt.imshow(Image.open("b.png"))plt.axis(off)plt.tight_layout() manager plt.get_current…

python 把matplotlib绘制的图片显示到html中

需求 一般网页中图片显示是给url链接过去&#xff0c;但是有的时候显示的图表是临时计算绘制的&#xff0c;并不需要保存&#xff0c;因此就需要直接显示一个图片的方法。 灵感是来自于jupyter&#xff0c;发现他是这样的&#xff1a; 估计是base64编码了。 查了一下如何把ma…

python如何将图片显示在网页上

from flask import Flask, render_template_string import base64 import cv2import osapp Flask(__name__)# 读取图像app.route(/)def index():# 读取图像文件并将其转换为Base64编码的字符串img_path 1.pngimg_data open(img_path, rb).read()img_base64 base64.b64encod…

python matplotlib 显示图像

python matplotlib 显示图像 首先需要import import os from PIL import Image import matplotlib.pyplot as plt 显示一幅彩色图片 img Image.open(os.path.join(images, 2007_000648 .jpg))plt.figure("Image") # 图像窗口名称 plt.imshow(img) plt.axis(on)…

python中plt.imshow()不显示图片

python画图函数可能是需要一些设置&#xff0c;或者一些特定的函数运行才会出现图片的。 Python中plt.imshow(image)无法显示图片解决办法 使用plt.imshow()发现不能显示图片&#xff0c;加了plt.show()也还是不能显示 先引入包pylab import pylab 然后在plt.imshow(img)后面…

Python tkinter之PhotoImage图片显示问题

作为Python小白的你&#xff0c;有没有遇到过这几个问题&#xff1f; PhotoImage不支持jpg等图片格式&#xff1b;将PhotoImage放在函数里图片不显示&#xff1b;循环加入图片只显示一张&#xff1b; 这些难倒了一大批小白&#xff0c;那么接下来&#xff0c;本文教你如何解决…

python图显示在新的窗口_python在新的图片窗口显示图片(图像)的方法

python在新的图片窗口显示图片(图像)的方法 使用python画图,发现生成的图片在console里。不仅感觉很别扭,很多功能也没法实现(比如希望在一幅图里画两条曲线)。 想像matlab一样单独地生成一个图片窗口,然后我在网上找了一个多小时,都没有找到想要的,要么仅仅是画图的教…

python人工智能-动态显示图片的几种办法实现

python人工智能动态显示图片 任务简介&#xff0c;在电脑创建一个文件加&#xff0c;利用代码往文件加入照片文件&#xff0c; 然后动态显示该文件夹中的照片&#xff0c;显示给用户观看&#xff0c;可以利用open-cv&#xff0c;matplotlib,PIL等技术实现 这里是电脑一个装有照…

Python 3 显示图像的方法

如果你正在寻找一个库来帮助你在 Python 中显示图像&#xff0c;那么你很幸运。Python 有许多不同的库可用于显示图像。三个最受欢迎的库是Pickel、Matplotlib 和 OpenCV。 到底应该选哪一个&#xff0c;还是看自己的个人喜好了。我喜欢并使用 OpenCV 而不是 Pillow 和 Matplot…

python显示图片_python图像显示

广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 使用python进行数字图片处理,还得安装pillow包。 虽然python里面自带一个pil(python images library), 但这个库现在已经停止更新了,所以使用pillow, 它是由p…

python显示图片

python显示图片两种方法 1.利用Pillow包 未安装的可以通过pip install Pillow安装 from PIL import Image img Image.open(notMNIST_large/A/aGFua28udHRm.png) img.show() 显示的图片如下&#xff1a; 2.利用matplotlib 同样可以通过pip进行安装 from PIL import Image …

使用Python查看并显示图像

今天又来写一个Python的基础用法&#xff0c;使用Python来显示图像&#xff0c;不多说&#xff0c;代码如下&#xff1a; import matplotlib.pyplot as plt from matplotlib.image import imreadimg imread(C:/Users/xx/Desktop/xx.jpg) plt.imshow(img)plt.show() 2022.05.1…

基于python多光谱遥感数据处理、图像分类、定量评估及机器学习方法应用

普通数码相机记录了红、绿、蓝三种波长的光&#xff0c;多光谱成像技术除了记录这三种波长光之外&#xff0c;还可以记录其他波长&#xff08;例如&#xff1a;近红外、热红外等&#xff09;光的信息。与昂贵、不易获取的高光谱、高空间分辨率卫星数据相比&#xff0c;中等分辨…

推扫式和快照式高光谱成像系统在红肉掺假检测中的应用

目录 摘要 推扫式高光谱成像系统的应用 快照式高光谱成像系统的应用 总结 参考文献 摘要 肉类掺假常见于用低价肉代替高价肉获取非法利益&#xff0c;它不但影响了食品的质量&#xff0c;甚至会威胁到人们的健康。目前的检测手段一般是取样后送样检测&#xff0c;很难实时…

不同光谱信息的图像比较

首先&#xff0c;我们要知道普通光学相机在自然光下拍的照片其实并不包含完整的光谱信息&#xff0c;一般在遥感领域&#xff0c;多/高光谱数据应该包含近红外波段&#xff0c;例如Landsat有如下波段&#xff1a; 编号 波长&#xff08;m&#xff09; 1 0.433–0.453 深蓝/紫…

双目多光谱融合_Matlab标定+OpenCV

1.标定注意事项 (1)标定板平整度 (2)棋盘格&#xff0c;圆&#xff08;偏心误差&#xff09;&#xff1b;精度&#xff1a;圆>棋盘格&#xff0c;前提条件优化偏心误差 (3)所有标定数据加一起尽量布满整个视野 (4)左右相机采用近处标定数据分别进行单目标定&#xff0c;…

卫星扫描高光谱图像成像原理图

中文版高清高光谱图像卫星扫描成像原理图。 因为毕业论文需要&#xff0c;自己弄了个pdf高清版的。可以转成eps、svg、png等。 下面上传个截图。