获取相机捕获的图像并创建位图图像

时间:2012-05-22 07:09:48

标签: blackberry

如何在原生相机应用程序退出后获取相机拍摄的图像?

如何从已有的SD卡Jpeg图像创建位图图像?

我想将此位图添加到屏幕上。

2 个答案:

答案 0 :(得分:2)

ButtonField btn_Take_Pic = new ButtonField("Take a pic",Field.FIELD_HCENTER|FOCUSABLE)
        {
            protected boolean navigationClick(int status, int time) {
                // TODO Auto-generated method stub

                InvokeCameraScreen screen = new InvokeCameraScreen(CaptureImage.this);
                screen.addCam(); 


                UiApplication.getUiApplication().invokeLater (new Runnable() {
                    public void run()
                    {
                        //Perform screen changes here.

                        //Calling invalidate() on your screen forces the paint
                        //method to be called.
                        CaptureImage deal_Screen = new CaptureImage();
                        deal_Screen.invalidate();
                    }
                });



                return true;
            }

        };

添加ImagePath()方法的正文部分

    public void ImagePath(String path) {
    try
    {
        Img_path = path;




EncodedImage.createEncodedImage(imgarr,0,imgarr.length);


        Bitmap setimage = resizeBitmap(getBitmapFromFile(info.getImg_Path()),(int) (ScreenWidth *0.20),(int) (ScreenHeight *0.20));
        Img_Field.setBitmap(setimage);


    }
    catch (Exception e) {
        // TODO: handle exception
        Dialog.alert("Ex path: " + e.toString());
    }
} 

实现ImagePath接口

public interface ImagePath {

public void ImagePath(String path);
}




public class InvokeCameraScreen implements FileSystemJournalListener {
private long lastUSN;
private String resultData, imagepath;
private VerticalFieldManager addToSCreen;
private boolean isAlreadeyExcuted = true;
private byte[] imageData;
private ImagePath pathImage;

public InvokeCameraScreen(ImagePath path) {
    this.pathImage = path;
}

public void addCam() {
    isAlreadeyExcuted = true;
    getAddToSCreen().deleteAll();
    startCam();
}

private void startCam() {
    lastUSN = FileSystemJournal.getNextUSN();
    UiApplication.getUiApplication().addFileSystemJournalListener(this);
    try {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA,
                        new CameraArguments());

            }
        });
    } catch (Exception e) {
        System.out.println("CheckinFormScreen Exception: " + e.toString());
    }
}

private void closeCamera() {
    System.out.println("Closing Cam");
    EventInjector.KeyEvent inject = new EventInjector.KeyEvent(
            EventInjector.KeyEvent.KEY_DOWN, Characters.ESCAPE, 0, 25);
    inject.post();
    try {
        Thread.sleep(500);
    } catch (Exception e) {
    }
    UiApplication.getUiApplication().removeFileSystemJournalListener(this);
    System.out.println("Done with closing cam");

}

public byte[] getImageData() {
    return imageData;
}

public void setImageData(byte[] imageData) {
    this.imageData = imageData;
}

public String getImagepath() {
    return imagepath;
}

public void setImagepath(String imagepath) {
    this.imagepath = imagepath;
}

public void fileJournalChanged() {
    try {
        closeCamera();
    } catch (Exception e) {
    }
    long USN = FileSystemJournal.getNextUSN();
    for (long i = USN - 1; i >= lastUSN && i < USN; --i) {
        FileSystemJournalEntry entry = FileSystemJournal.getEntry(i);
        if (entry == null) {
            break;
        }
        if (entry.getEvent() == FileSystemJournalEntry.FILE_ADDED) {
            String path = entry.getPath();
            if (path != null && path.indexOf(".jpg") != -1) {
                if (isAlreadeyExcuted) {
                    isAlreadeyExcuted = false;
                    resultData = "file://" + path;
                    pathImage.ImagePath(resultData);
                    // ShowUploadImage(resultData);
                    EventInjector.KeyEvent inject = new EventInjector.KeyEvent(
                            EventInjector.KeyEvent.KEY_DOWN,
                            Characters.ESCAPE, 0, 25);
                    inject.post();
                    inject.post();
                    if (Display.getWidth() == 480
                            && Display.getHeight() == 360
                            || Display.getWidth() == 360
                            && Display.getHeight() == 480) {
                        try {
                            Thread.sleep(500);
                            inject.post();
                        } catch (Exception e) {
                        }
                    }
                }
            }
            return;
        }
    }
}

