BufferedImage的Andr​​oid中BufferedImage、Andr、oid

2023-09-12 08:08:57 作者:故事太撩人

我有一个应用程序,需要一个摄像头的图片,并保存在SD卡为JPEG。我想扭曲的画面,一个球面过滤器。我可以读取JPEG位图,但codeI已经发现,确实失真扭曲的BufferedImage。据我所知,javax.imageio中不支持Android的,但有没有读一个JPEG到存储器为BufferedImage的方式?

由于垫。

  / *
版权所有2006杰里Huxtable

Apache许可证下授权,版本2.0(以下简称许可证);
你可能不使用这个文件除了在遵守许可。
您可以在获得许可证的副本

   http://www.apache.org/licenses/LICENSE-2.0

除非适用法律要求或书面同意,软件
该许可证下发布分布在原样的基础上,
无担保或任何形式的条件,无论是EX preSS或暗示的保证。
查看许可证的特定语言的管理权限和
该许可证下的限制。
* /

包com.jhlabs.image;

进口java.awt中*。
导入的java.awt.geom *。
进口java.awt.image中*。

/ **
 *一个过滤器,模拟镜头置于图像。
 * /
公共类SphereFilter扩展TransformFilter {

    私人浮子上的= 0;
    私人浮子B = 0;
    私人浮子A2 = 0;
    私人浮动B2 = 0;
    私人浮动CENTREX = 0.5F;
    私人浮动centreY = 0.5F;
    私人浮动refractionIndex = 1.5F;

    私人浮动icentreX;
    私人浮动icentreY;

    公共SphereFilter(){
        setEdgeAction(CLAMP);
        setRadius(100.0f);
    }

    / **
     *设置refaction的指标。
     * @参数refractionIndex refaction的索引
     * @see #getRefractionIndex
     * /
    公共无效setRefractionIndex(浮动refractionIndex){
        this.refractionIndex = refractionIndex;
    }

    / **
     *获取refaction的指标。
     返回:refaction指数
     * @see #setRefractionIndex
     * /
    公众持股量getRefractionIndex(){
        返回refractionIndex;
    }

    / **
     *设置的效果的半径。
     *参数r是半径
     * @最小值0
     * @see #getRadius
     * /
    公共无效setRadius(浮点R){
        this.a = R;
        this.b = R;
    }

    / **
     *获取的效果的半径。
     返回:半径
     * @see #setRadius
     * /
    公众持股量getRadius(){
        返回;
    }

    / **
     *设置在X方向上图像尺寸的比例效果的中心。
     *参数CENTREX中心
     * @see #getCentreX
     * /
    公共无效setCentreX(浮动CENTREX){
        this.centreX = CENTREX;
    }

    公众持股量getCentreX(){
        返回CENTREX;
    }

    / **
     *设置在作为图像大小的比例Y方向的效果的中心。
     *参数centreY中心
     * @see #getCentreY
     * /
    公共无效setCentreY(浮动centreY){
        this.centreY = centreY;
    }

    / **
     *获取在作为图像大小的比例Y方向的效果的中心。
     返回:中心
     * @see #setCentreY
     * /
    公众持股量getCentreY(){
        返回centreY;
    }

    / **
     *设置的效果的图像大小的比例的中心。
     *参数中心中心
     * @see #getCentre
     * /
    公共无效setCentre(的Point2D中心){
        this.centreX =(浮点)centre.getX();
        this.centreY =(浮点)centre.getY();
    }

    / **
     *获取的效果作为图像尺寸的比例的中心。
     返回:中心
     * @see #setCentre
     * /
    公众的Point2D getCentre(){
        返回新Point2D.Float(CENTREX,centreY);
    }

    公共BufferedImage的过滤器(SRC的BufferedImage,BufferedImage的DST){
        INT宽度= src.getWidth();
        INT高= src.getHeight();
        icentreX =宽* CENTREX;
        icentreY =身高* centreY;
        若(a == 0)
            一个=宽度/ 2;
        如果(B == 0)
            B =高度/ 2;
        A2 = * A A;
        B2 = B * B;
        返回super.filter(SRC,DST);
    }

    保护无效transformInverse(INT X,INT Y,浮[]出){
        浮DX = X-icentreX;
        浮DY = Y-icentreY;
        浮X2 = DX * DX;
        浮Y2 = DY * DY;
        如果(Y2> =(B2  - (B2 *×2)/ A2)){
            出[0] = X;
            出来[1] = Y;
        } 其他 {
            浮rRefraction = 1.0F / refractionIndex;

            浮Z =(浮动)的Math.sqrt((1.0F  -  X2 / A2  -  Y2 / B2)*(A * B));
            浮Z2 = Z * Z;

            浮动xAngle =(浮点)Math.acos(DX /的Math.sqrt(X2 + Z2));
            浮角度1 = ImageMath.HALF_PI  -  xAngle;
            浮动ANGLE2 =(浮点)Math.asin(Math.sin(角度1)* rRefraction);
            ANGLE2 = ImageMath.HALF_PI  -  xAngle  -  ANGLE2;
            出来[0] = X  - (浮点)Math.tan(ANGLE2)* Z;

            浮动yAngle =(浮点)Math.acos(DY /的Math.sqrt(Y2 + Z2));
            角度1 = ImageMath.HALF_PI  -  yAngle;
            ANGLE2 =(浮点)Math.asin(Math.sin(角度1)* rRefraction);
            ANGLE2 = ImageMath.HALF_PI  -  yAngle  -  ANGLE2;
            出来[1] = Y  - (浮点)Math.tan(ANGLE2)* Z;
        }
    }

    公共字符串的toString(){
        返回扭曲/球球......;
    }

}
 

