如何裁剪图像与黑莓特定的形状? [英] how to crop image with particular shape in Blackberry?

查看:158
本文介绍了如何裁剪图像与黑莓特定的形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好感谢阅读我的回答希望你能帮助我。

Hi all thanks to read my answer hope you can help me

我的工作形象黑莓种植。在我的应用程序包含3个主要的东西。

I am working on Image cropping in blackberry. In my application contain 3 main things.

1)装载在屏幕上的图像

1)Load the image on the screen

2)选择裁剪区域的形状

2)select the shape of the cropping area

3)显示屏,在下一个屏幕上裁剪图像与出失去它的形状

3)display that cropping image on next screen with out losing its shape

第一步:我可以做到图像装载部分

Step1: i can done the image loading part

第二步:使用菜单我只是添加了4种形状

step2: using Menu i just add the 4 types of shapes

       1)Circle

        2)Rectangle with rounded shape

       3)Star 

       4)Cloud 

使用菜单

当他在任意一个菜单项单击,则该特定形状的图像会显示在屏幕上。

using menu when he click on any menu item ,then that particular shape image will display on the screen.

我们可以给运动的形象,因为我们要为他选择图像的任意部分。

we can give the movement to that image because we have to provide him to select any portion of the image.

第三步:修复后的位置,然后我们将允许用户使用菜单作物。
当他点击菜单项剪裁。那么我们要根据形状来裁剪图像,并且图像应显示下一个屏幕上

step3: After fix the position then we will allow the user to crop using menu. when he click on menu item " CROP". then we have to crop the image according the shape and that image should display on the next screen

注:以下code只为​​长方形的工作,但我想
  使用各种形状

Note: Following code working only for Rectangular shape but i want to use all shapes

这是我的样本code:

This is my sample code ::

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class ClipMove extends MainScreen{
    Bitmap circle_frame,rectangle_frame,star_frame,cloud_frame,image,selected_frame;
    BitmapField frmae_field;
    private int padding_x=0,padding_y=0;
    private VerticalFieldManager vrt_mgr;
    public ClipMove() {
        //Here my shape images are transparent  
        circle_frame=Bitmap.getBitmapResource("circle.gif");
        rectangle_frame=Bitmap.getBitmapResource("rect1.png");
        star_frame=Bitmap.getBitmapResource("star.gif");
        cloud_frame=Bitmap.getBitmapResource("cloud.gif");

        //this is my actual image to crop
        image=Bitmap.getBitmapResource("sample.jpg"); 

        vrt_mgr=new VerticalFieldManager(){
            protected void sublayout(int maxWidth, int maxHeight) {
                super.sublayout(Display.getWidth(),Display.getHeight());
                setExtent(Display.getWidth(),Display.getHeight());
            }

        };
        vrt_mgr.setBackground(BackgroundFactory.createBitmapBackground(image));


        add(vrt_mgr);
    }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);
        menu.add(new MenuItem("Rect",0,0) {
            public void run() {
                // TODO Auto-generated method stub
                vrt_mgr.deleteAll();
                selected_frame=rectangle_frame;
                frmae_field=new BitmapField(rectangle_frame);
                vrt_mgr.add(frmae_field);
                vrt_mgr.invalidate();

            }
        });

        menu.add(new MenuItem("Circle",0,0) {
            public void run() {
                // TODO Auto-generated method stub
                vrt_mgr.deleteAll();
                selected_frame=circle_frame;
                frmae_field=new BitmapField(circle_frame);
                vrt_mgr.add(frmae_field);
                vrt_mgr.invalidate();

            }
        });

        menu.add(new MenuItem("Star",0,0) {
            public void run() {
                // TODO Auto-generated method stub
                vrt_mgr.deleteAll();
                selected_frame=star_frame;
                frmae_field=new BitmapField(star_frame);
                vrt_mgr.add(frmae_field);
                vrt_mgr.invalidate();
            }
        });

        menu.add(new MenuItem("Cloud",0,0) {
            public void run() {
                // TODO Auto-generated method stub
                vrt_mgr.deleteAll();
                selected_frame=cloud_frame;
                frmae_field=new BitmapField(cloud_frame);
                vrt_mgr.add(frmae_field);
                vrt_mgr.invalidate();
            }
        });
        menu.add(new MenuItem("Crop",0,0) {
            public void run() {
                // TODO Auto-generated method stub
                Field f=vrt_mgr.getField(0);
//              XYRect rect=getFieldExtent(f);
                XYRect rect=new XYRect(padding_x, padding_y,frmae_field.getBitmapWidth(),frmae_field.getBitmapHeight());
                 Bitmap crop = cropImage(image, rect.x, rect.y,
                            rect.width, rect.height);
                 synchronized (UiApplication.getEventLock()) {
                    UiApplication.getUiApplication().pushScreen(new sampleScreen(crop,selected_frame));
                }

            }
        });

    }
    protected boolean navigationMovement(int dx, int dy, int status, int time) {
        if(frmae_field!=null){
            padding_x=padding_x+dx;
            padding_y=padding_y+dy;
            XYEdges edge=new XYEdges(padding_y, 0, 0, padding_x);
            frmae_field.setPadding(edge);
            vrt_mgr.invalidate();
            return true;
        }else {
            return false;
        }

    }

     public void DisplayMessage(final String str)
     {
         UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                Dialog.inform(str);
            }
        });
     }
     public XYRect getFieldExtent(Field fld) {
            int cy = fld.getContentTop();
            int cx = fld.getContentLeft();
            Manager m = fld.getManager();
            while (m != null) {
                cy += m.getContentTop() - m.getVerticalScroll();
                cx += m.getContentLeft() - m.getHorizontalScroll();
                if (m instanceof Screen)
                    break;
                m = m.getManager();
            }
            return new XYRect(cx, cy, fld.getContentWidth(), fld.getContentHeight());
       }
     // this logic only useful for rectangler shape 
     public  Bitmap cropImage(Bitmap image, int x, int y, int width,int height) {
            Bitmap result = new Bitmap(width, height);
            Graphics g = Graphics.create(result);
            g.drawBitmap(0, 0, width, height, image, x, y);
            return result;
     }
}
//this is my next screen to show the croped image 
class sampleScreen extends MainScreen
{
    VerticalFieldManager manager;
    public sampleScreen(final Bitmap img,final Bitmap back) {
        manager=new VerticalFieldManager(){
            protected void paint(Graphics graphics) {
                graphics.drawBitmap(0, 0, img.getWidth(), img.getHeight(), img, 0, 0);
                super.paint(graphics);
            }
            protected void sublayout(int maxWidth, int maxHeight) {
                super.sublayout( img.getWidth(), img.getHeight());
                setExtent( img.getWidth(), img.getHeight());
            }
        };
        BitmapField field=new BitmapField(back);
        field.setPadding(0, 0, 0, 0);
        manager.add(field);
        add(manager);
    }
}