public VerticalFieldManager getAddToSCreen() {
    if (addToSCreen == null) {
        addToSCreen = new VerticalFieldManager();
        addToSCreen.setPadding(10, 10, 10, 10);
    }
    return addToSCreen;
}

public void setAddToSCreen(VerticalFieldManager addToSCreen) {
    this.addToSCreen = addToSCreen;
}

private void ShowUploadImage(String path) {
}
}

答案 1 :(得分:0)

试试这个,

CAll Below Class Like:
      EncodedImage result = NcCamera.getInstance().getPictureTaken();

// 这是可以获取相机图像的类。

import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class NcCamera extends MainScreen 
{
    private VideoControl videoControl;
    private Field videoField;
    public static EncodedImage pictureTaken = null;
    private boolean initialized = false;
    private boolean result;

    public static boolean CAPTURING_DONE = true;
    public static boolean CAPTURING_CANCELED = false;

    private VerticalFieldManager main;
    private static NcCamera instance;
    CommonFunction cmn_fun;
    public static byte[] raw;


    public NcCamera() 
    {
        super(NO_VERTICAL_SCROLL);
        main = new VerticalFieldManager(USE_ALL_WIDTH | USE_ALL_HEIGHT) 
        {
            // I wan't to force the backgound to black
            public void paintBackground(Graphics g) 
            {
                g.setBackgroundColor(0x000000);
                g.clear();
            }
        };
        super.add(main);
    }
    public void add(Field field) 
    {
        main.add(field);
    }

    public EncodedImage getPictureTaken() 
    {
        return pictureTaken;
    }

    public Screen getThisScreen()
    {
        return this;
    }

    public void reset() 
    {
        pictureTaken = null;
        result = CAPTURING_CANCELED;
    }

    public boolean showDialog() 
    {
        result = CAPTURING_CANCELED;
        UiApplication.getUiApplication().invokeAndWait(new Runnable() 
        {
            public void run() 
            {
                UiApplication.getUiApplication().pushModalScreen(getThisScreen());
                if (pictureTaken != null) 
                    result = CAPTURING_DONE;
            }
        });
        return result;
    }

    public static NcCamera getInstance() 
    {
        if (instance == null) 
            instance = new NcCamera();
        return instance;
    }

    public void onDisplay() {
        if (!initialized) 
        {
            initializeCamera();
            if (videoField!=null) add(videoField);
            else {
                LabelField sorry = new LabelField("Sorry, we cannot use camera right now.") {
                    // I need to force the label to be white colored to be visible above
                    // it's underlying manager (that is black).
                    public void paint(Graphics g) {
                        int color = g.getColor();
                        g.setColor(0xffffff);
                        super.paint(g);
                        g.setColor(color);
                    }
                };
                add(sorry);
            }
            initialized = true;
        }
    }

    private void initializeCamera() 
    {
        try {
            // Create a player for the Blackberry's camera
            Player player = Manager.createPlayer("capture://video?encoding=jpeg&width=1024&height=768");

            // Set the player to the REALIZED state (see Player javadoc)
            player.realize();

            videoControl = (VideoControl) player.getControl("VideoControl");

            if (videoControl != null) 
            {
                videoField = (Field) videoControl.initDisplayMode(
                        VideoControl.USE_GUI_PRIMITIVE,
                        "net.rim.device.api.ui.Field");
                videoControl.setDisplayFullScreen(true);
                videoControl.setVisible(true);
            }

            player.start();
        } catch (Exception e) 
        {
        }
    }

    protected boolean invokeAction(int action) 
    {
        boolean handled = super.invokeAction(action);

        if (!handled)
        {
            switch (action) 
            {
            case ACTION_INVOKE: // Trackball click
            {
                takePicture();
                return true;
            }
            }
        }
        return handled;
    }

    public void takePicture()
    {       
        try {
            // Retrieve the raw image from the VideoControl and
            // create a screen to display the image to the user.
            raw = videoControl.getSnapshot(null);       

            //  SaveImageSdcard(raw);


            Bitmap img= Bitmap.createBitmapFromBytes(raw, 0, raw.length,3); 
            Constants.PICTURE_TAKEN=cmn_fun.resizeBitmap(img, 150, 150);

            // save the picture taken into pictureTaken variable
            pictureTaken = EncodedImage.createEncodedImage(raw, 0, raw.length);
            result = CAPTURING_DONE;

            close();
        } catch (Exception e) 
        {
        }
    }


    }

    public boolean keyDown(int keycode,
            int time) {
        if (keycode == 1769472) 
        { // escape pressed
            reset();
            close();
            return true;
        }
        return super.keyDown(keycode, time);
    }



}