如何使用 Anywall 在 Parse 上拍摄和保存照片? [关闭]

Posted

技术标签:

【中文标题】如何使用 Anywall 在 Parse 上拍摄和保存照片? [关闭]【英文标题】:How to take and save pictures on Parse with the Anywall? [closed] 【发布时间】:2015-08-07 09:59:13 【问题描述】:

我正在阅读 Parse.com 的 Anywall 和 MealSpotter 示例,我想知道如何结合它们的一些功能。例如,我将如何拍摄和保存图片,而不是简单地在 Anywall 示例的帖子中写文字?我会创建一个新的 CameraFragment 类,还是需要创建其他类?

【问题讨论】:

【参考方案1】:

我在 MealSpotter 应用程序之后为我的大学创建了一个应用程序。

创建一个使用 getter 和 setter 扩展 ParseObject 的类

public ParseFile getPhotoFile() 
    return getParseFile("photo");


public void setPhotoFile(ParseFile file) 
    put("photo", file);

然后,创建 CamaraFragment 类

public class CameraFragment extends Fragment 

public static final String TAG = "CameraFragment";

private Camera camera;
private SurfaceView surfaceView;
private ParseFile photoFile;
private ActionButton photoButton;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
        Bundle savedInstanceState) 
    View v = inflater.inflate(R.layout.fragment_camera, parent, false);

    photoButton = (ActionButton) v.findViewById(R.id.camera_photo_button);

    if (camera == null) 
        try 
            camera = Camera.open();
            photoButton.setEnabled(true);
         catch (Exception e) 
            Log.e(TAG, "No camera with exception: " + e.getMessage());
            photoButton.setEnabled(false);
            Toast.makeText(getActivity(), "No camera detected",
                    Toast.LENGTH_LONG).show();
        
    

    photoButton.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View v) 
            if (camera == null)
                return;
            camera.takePicture(new Camera.ShutterCallback() 

                @Override
                public void onShutter() 
                    // nothing to do
                

            , null, new Camera.PictureCallback() 

                @Override
                public void onPictureTaken(byte[] data, Camera camera) 
                    saveScaledPhoto(data);
                

            );

        
    );

    surfaceView = (SurfaceView) v.findViewById(R.id.camera_surface_view);
    SurfaceHolder holder = surfaceView.getHolder();
    holder.addCallback(new Callback() 

        public void surfaceCreated(SurfaceHolder holder) 
            try 
                if (camera != null) 
                    camera.setDisplayOrientation(90);
                    camera.setPreviewDisplay(holder);
                    camera.startPreview();
                
             catch (IOException e) 
                Log.e(TAG, "Error setting up preview", e);
            
        

        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) 
            // nothing to do here
        

        public void surfaceDestroyed(SurfaceHolder holder) 
            // nothing here
        

    );

    return v;


/*
 * ParseQueryAdapter loads ParseFiles into a ParseImageView at whatever size
 * they are saved. Since we never need a full-size image in our app, we'll
 * save a scaled one right away.
 */
private void saveScaledPhoto(byte[] data) 

    // Resize photo from camera byte array
    Bitmap bookImage = BitmapFactory.decodeByteArray(data, 0, data.length);
    // Trabajar con la foto aqui
    int h = bookImage.getHeight();
    int w = bookImage.getWidth();
    Bitmap bookImageScaled = Bitmap.createScaledBitmap(bookImage, 200, 200//800, 800
            * bookImage.getHeight() / bookImage.getWidth(), false);

    // Override android default landscape orientation and save portrait
    Matrix matrix = new Matrix();
    matrix.postRotate(90);

    Bitmap rotatedScaledBookImage = Bitmap.createBitmap(bookImageScaled, 0,
            0, bookImageScaled.getWidth(), bookImageScaled.getHeight(),
            matrix, true);
    /*Bitmap rotatedScaledBookImage = Bitmap.createBitmap(bookImage,0,0,bookImage.getWidth(),bookImage.getHeight(),matrix,true);*/

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    rotatedScaledBookImage.compress(Bitmap.CompressFormat.JPEG, 100, bos);

    byte[] scaledData = bos.toByteArray();

    // TEST SCALED DATA -> DATA

    // Save the scaled image to Parse
    photoFile = new ParseFile("book_photo.jpg", scaledData);
    photoFile.saveInBackground(new SaveCallback() 

        public void done(ParseException e) 
            if (e != null) 
                Toast.makeText(getActivity(),
                        "Error saving: " + e.getMessage(),
                        Toast.LENGTH_LONG).show();
             else 
                addPhotoToSaleAndReturn(photoFile);
            
        
    );


/*
 * Once the photo has saved successfully, we're ready to return to the
 * NewbookFragment. When we added the CameraFragment to the back stack, we
 * named it "NewMealFragment". Now we'll pop fragments off the back stack
 * until we reach that Fragment.
 */
private void addPhotoToSaleAndReturn(ParseFile photoFile) 
    //((SaleFragment) getActivity()).getCurrentSale().setPhotoFile(photoFile);
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Testttttttttttttt
    SaleFragment.getCurrentSale().setPhotoFile(photoFile);
    FragmentManager fm = getActivity().getSupportFragmentManager();
    fm.popBackStack("NewSaleFragment",
            FragmentManager.POP_BACK_STACK_INCLUSIVE);


@Override
public void onResume() 
    super.onResume();
    if (camera == null) 
        try 
            camera = Camera.open();
            photoButton.setEnabled(true);
         catch (Exception e) 
            Log.i(TAG, "No camera: " + e.getMessage());
            photoButton.setEnabled(false);
            Toast.makeText(getActivity(), "No camera detected",
                    Toast.LENGTH_LONG).show();
        
    


@Override
public void onPause() 
    if (camera != null) 
        camera.stopPreview();
        camera.release();
    
    super.onPause();

最后,将对象保存在你喜欢的activity中

// Save the sale and return
                      sale.saveInBackground(new SaveCallback() 

                           @Override
                           public void done(ParseException e) 
                               if (e == null) 
                                        getActivity().setResult(Activity.RESULT_OK);                                           

                               
                               else 
                                        Toast.makeText(
                                                getActivity().getApplicationContext(),
                                                "Error saving: " + e.getMessage(),
                                                Toast.LENGTH_SHORT).show();
                               
                           

                      );

【讨论】:

以上是关于如何使用 Anywall 在 Parse 上拍摄和保存照片? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

PFFile 大小问题与 parse 和 swift

如何在 iOS8.1 上使用 Swift 按顺序拍摄多张照片(每张延迟 1 秒)?

如何在 Parse Server 上配置推送通知?

无法快速将视频保存到 Parse

如何在 iOS 上拍摄全屏相机照片?

如何在 iPhone/iPad 上使用 iOS5 或 iOS6 拍摄的照片上添加文本标签?