我的屏幕截图:

推荐答案

通过使用另一虚设图象,有可能确定哪些需要被删除的原始图像的像素(我们可以使它们透明)。虽然这可能不是最佳的解决方案,但它可应用于任何几何图中我们可以在BlackBerry绘制

By using another dummy image, it is possible to determine which pixels of the original image needs to be deleted (we can make them transparent). Though It may not be the optimal solution, but it can be applied for any geometric figure we can draw on BlackBerry.

检查步骤如下:


  • 创建一个新的位图图像尺寸相同的( dummyImage
    源图像( MYIMAGE )。

  • 使用定义的颜色绘制(填充)就可以了目标几何形
    填充颜色)。

  • 现在,为 MYIMAGE 的每个像素,如果 dummyImage 的相同的像素
    包含填充颜色则保持不变,否则使这个像素
    通过分配零( 0 )将其完全透明。

  • 现在, MYIMAGE 已准备得差不多,需要修剪这个图像
    透明像素去除。

  • Create a new Bitmap image (dummyImage) of same dimension as the source image (myImage).
  • Draw (fill) the target geometric shape on it using a defined color (fillColor).
  • Now for each pixel of myImage, if the same pixel of dummyImage contains fillColor then keep it unchanged, else make this pixel fully transparent by assigning zero (0) to it.
  • Now myImage is almost ready, need to trim this image for transparent pixel removal.

继code将适用于图像在一个圆形的作物。 (但不会修剪透明像素)。

Following code will apply a circular crop on a image. (but won't trim the transparent pixels).

package mypackage;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;

class MyScreen extends MainScreen {
    private Bitmap myImage = Bitmap.getBitmapResource("img/myImage.jpeg");
    private BitmapField _bf;

    public MyScreen() {
        _bf = new BitmapField(myImage);
        adjustBitmapMargin();
        add(_bf);
    }

    private void adjustBitmapMargin() {
        int x = (Display.getWidth() - myImage.getWidth()) / 2;
        int y = (Display.getHeight() - myImage.getHeight()) / 2;
        _bf.setMargin(y, 0, 0, x);
    }

    protected void makeMenu(Menu menu, int instance) {
        menu.add(miCropCircle);
        super.makeMenu(menu, instance);
    }

    private MenuItem miCropCircle = new MenuItem("Crop - Circle", 0, 0) {
        public void run() {
            cropImage();
        }
    };

    private void cropImage() {
        int width = myImage.getWidth();
        int height = myImage.getHeight();

        // get original data from the image
        int myImageData[] = new int[width * height];
        myImage.getARGB(myImageData, 0, width, 0, 0, width, height);

        // get default color of a newly created bitmap
        int defaultColors[] = new int[1 * 1];
        (new Bitmap(1, 1)).getARGB(defaultColors, 0, 1, 0, 0, 1, 1);

        int defaultColor = defaultColors[0];
        int fillColor = Color.RED;
        int diameter = 200;

        // dummy data preparation
        Bitmap dummyImage = new Bitmap(width, height);
        Graphics dummyImageGraphics = Graphics.create(dummyImage);
        dummyImageGraphics.setColor(fillColor);
        int startX = width / 2 - diameter / 2;
        int startY = height / 2 - diameter / 2;
        dummyImageGraphics.fillArc(startX, startY, diameter, diameter, 0, 360);
        int dummyData[] = new int[width * height];
        dummyImage.getARGB(dummyData, 0, width, 0, 0, width, height);

        // filling original data with transparent value.
        int totalPixels = width * height;
        for (int i = 0; i < totalPixels; i++) {
            if (dummyData[i] == defaultColor) {
                myImageData[i] = 0;
            }
        }

        // set new data
        myImage.setARGB(myImageData, 0, width, 0, 0, width, height);

        // redraw screen
        _bf.setBitmap(myImage);
        adjustBitmapMargin();
        invalidate();

        // free up some memory here
        defaultColors = null;
        dummyImage = null;
        dummyData = null;
        dummyImageGraphics = null;
    }
}

结果
上述code的输出:
结果


Output of the above code:

这篇关于如何裁剪图像与黑莓特定的形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