解决方案

没有。不能使用BufferedImage的,因为,就像你说的,javax.imageio中是不是在Android SDK。 Bitmap类,但不支持让使用的 与getPixel()的getPixels() 方法,所以你应该能够使用这些做任何类型的图像变换你想做的事。

I've an app that takes a camera picture and saves on sdcard as jpeg. i want to distort the picture with a spherize filter. I can read the jpeg to a bitmap, but the code i have found that does the distortion distorts a bufferedimage. i understand that javax.imageio is not supported in android but is there a way of reading a jpeg into the memory as a bufferedimage?

thanks mat.

/*
Copyright 2006 Jerry Huxtable

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.jhlabs.image;

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;

/**
 * A filter which simulates a lens placed over an image.
 */
public class SphereFilter extends TransformFilter {

    private float a = 0;
    private float b = 0;
    private float a2 = 0;
    private float b2 = 0;
    private float centreX = 0.5f;
    private float centreY = 0.5f;
    private float refractionIndex = 1.5f;

    private float icentreX;
    private float icentreY;

    public SphereFilter() {
        setEdgeAction( CLAMP );
        setRadius( 100.0f );
    }

    /**
     * Set the index of refaction.
     * @param refractionIndex the index of refaction
     * @see #getRefractionIndex
     */
    public void setRefractionIndex(float refractionIndex) {
        this.refractionIndex = refractionIndex;
    }

    /**
     * Get the index of refaction.
     * @return the index of refaction
     * @see #setRefractionIndex
     */
    public float getRefractionIndex() {
        return refractionIndex;
    }

    /**
     * Set the radius of the effect.
     * @param r the radius
     * @min-value 0
     * @see #getRadius
     */
    public void setRadius(float r) {
        this.a = r;
        this.b = r;
    }

    /**
     * Get the radius of the effect.
     * @return the radius
     * @see #setRadius
     */
    public float getRadius() {
        return a;
    }

    /**
     * Set the centre of the effect in the X direction as a proportion of the image size.
     * @param centreX the center
     * @see #getCentreX
     */
    public void setCentreX( float centreX ) {
        this.centreX = centreX;
    }

    public float getCentreX() {
        return centreX;
    }

    /**
     * Set the centre of the effect in the Y direction as a proportion of the image size.
     * @param centreY the center
     * @see #getCentreY
     */
    public void setCentreY( float centreY ) {
        this.centreY = centreY;
    }

    /**
     * Get the centre of the effect in the Y direction as a proportion of the image size.
     * @return the center
     * @see #setCentreY
     */
    public float getCentreY() {
        return centreY;
    }

    /**
     * Set the centre of the effect as a proportion of the image size.
     * @param centre the center
     * @see #getCentre
     */
    public void setCentre( Point2D centre ) {
        this.centreX = (float)centre.getX();
        this.centreY = (float)centre.getY();
    }

    /**
     * Get the centre of the effect as a proportion of the image size.
     * @return the center
     * @see #setCentre
     */
    public Point2D getCentre() {
        return new Point2D.Float( centreX, centreY );
    }

    public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
        int width = src.getWidth();
        int height = src.getHeight();
        icentreX = width * centreX;
        icentreY = height * centreY;
        if (a == 0)
            a = width/2;
        if (b == 0)
            b = height/2;
        a2 = a*a;
        b2 = b*b;
        return super.filter( src, dst );
    }

    protected void transformInverse(int x, int y, float[] out) {
        float dx = x-icentreX;
        float dy = y-icentreY;
        float x2 = dx*dx;
        float y2 = dy*dy;
        if (y2 >= (b2 - (b2*x2)/a2)) {
            out[0] = x;
            out[1] = y;
        } else {
            float rRefraction = 1.0f / refractionIndex;

            float z = (float)Math.sqrt((1.0f - x2/a2 - y2/b2) * (a*b));
            float z2 = z*z;

            float xAngle = (float)Math.acos(dx / Math.sqrt(x2+z2));
            float angle1 = ImageMath.HALF_PI - xAngle;
            float angle2 = (float)Math.asin(Math.sin(angle1)*rRefraction);
            angle2 = ImageMath.HALF_PI - xAngle - angle2;
            out[0] = x - (float)Math.tan(angle2)*z;

            float yAngle = (float)Math.acos(dy / Math.sqrt(y2+z2));
            angle1 = ImageMath.HALF_PI - yAngle;
            angle2 = (float)Math.asin(Math.sin(angle1)*rRefraction);
            angle2 = ImageMath.HALF_PI - yAngle - angle2;
            out[1] = y - (float)Math.tan(angle2)*z;
        }
    }

    public String toString() {
        return "Distort/Sphere...";
    }

}

解决方案

No. You can't use BufferedImage because, like you said, javax.imageio isn't in the Android SDK. The Bitmap class, however, does support getting individual pixels using the getPixel() and getPixels() methods so you should be able to use those to do any type of image transform you want to do.